Fun Infused Games  |   Smooth Operator RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    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


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