Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Fun With __doPostBack
Date 7/17/2008    Tags ASP.net, C#    (0)

This post is more for my reference than anything (as I often use this and have found myself looking up the usage more often than I like), but if it helps you out too, then great. I often find myself having the need to postback a page via a client-side JavaScript call. In order to do that, I call on my good friend __doPostBack.

__doPostBack is a JavaScript function that takes two arguments, eventTarget and eventArgument. eventTarget must contain the ID of the control that causes the postback and eventArgument will contain any additional information that you want to pass along with your postback.

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. 0"<


This article has been view 3493 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)