ASP.Net PostBack & Button Internal - asp.net

i worked for long time with windows apps using .net. for last 2 year i am working with asp.net. we often work 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.

Related

ASP.NET DefaultButton is focused on page load

I'm setting the propriety DefaultButton on my asp form, in order to send the form on the key Enter.
Everything is working fine, but my button has the focus state on the page load which is bothering because of some CSS styles applied on the page.
<form id="loginForm" runat="server" DefaultButton="btnLogin">
...
</form>
I would like the button to not be focused on the page load.
Thank you for your help
This is the default behavior of default button in ASP.NET. What you can do to override that is to set default focus manually on some other control. To do so, call Page.SetFocus:
Page.SetFocus(SomeOtherControlID);
This will set focus control on the form like that:
<form id="loginForm" runat="server"
DefaultFocus="SomeOtherControlID"
DefaultButton="btnLogin">

simple html5 button trigger postback in asp.net webform

I have a simple HTML button to trigger jquery function but when i add this button to webform it triggers post-back.
Play
I tried adding OnClientClick="myfunction(); return false;" but it sill postback.
How can i use simple button without postback
Try adding stoppropagation() to your function
If you are using input tag with type="submit" or button tag with type="submit" than clicking it will cause the page to submit. So you may need to use type="button".
<input type="button" onclick="yourfunction()" value="SomeValue" />

Refresh page in asp.net

I have a webpage, in that page I have a button. How can I refresh the page when clicking on that button?
<input type="button" value="Reload" onclick="window.location.reload(true);" />
However, if the page is created from a postback, you would need to use an asp:Button control instead, and let another postback refresh the page. You also have to make sure that the correct code is executed in the code behind to recreate the correct result.
are you using an
<asp:button />
tag?
If so the page should refresh when the button is clicked by default.
<form>
<input TYPE="submit" VALUE="Submit Information Now" />
</form>
SUBMIT is a TYPE attribute value to the INPUT element for FORMs. It specifies a button that, when activated, submits the information entered to a processing script. If there are multiple SUBMIT buttons in a form, only the one activated should be sent to the form processing script.
When you press button your page must refresh, only not refresh that controls which are in AJAX Update Panel

How do I make a key binding/shortcut for an ASP.NET button to postback via that button?

I'm developing what's intended to be a very efficient UI in ASP.NET.
I want my users to be able to hit ALT-A to postback the form via a particular button. In other words, when they hit "ALT A" the form will post back and the event handler/function associated with a particular ASP.NET button control will run.
How can I do this? I have ASP.NET 2.0/3.5. I also have the Telerik control set at my disposal.
-KF
Use the "accesskey" attribute in HTML.
<input type="submit" accesskey="a" value="Submit">
<asp:Button AccessKey="a" runat="server" ....

When does an HTML input tag post back on enter?

I have a couple of asp.net pages with TextBox controls on them. Some of them postback on enter (within a TextBox control) and others don't. For the life of me, I can't figure out why or why not...
I don't think this has anything to do with asp.net. Not using ajax etc. Tried playing with a very simple html page and still can't figure it out.
Maybe within or not within a <form> tag? But all my asp:textbox controls are within the standard form tag and some do and some don't. Not related to autoPostback from what I can see.
EDIT:
Sample markup which causes a post back:
<html>
<body>
<form>
<input type=text />
</form>
</body>
</html>
I have asp.net pages that definately don't post back. The code is extensive and I can't figure out why not.
EDIT2:
How can I prevent a postback?
If you only have one textbox and don't wan't this behavior, you could always put a hidden input on the form.
<html>
<body>
<form>
<input type="text" ></input>
<input type="text" style="visibility:hidden"></input>
</form>
</body>
</html>
I've tried this in IE7 and FF3 and it works.
Generally most web browsers will detect when a form has only one textbox and will automatically submit the form when enter is pressed.
If you have multiple text boxes or multiple form buttoms, the behaviour varies. ASP.Net has the "defaultButton" mechanism for buttons, meaning you can specify which button is the submit button for a given panel in the case of the enter key being pressed. You could check out this post for more information on setting up your forms to correctly post back.
As Boo mentioned, this won't be triggered by multi-line textboxes.
If the behaviour is really crappy in your form between browsers, you may need to look at a javascript solution to catch the enter key being pressed, and "manually" submitting the form via javascript.
If the textbox control is set to autopostback it will postback on every keypress (thus allowing the firing of the TextChanged event).
Next, text boxes that have their Multiline property set to false will appear to postback on Enter.. what is actually happening is a form submit as defined by the behaviour within the browser.
Boxes that have Multiline set to true will not cause a form submit on Enter.

Resources