انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

How postback works in ASP.NET

الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 2
أستاذ المادة حۡــسۜــنۨ ا̍ڷــڔهــٻۧــمۘــې       3/5/2012 4:45:36 PM
def : a postback is an http post to the same page that the form is on. in other words, the contents of the form are posted back to the same url as the form , in asp.net with the __dopostback() function and an application model that allows a page to perform validation and processing on its own form data. postbacks are commonly seen in edit forms, where the user updatings information in a form, hits save or submit, and the page is refreshed with a new form populated with the recently updatingd information.

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.

function __dopostback(eventtarget, eventargument)

one of the most important features of the asp.net environment is the ability to declare controls that run on the server, and post back to the same page. remember the days of classic asp? we would create a form which would accept the user s input, and then we would most probably have to create another page that would accept all those inputs, either through http get or post, and perform some kind of validation, display and action. sometimes, even a third page was necessary to perform our actions. this wasted a lot of time and complicated things when you had to make a change. but of course, this is not necessary any more with asp.net. there is no need to create second pages that accept the inputs of the first, process them and so on. form fields and other controls can be declared to run on the server, and the server simply posts the page back to itself and performs all the validation, display and actions.


when a control is declared to run on the server, a viewstate is created which remembers the id of that control, and the method to call when an action is performed. for example, let s say we input this html on a page:

< html>
< body>
< form runat="server" id="myform">
< asp:linkbutton id="test" runat="server" text="create text file" onclick="test_click" />
< /form>
< /body>
< /html>

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:


< 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>

< a id="test" href="javascript:__dopostback( test , )"> create text file< /a>
< /form>
< /body>
< /html>

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
1 - 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

المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .