Postbacks in
Postbacks in
Postbacks in
Ads by Google
ASP Net
Introduction
In this article, we will take a closer look at how ASP.NET pages post back to themselves, and how to customize this feature in our web applications.
<script language="VB" runat="server"> Sub Test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'enter your code to perform End Sub </script> <html> <body> <form runat="server" id="myForm"> <asp:linkbutton id="Test" runat="server" text="Create Text file" onclick="Test_Click" /> 10 </form> 11 </body> 12 </html>
Select this code
This is a very simple page. We declare only one web control, a linkbutton, to run on the server, with an ID of Test and we assign a method called Test_Click to run when the link is clicked on the page. The linkbutton has to be wrapped inside a form that runs on the server as well. We save the above as an ASPX page and then we browse to it. The HTML that gets created looks like this:
1 2 3 4 5 6 7 8 9
O t h e r
a r t i c l e s
i n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<html> <body> <form name="myForm" method="post" action="test.aspx" id="myForm"> <input type="hidden" name="__EVENTTARGET" value="" /> <input type="hidden" name="__EVENTARGUMENT" value="" /> <input type="hidden" name="__VIEWSTATE" value="dDwtMTAwOTA0ODU1NTs7PsllCK1DzvZsK0J6OQ2dxKqmEwVs" /> <script language="javascript">
<!-function __doPostBack(eventTarget, eventArgument) { var theform = document.myForm; theform.__EVENTTARGET.value = eventTarget; theform.__EVENTARGUMENT.value = eventArgument; theform.submit(); } // --> </script>
1. Smart headers and footers using ASP.NET User C ontrols December 23, 2002 Good site usability often means removing links from one page back to itself. In this article we will look at how to create an ASP.NET User C ontrol which will act as a common header to a site. It will automatically know which page we are looking at, and it will remove links to the same page from itself. For example, on this site, if we click on the About us section of the header, it will take you to the page, and it will make that link inactive. That way, we know that we are under that section, and we can't click on it anymore. 2. Maintaining Sorting while Paging in an ASP.NET Datagrid December 18, 2002 The Datagrid server control offers much control and flexibility in presenting data. Two of the actions
converted by Web2PDFConvert.com
19 20 21 22 23
Our link calls the javascript function __doPostBack when clicked (that's 2 underscore symbols in front of it). You do not write this function, instead it is generated by the ASP.NET engine and automatically included in our page. It submits the form to the same page, and accepts 2 arguments: 1. eventTarget: the control doing the submission 2. eventArgument: any additional information for the event For example, our generated link button, is telling the javascript function that the control submitting the form is the one with ID=Test, and no further information is needed for the second argument. The javascript function is actually setting 2 hidden form fields with these 2 arguments: __EVENTTARGET and __EVENTARGUMENT. When the form is submitted back to the server, the server reads these 2 hidden form fields and decides what submitted the form and performs the necessary action. In this case, the server will determine that the linkbutton performed the action, and will execute the Test_Click method. Lastly, I should point out that at least one control needs to be set to Visible, for the server to generate the __doPostBack function in our page. Even if we have numerous web controls declared to run on the server, but they are all set to Visible=false, then the javascript function will not be included and we will not be able to perform any actions. You can test this out, by changing the linkbutton source code to this and looking at the source code generated when it runs:
1 ... 2 <asp:linkbutton id="Test" runat="server" text="Create Text file" onclick="Test_Click" visible="false" /> 3 ...
that are hard-wired into it are Paging and Sorting. On their own they work great, but not so well together. When you sort a column and then move to a previous or next page, the sorting preference is not maintained. In this article we will see how to maintain both by using the Viewstate object. 3. Viewing and editing file and directory attributes in ASP.NET December 2, 2002 The System.IO.FileAttributes class gives us access to file/directory attributes. In this article, we'll see how to use this class to first read the current attributes and then change them. 4. C opying a directory in ASP.NET November 25, 2002 The System.IO.DirectoryInfo class does not come with a method to copy a directory. In this article, we'll see how to create a method to do that, and then use it in an ASP.NET page. 5. C reating and consuming a Web Service using Visual Studio .NET November 18, 2002 This article will go through the complete process of how to create a Web Service and then how to consume it in any ASPX page. We'll do this entirely within the Visual Studio.NET. Our Web Service will convert Fahrenheit degrees to C elcius.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<script language="VB" runat="server"> Sub CreateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim strFileName As String = funcParam.Value 'custom code to create a text file End Sub </script> <html> <head> <script language="JavaScript"> //ask for user input and then create file function CreateFile() {
Conclusion
We have seen how the postback function works with ASP.NET, and how to use it in our web applications. This article has concentrated on how to use this with a simple prompt box, but you can use a similar technique to combine the __doPostBack function with a showModalDialog function, or to return a value from another pop up window.
All content on the site is provided by Evagoras C haralambous. Feel free to use it as you please.
converted by Web2PDFConvert.com