c# - how to use __doPostBack function in asp.net -


i trying use __dopostback function can force page post pack on page load having difficulties understanding.

when looking @ examples online. on button click, want post not sure how complete code in code behind.

here have far:

<script type="text/javascript">         function dopostback() {             __dopostback('btnnew', 'myargument');         }     </script> 

here button

 <asp:button id="btnnew" runat="server" causesvalidation="false" commandname="new" onclick="dopostback()" text="create" /> 

i don't seem understand use "myargument" in code behind. need in code behind post on page load? in advance assistance.

scenario 1

if want use javascript function trigger postback, need replace onclick onclientclick. modify button definition (i'm assuming it's nested inside updatepanel):

<asp:button id="btnnew"      runat="server"      causesvalidation="false"      commandname="new"      onclientclick="dopostback();"      text="create" /> 

in code behind, in page_load method, read request["__eventtarget"] , request["__eventargument"]:

protected void page_load(object sender, eventargs e) {     if (page.ispostback)     {         if (request["__eventtarget"] == "btnnew" &&              request["__eventargument"] == "myargument")         {             //do         }     } } 

scenario 2

if don't want use javascript, modify button's definition this:

<asp:button id="btnnew"     runat="server"     causesvalidation="false"     commandname="new"     onclick="dopostback"     commandargument="myargument"     text="create" /> 

then, add following method in code behind:

protected void dopostback(object sender, eventargs e) {     var target = ((button)(sender)).clientid; //"btnnew"     var argument = ((button)(sender)).commandargument; //"myargument"     if (target == "btnnew" &&         argument == "myargument")     {         //do     } } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -