Fun With __doPostBack
Let's say you wanted to postback the page when the textbox txtBox is changed. First add the onChange attribute to the textbox with the __doPostBack call like so:
txtBox.Attributes.Add("onchange", "hasPageChanged();"); = "__doPostBack('" + txtBox.ClientID + "', 'Value to pass along');";
Now in your Page_Load event, we need to capture when this event occurs. While we're at it, we'll save the value passed (in this case "Value to pass along") into a string called myArgument (if this was a real app, we'd then proceed to do something really cool with this value, like save it to a database or arbitrarily convert the letters to their corresponding ASCII code simply because we can).
if (Request["__EVENTTARGET"] == txtBox.ClientID)
string myArgument = Request["__EVENTARGUMENT"].ToString();
This might not sound all this exciting, but when you combine __doPostBack with other ASP.net controls, you can do some pretty cool things. For instance, you can make it so that when a user clicks on a row in a gridView control, it goes into edit mode instead of forcing the user to have to click on a button. In fact, I'd like to write up that example if I can get around to finding the time.
This article has been view 3493 times.