Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 133, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

How to add PostBack to any HTML Web Control that supports JavaScript onClick event ...

I was recently working on one of my project and needed to add postback of a button to one of the images on my Page. After doing some research I found out that it is real easy to add Postbacks to your HTML elements that support Javascript onClick event ...

Here is How ...

            imgArrow.Attributes.Add("onclick", Page.GetPostBackEventReference(targetButton))

  This will fire the postback event


  Also sometimes you are generating anchor hyperlinks in your code behind and want the page to be posted back to the sever when someone clicks on those links rather then going to another page. Here is how to accomplish this ...


 // Uses the GetPostBackEventReference method to pass the command you want to execute for e.g
 // 'command1' to the RaisePostBackEvent method when the link this code creates is clicked.
 
lt.Text = "<a href=\"javascript:" + Page.GetPostBackEventReference(this,"Login") + "\">LogIn</a>";

 Assuming lt is the Literal on the page where we want to place the link ...then in the codebehind file ..

   if (Page.Request.Form["__EVENTTARGET"] == this.UniqueID)
   {
    switch (Page.Request.Form["__EVENTARGUMENT"])
    {
     case "DisplayRegUI":
      break;
     case "Login":
      Response.Write ("Inside the Login Postback functionality.");
      break;
     case "Register":
      break;
    }
   }

Print | posted on Wednesday, June 15, 2005 10:24 AM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: How to add PostBack to any HTML Web Control that supports JavaScript onClick event ...

Excellent - just what I needed. Thanks
3/9/2006 3:38 AM | Richard Slade

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: