开发者

ASP.Net PostBack & Button Internal

开发者 https://www.devze.com 2023-04-03 07:27 出处:网络
i worked for long time with windows apps using .net. for last 2 year i am working with asp.net. we often work开发者_如何学Python with asp.net button control. when we click on button then postback occu

i worked for long time with windows apps using .net. for last 2 year i am working with asp.net. we often work开发者_如何学Python with asp.net button control. when we click on button then postback occur and right server side event called by asp.net engine. i how link & image button causes postback when user click on those button.

when we work with link & image button then __doPostback js function is called and that causes form submit to server and request the same page and asp.net engine detect which control causes the postback from the hidden input control called eventTarget and invoke right event handler for that control.

but i dont know when we work with asp.net button control like

<asp:Button ID="SampleButton" runat="server" 
        Text="Submit" 
        OnClick="ButtonClick" />

when we click button then also form submit but how. i know that in this case __doPostback does not invoke because for button __doPostback is never rendered in the page. so in this case form submit but how asp.net engine detect which button causes postback and invoke right event handler?

how we get the data from textbox like txt1.text when postback occur. is it extracted from viewstate....am i right.

please answer for my 2 question. try to explain here instead of giving any url...thanks.


It's pretty simple with respect to how controls postback. They do it in the same fashion as pure html solutions. There is a Form tag on the page. When the button is pressed it causes the form to submit a postback to the server.

Notice the action target of the form tag, it points to my aspx page This is all generated by the aspx page itself.

<form name="form1" method="post" action="Default.aspx" id="form1">
    <div>
         <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA0OTM4MTAwNGRk5H5eaMNZp5tsD/GQ2iYj2p8J0as=" />
    </div>

    <div>
          <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKmxpCiBgKSotaIC5iWtiHNi+oxYiuBULPgr5d0/WFu" />         
    </div>
    <div>
         <input type="submit" name="btn1" value="click me" id="btn1" />
    </div> 
</form>


Yes asp:button works differently by default. It outputs a standard HTML Submit button which posts back to the Action URL specified on the form. It therefore doesn't use events in the same way as other controls.

However if you add a OnCommand and assign CommandName/CommandArgs to the control, it will then cause the same event based callback behaviour as other controls. The same is true of OnClick. I assumed this used the same process as other controls but actually it's different.

It looks like it uses __VIEWSTATE and __EVENTVALIDATION to determine which form posted back, and the name of the submit button id is posted in the form as part of part of standard HTML form behaviuor. This is what is used to trigger the server-side events.

0

精彩评论

暂无评论...
验证码 换一张
取 消