Question:
hello
Experts
Could you let me know how .net knows or captures the Event. like Onclick,MouseDown mouseUP etc .
does it use API in it's code ?
Thanks ..
Answer1:
You have to define method that corresponds to delegate by it's signature.
void MyMethod()
{
Button btn = new Button();
btn.Click += new EventHandler(btn_Click);
}
protected void btn_Click(object sender, EventArgs e)
{
// button btn was clicked
}
If you are using ASPX/ASCX files then events are wired up for you automatically by default, so you don't have to attach them manually. Just make sure your event handlers are defined as protected or public. In ASPX/ASCX files you can use something like this <asp:Button runat="server" id="btn" Text="My Button" OnClick="btn_Click"/>>
Always make sure that your event handler corresponds to delegate type that is expected.
Answer2:
ya
Digimortal
that works .. but what i want is that how .net captures the Click Event
How Click Event is Attached with EventHandler Delegates by .Net manged Code
Answer3:
For things like buttons, it will read the request to see which button has a value which means that was the one that was clicked. For things like LinkButton, etc. it is using javascript to submit the form. I believe it is also setting a hidden input on the the page with at least the id of the control which invoked the postback. During the page life cycle for post back it will go through the Load Postback Data phase. Each control is responsible for taking it's postback data and process it. Example would be for each textbox on the page, it takes the postback data and populates the Text property with what is posted. That is why the Text property is updated with what the user typed in. During that phase, each control will check to see if it needs to raise events based on the data. An example with the TextBox is the changed event. That is fired when the TextBox reads in the posted information and checks it against what the text used to be before the postback. If it is different, if raises the change event. So for things like the LinkButton and such, during that phase it determines if it was responsible for the postback. If it was, the control raises the appropriate event, such as onclick.
Hope that helps.
Answer4:
Yes, there is hidden input that holds view state of a page and controls. There is also one hidden field that is used to say that this control caused the postback. If I'm correct there should be one hidden field more that sais what kind of event happened. You can write out all post variables to see what happens.
foreach(string key in Request.Form.Keys)
Response.Write(key + ": " + Request.Form[key] + "<br>");