码迷,mamicode.com
首页 > 编程语言 > 详细

ASP.NET postback with JavaScript

时间:2019-07-09 19:23:15      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:receive   pos   +=   you   values   specific   its   bsp   ||   

ASP.NET postback with JavaScript

Here is a complete solution

Entire form tag of the asp.net page

<form id="form1" runat="server">
    <asp:LinkButton ID="LinkButton1" runat="server" /> <%-- included to force __doPostBack javascript function to be rendered --%>

    <input type="button" id="Button45" name="Button45" onclick="javascript:__doPostBack(‘ButtonA‘,‘‘)" value="clicking this will run ButtonA.Click Event Handler" /><br /><br />
    <input type="button" id="Button46" name="Button46" onclick="javascript:__doPostBack(‘ButtonB‘,‘‘)" value="clicking this will run ButtonB.Click Event Handler" /><br /><br />

    <asp:Button runat="server" ID="ButtonA" ClientIDMode="Static" Text="ButtonA" /><br /><br />
    <asp:Button runat="server" ID="ButtonB" ClientIDMode="Static" Text="ButtonB" />
</form>

Entire Contents of the Page‘s Code-Behind Class

 public partial class Main : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ButtonA.Click += ButtonA_Click;
            ButtonB.Click += ButtonB_Click;
        }

        private void ButtonA_Click(object sender, EventArgs e)
        {
            Response.Write("You ran the ButtonA click event");
        }

        private void ButtonB_Click(object sender, EventArgs e)
        {
            Response.Write("You ran the ButtonB click event");
        }
    }

The LinkButton is included to ensure that the __doPostBack javascript function is rendered to the client.

Simply having Button controls will not cause this __doPostBack function to be rendered.

This function will be rendered by virtue of having a variety of controls on most ASP.NET pages, so an empty link button is typically not needed

 

What‘s going on?

Two input controls are rendered to the client:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
  • __EVENTTARGET receives argument 1 of __doPostBack
  • __EVENTARGUMENT receives argument 2 of __doPostBack

The __doPostBack function is rendered out like this:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

As you can see, it assigns the values to the hidden inputs.

When the form submits / postback occurs:

  • If you provided the UniqueID of the Server-Control Button whose button-click-handler you want to run (javascript:__doPostBack(‘ButtonB‘,‘‘), then the button click handler for that button will be run.

前台JavaScript的theForm.submit();运行后,会触发后台绑定的click事件

 

What if I don‘t want to run a click handler, but want to do something else instead?

You can pass whatever you want as arguments to __doPostBack

You can then analyze the hidden input values and run specific code accordingly:

If Request.Form("__EVENTTARGET") = "DoSomethingElse" Then
    Response.Write("Do Something else") 
End If

Other Notes

  • What if I don‘t know the ID of the control whose click handler I want to run?
    • If it is not acceptable to set ClientIDMode="Static", then you can do something like this: __doPostBack(‘<%= myclientid.UniqueID %>‘, ‘‘).
    • Or: __doPostBack(‘<%= MYBUTTON.UniqueID %>‘,‘‘)
    • This will inject the unique id of the control into the javascript, should you wish it

 

ASP.NET postback with JavaScript

标签:receive   pos   +=   you   values   specific   its   bsp   ||   

原文地址:https://www.cnblogs.com/chucklu/p/11159222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!