JC Borlagdan
JC Borlagdan

Reputation: 3618

How to make onserverclick hit for html button in ASP.Net?

Yes I've read a lot on how to make this thing work, but unfortunately mine is a dynamic button and all id are unique; so it's created using StringBuilder in C# like so:

html.Append("<button id=\"" + dr["ID"].ToString() + "\" type=\"submit\" class=\"btn btn-primary btn-xs\" data-toggle=\"modal\" data-target=\"#myModal\" runat=\"server\" onserverclick=\"btnShowModal_ServerClick\"><i class=\"fa fa-eye\"></i></button>");

Obviously it opens up a modal, but I want to do something before it opens up the modal inside the btnShowModal_ServerClick event. Sadly it doesn't hit, the event, but it hits the Page_Load event because of the attribute type="submit" which makes the modal NOT show up. Any workaround on this? i've been spending 3hrs on this already.

Upvotes: 0

Views: 971

Answers (1)

David Chelliah
David Chelliah

Reputation: 1349

Based on your requirement, this is how you can implement

Create your dynamic HTML controls with OnClick event pointing to a javascript method btnShowModal_ServerClick

html.Append("<button id=\"" + dr["ID"].ToString() + "\" type=\"submit\" class=\"btn btn-primary btn-xs\" data-toggle=\"modal\" data-target=\"#myModal\" runat=\"server\" onclick=\"javascript:btnShowModal_ServerClick('" + dr["ID"] + "')\"><i class=\"fa fa-eye\"></i></button>");

Then in btnShowModal_ServerClick javaScript function, trigger your modal popup to show up. An important thing to note in my below JavaScript function, is how I assign a callback function to an imaginary popup. This callback will be invoked when you click OK in popup modal.

NOTE : Most modal plugins will have a way to specify the callback funtions on Yes or No selection

<script type="text/javascript">
function btnShowModal_ServerClick(parameterId)
{
      showModal({
          callBackFn : function(){ 
               __doPostBack('btnSave', parameterId);
          }
      });
 }
</script>

When you select yes, callBack function attached to the modal popup will execute and it will trigger a .NET postback specified within that.

So when the page postback & reloads you can test the Event argument to get back the value specified by dr["ID""] like below

public void Page_Load(object sender, EventArgs e)
{
  string parameter = Request["__EVENTARGUMENT"]; // parameter
  if(Request["__EVENTTARGET"] =="btnSave")
   { /*Do Something with the parameter = dr["ID"]*/}
}

Upvotes: 2

Related Questions