What is ispostback

Support us .Net Basics C# SQL ASP..NET Aarvi MVC Slides C# Programs Subscribe Download

*

Suggested VideosPart 3 - ViewState in ASP.NETPart 6 - ASP.NET Page Life Cycle EventsPart 7 - ASP.NET Server Control EventsIsPostBack is a Page level property, that can be used to determine whether the page is being loaded in response lớn a client postbaông xã, or if it isbeing loaded và accessed for the first time.

Bạn đang xem: What is ispostback

In real time there are many situations where IsPostBaông chồng property is used. For example, consider the websize used to register employee details. Asample form that we will use for this example is shown below. The khung has First Name, Last Name and City fields.
*
If you want to follow along with me, copy & paste the following HTML in a website form.
table style="font-family: Arial"> tr> tdcolspan = "2">b>Employee Details Formb>td> /tr> tr> td>First Name: td> td>asp:TextBox ID="TextBox1" runat="server">asp:TextBox>td> /tr> tr> td>Last Name: td> td>asp:TextBox ID="TextBox2" runat="server">asp:TextBox>td> /tr> tr> td>City:td> td> asp:DropDownList ID="ddlCity" runat="server"> /asp:DropDownList> /td> /tr> tr> td>td> td> asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Register Employee" /> /td> /tr>/table>Copy và Paste the following code in the code behind file of the web size.protected void Page_Load(object sender, EventArgs e) LoadCityDropDownList();public void LoadCityDropDownList() ListItem li1 = new ListItem("London"); ddlCity.Items.Add(li1); ListItem li2 = new ListItem("Sydney"); ddlCity.Items.Add(li2); ListItem li3 = new ListItem("Mumbai"); ddlCity.Items.Add(li3);protected void Button1_Click(object sender, EventArgs e)Now run the application.

Xem thêm: Đạt Giải Nhất Tiếng Anh Là Gì ? Giải Khuyến Khích Tiếng Anh Là Gì

Look at the City DropDownList. The cities, (London, Sydney và Mumbai) are correctly shown as expected. Just cliông xã thebutton once. Notice, that the city names in the DropDownList are duplicated. So, every time you cliông xã the button, the thành phố names are again added tothe DropDownList.Let"s now underst& the cause for this duplication.We know that all ASP..NET hệ thống controls retain their state across postback. These controls make use of ViewState. So, the first time, when thewebsize load. the cities get correctly added to lớn the DropDownList and sent baông xã to lớn the client. Now, when the client clicks the button control, & thewebkhung is posted bachồng khổng lồ the server for processing. During the Page initialization, ViewState restoration happens. During this stage, the thành phố namesare retrieved from the viewstate & added khổng lồ the DropDownList. PageLoad sự kiện happens later in the life cycle of the webkhung. During page load weare again adding another set of cities. Hence, the duplication.How to lớn solve the DropDownList items duplicationThere are several ways to solve sầu this. One of the best ways to vì chưng this, is khổng lồ use IsPostBack property. So, in the Page_Load, callLoadCityDropDownList() method, if the request, is not a postbachồng request. That is, only if the webform is being loaded and accessed for the firsttime.

Xem thêm: Pointing Device Là Gì - Pointing Device Trong Tiếng Tiếng Việt

protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) LoadCityDropDownList(); Another way khổng lồ solve sầu, this problem is lớn simply disable the ViewState of the DropDowncác mục control. To disable the viewstate, right cliông chồng theDropDownList control, & phối EnableViewState property to false. Now run the project, & the cities duplication issue is gone. But the problem, withthis approach is that, the DropDownList danh sách, does not remember your selecttion across postbachồng. That is, Select "Mumabi" as the thành phố, & submit thesize. When the page rerenders, obhệ thống that selection is mix baông xã lớn "London". Another problem with, disabling the viewstate is that, theDropDownList events may not work correctly as expected.Another way to lớn solve this, is to lớn clear all the DropDownList items, before calling LoadCityDropDownList() method. But this not efficient from aperformance perspective. The modified code is shown below.protected void Page_Load(object sender, EventArgs e) ddlCity.Items.Clear(); LoadCityDropDownList();
*


Chuyên mục: Công Nghệ