|
Seems ok. But if you are not using "EventArgs e" you pass null instead of "new EventArgs();".
and on the other hand i think it is better to initialize event handlers in OnInit instead of Page_Load..
What do you think guys?
|
|
|
|
|
what you told is exactly correct.bcoz i got error
object ref is not set to an instance of an object.but i am not getting how to handle it.i thought u hav a good knowledge on this topic.plz help me...(wats the code i need to write)
|
|
|
|
|
//in MyControl.ascx.cs
public event System.EventHandler Hello;
//in Button_click event
///I suggest:
if(this.Hello != null) this.Hello(this, null);
//in default.aspx.cs
protected MyControl MyControl1;
//in page_Load XX
///override OnInit
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.MyControl.Hello +=new
EventHandler(MyControl_Hello);
}
//after Page_load
private void MyControl_Hello(object sender, EventArgs e)
{
//this is not a good way put a label and assing your value to it
Response.Write("welcome");
}
|
|
|
|
|
hi...
still i am getting same error.plzzzz check once again the code u posted.
|
|
|
|
|
man i don't know then your problem is something else,
try giving more detail about the error
|
|
|
|
|
Hi,
I have used javascript confirmation on button click.On button click confirmation message pop up correctly.
I have "update()" and "addnew()" function calls in button click event,If user click button confirmation message should get pop up and on confirmation
"update()" function should get called else "AddNew()" function should get called.
I hope someone know solution plz share ur idea with me.
Thanks,
Nagraj.
|
|
|
|
|
Response.Write( "<script language='JavaScript'>" +
" function showAchPendingMsg() { " +
" var pendingConfirmation = window.confirm('PG');" +
" if (pendingConfirmation == true) { ShowDetail()"+
" } else { DoShow()"+
" " +
" } " +
" }" +
" showAchPendingMsg();" +
" function DoShow() { alert('else');}"+
" function ShowDetail() { alert('if');}"+
"<" + "/" + "script>");
|
|
|
|
|
RegisterClientScriptBlock is better, and you didn't show him how to hook the call into his click event, either.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Is this the code wot u were talking about ? If yes will u plz lemme know why it is better?
string scriptString =( "" +
" function showAchPendingMsg() { " +
" var pendingConfirmation = window.confirm('PG');" +
" if (pendingConfirmation == true) { ShowDetail()"+
" } else { DoShow()"+
" " +
" } " +
" }" +
" showAchPendingMsg();" +
" function DoShow() { alert('else');}"+
" function ShowDetail() { alert('if');}"+
"<" + "/" + "script>");
string scriptName = "Confirm";
if(!Page.IsClientScriptBlockRegistered(scriptName);)
{
Page.RegisterClientScriptBlock(scriptName,scriptString);
}
|
|
|
|
|
He is telling instead of using Response.Write to send JS script, use RegisterClientScript which is in page object.
|
|
|
|
|
Yeah I understood.. But now I just want to know how it is better.. what is the utility of it.
|
|
|
|
|
What did you exactly tried to do with the Response.Write method? Let me say that clearly - You tried to Register blocks of client-side script containing functions . Microsoft has provide a method very specific to perform this particular task 'RegisterClientScriptBlock' .
Response.write Method is meant for writting a specified string to the current HTTP output. The specified string can be a Dynamically Build Javascript Code. But it's well suitable for a Javascript Alert message when we try to write Javascript code on the client using Response.Write(), Not for Registering blocks of client-side script containing functions. There are many ways to solve a problem. The question is Are we doing it the right way, the best way?
- Regards - JON Life is not measured by the amount of breaths we take, but by the moments that take our breath away.
|
|
|
|
|
This is not the exact difference. Response.Write() places the string (in this case dynamic JS code) supplied on the top of the page, that is outside HTML tag. But using the specified methods to emit client script ensures that it is inside the DOM. You can use either RegisterClientScriptBlock() or RegisterStartupScript() for injecting JS code to the page.
More over, RegisterStartupScript() executes before the page's OnLoad() event fires.
|
|
|
|
|
Thanks!!! Thats wot I wanted to know, If I am doing something I should know why I am doing so...
|
|
|
|
|
N a v a n e e t h wrote: This is not the exact difference
Did I explain about the Differences? I guess No. I tried to compare with what Prateek did with Response.Write() and What Could have been done.
- Regards - JON Life is not measured by the amount of breaths we take, but by the moments that take our breath away.
|
|
|
|
|
John Prabhu wrote: Did I explain about the Differences?
He was asking about why use client script methods to register instead of Response.Write. But your answer was not fully explaining. So I guessed you were comparing Response.Write and the other and given my reply.
|
|
|
|
|
i want to use images that is randomly generating codes in my sign up form. i got some sample programs from a site. but in this, the image is showing on the top left corner of the page. but i want to show it in a perticular place in my form. how to do it. plz help me...
|
|
|
|
|
|
here its using Response.BinaryWrite(); to display image.
plz check this link http://www.aspspider.net/resources/Resource272.aspx
|
|
|
|
|
The only way you can use binarywrite to place an image in a page is if you register a custom file type to handle a URL and write the image back. This is commonly what CAPTHA systems do
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi All,
I am using StoreProcedure to delete Row .I have passed Parameter GroupId to StoreProcedure ,to delete row.I am using Datasource to bind data in data grid .I want to delete Row from GridView ,onClick of Delete. But What is happening is,When i click on select then I need to click on Delete and It gets delete. I want to delete On click of delete.
Help me Plz.
Pallav
|
|
|
|
|
Place a delete button in the gridview and then on click of that button call the stored procedure to delete the row. And after deleting the row bind the gridview again.
Apurva Kaushal
|
|
|
|
|
Always welcome..
Apurva Kaushal
|
|
|
|
|
The first Question About "App_GlobalResources" , I did according to the teaching material, but can't display fi-FI, only display English. Thanks for your help. The following is original text:
\App_GlobalResources Folder
Resource files are string tables that can serve as data dictionaries for your applications when these applications require changes to content based on things such as changes in culture. You can add Assembly Resource Files (.resx) to this folder, and they are dynamically compiled and made part of the solution for use by all your .aspx pages in the application. When using ASP.NET 1.0/1.1, you had to use the resgen.exe tool and also had to compile your resource files to a .dll or .exe for use within your solution. Now it is considerably easier to deal with resource files in ASP.NET 2.0.
In addition to strings, you can also add images and other files to your resource files. For an example of how to use resource files to create a multilingual ASP.NET 2.0 application, first create the \App_GlobalResources folder in your application. For this example, create two resource files in this folder: Resource.resx and Resource.fi-FI.resx. The first file, Resource.resx, is the default language file using American English. The second file is for the same text, but in the Finnish language. Hence, this file uses fi-FI in its name. When someone with a browser culture of fi-FI invokes the page, he sees the information that comes from this file (Resource.fi-FI.resx). Everyone else who comes to the site gets the information that comes from the other file (Resource.resx).
Notice (as shown in Figure 3-11) that you can actually do a lot with .resx files. The idea is to create a table of the items to be localized (such as text, images, and files). For this example, you can stick to text.
Figure 3-11:
The Resource.resx file should have the following structure:
Name Value
Answer Hello there
PageTitle Sample Page
Question What is your name?
For the Resource.fi-FI.resx file, use the following structure:
Name Value
Answer Hei
PageTitle Näytesivu
Question Mikä sinun nimi on?
To use these files, create a simple .aspx page with the code from Listing 3-18.
Listing 3-18: A simple ASP.NET page that uses resource files
VB
<%@ Page Language= "VB " Culture= "Auto " UICulture= "Auto " %>
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
Page.Title = Resources.Resource.PageTitle
End Sub
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = Resources.Resource.Answer & " " & Textbox1.Text
End Sub
<title>
<%= Resources.Resource.Question %>
<asp:textbox id="Textbox1 " runat="server ">
<asp:button id="Button1 " runat="server " text="Submit "
="" onclick="Button1_Click ">
<asp:label id="Label1 " runat="server ">
C#
<%@ Page Language= "C# " Culture= "Auto " UICulture= "Auto " %>
protected void Page_Load(object sender, System.EventArgs e)
{
Page.Title = Resources.Resource.PageTitle;
}
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = Resources.Resource.Answer + " " + Textbox1.Text;
}
When this is run, you get the appropriate text based upon the culture setting in your browser. If this setting is not fi-FI, you get the American English text. The page output is shown in Figure 3-12.
Figure 3-12:
In order to see the Finnish text, change your preferred culture in the Microsoft Internet Explorer browser by choosing Tools ® Internet Options. This pulls up the Internet Options dialog. From the first tab, General, you can click the Languages button to pull up a dialog that enables you to specify the Finnish language as your preferred language choice. After you have added the Finnish language to the list, be sure that it is the uppermost choice in the dialog. You can do this by highlighting this choice and pressing the Move Up button until it is the uppermost choice.
After this is in place, run the page. You see the Finnish language output shown in Figure 3-13.
Figure 3-13:
The second Question About "callback" , I did according to the teaching material, but when I click the button, IE display an error, prompt the mothed "UseCallback();" has some error. Thanks for your help. The following is original text:
Using the Callback Feature — A Simple Approach
Begin examining the callback feature by looking at how a simple ASP.NET page uses it. For this example, you have only an HTML button control and a TextBox server control (the Web server control version). The idea is that when the end user clicks the button on the form, the callback service is initiated and a random number is populated into the text box. Listing 4-14 shows an example of this in action.
Listing 4-14: Using the callback feature to populate a random value to a Web page
.aspx page (VB version)
<%@ Page Language= "VB " AutoEventWireup= "false " CodeFile= "RandomNumber.aspx.vb "
Inherits= "RandomNumber " %>
<title> Callback Page
function GetNumber(){
UseCallback();
}
function GetRandomNumberFromServer(TextBox1, context){
document.forms[0].TextBox1.value = TextBox1;
}
<asp:textbox id="TextBox1 " runat="server ">
VB (code-behind)
Partial Class RandomNumber
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
Dim _callbackResult As String = Nothing
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
Dim cbReference As String = Page.ClientScript.GetCallbackEventReference(
Me, "arg ", "GetRandomNumberFromServer ", "context ")
Dim cbScript As String = "function UseCallback(arg, context) " & _
"{ " & cbReference & "; " & "} "
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _
"UseCallback ", cbScript, True)
End Sub
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
_callbackResult = Rnd().ToString()
End Function
Public Function GetCallbackResult() As String _
Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return _callbackResult
End Sub
End Class
C# (code-behind)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class RandomNumber : System.Web.UI.Page,
System.Web.UI.ICallbackEventHandler
{
private string _callbackResult = null;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this,
"arg ", "GetRandomNumberFromServer ", "context ");
string cbScript = "function UseCallback(arg, context) " +
"{ " + cbReference + "; " + "} ";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"UseCallback ", cbScript, true);
}
public void RaiseCallbackEvent(string eventArg)
{
Random rnd = new Random();
_callbackResult = rnd.Next().ToString();
}
public string GetCallbackResult()
{
return _callbackResult;
}
}
}
When this page is built and run in the browser, you get the results shown in Figure 4-15.
Figure 4-15:
Clicking the button on the page invokes the client callback capabilities of the page, and the page then makes an asynchronous request to the code behind of the same page. After getting a response from this part of the page, the client script takes the retrieved value and places it inside the text box — all without doing a page refresh!
Now take a look at the .aspx page, which simply contains an HTML button control and a TextBox server control. Notice that a standard HTML button control is used because a typical <asp:button> control does not work here. No worries. When you work with the HTML button control, just be sure to include an onclick event to point to the JavaScript function that initiates this entire process:
You don 't have to do anything else with the controls themselves. The final thing to include in the page is the client-side JavaScript functions to take care of the callback to the server-side functions. GetNumber() is the first JavaScript function that 's instantiated. It starts the entire process by calling the name of the client script handler that is defined in the page 's code behind. A string type result from GetNumber()is retrieved using the GetRandomNumberFromServer() function. GetRandomNumberFromServer() simply populates the string value retrieved and makes that the value of the Textbox control — specified by the value of the ID attribute of the server control (TextBox1):
function GetNumber(){
UseCallback();
}
function GetRandomNumberFromServer(TextBox1, context){
document.forms[0].TextBox1.value = TextBox1;
}
Now turn your attention to the code behind.
The Page class of the Web page implements the System.Web.UI.ICallbackEventHandler interface:
Partial Class RandomNumber
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
' Code here
End Class
This interface requires you to implement a couple of methods—the RaiseCallbackEvent and the GetCallbackResult methods, both of which work with the client script request. RaiseCallbackEvent enables you to do the work of retrieving the value from the page, but the value can be only of type string:
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
_callbackResult = Rnd().ToString()
End Sub
The GetCallbackResult is the method that actually grabs the returned value to be used:
Public Function GetCallbackResult() As String _
Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return _callbackResult
End Function
In addition, the Page_Load event includes the creation and placement of the client callback script manager (the function that will manage requests and responses) on the client:
Dim cbReference As String = Page.GetCallbackEventReference(Me, "arg ", _
"GetRandomNumberFromServer ", "context ")
Dim cbScript As String = "function UseCallback(arg, context) " & _
"{ " & cbReference & "; " & "} "
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _
"UseCallback ", cbScript, True)
The function placed on the client for the callback capabilities is called UseCallback(). This string is then populated to the Web page itself using the Page.ClientScript.RegisterClientScripBlock that also puts tags around the function on the page. Make sure that the name you use here is the same name you use in the client-side JavaScript function presented earlier.
In the end, you have a page that refreshes content without refreshing the overall page. This opens the door to a whole new area of possibilities. One caveat is that the callback capabilities described here use XmlHTTP and, therefore, the client browser needs to support XmlHTTP (Microsoft 's Internet Explorer and FireFox do support this feature). Because of this, the .NET Framework 2.0 introduces the SupportsCallBack and the SupportsXmlHTTP properties. To ensure this support, you could put a check in the page 's code behind when the initial page is being generated. It might look similar to the following:
VB
If (Page.Request.Browser.SupportsXmlHTTP) Then
End If
C#
if (Page.Request.Browser.SupportsXmlHTTP == true) {
}
<div class="ForumSig">http://www.123de6.cn</div>
|
|
|
|