Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi experts, am working on asp.net c# sql.

I have SerialNos in Database to fill Textbox. This SerialNo is Issued for Creating Tickets.

Am Retreving SerialNo into Textbox from Database as 20001.

I have a dropdownlist also on this page its autopostback property is true.

When I select item from dropdownlist it gets postback & Refreshes the Page ...and the value of the Textbox changes to 20002

Please can you give the solution.
Posted
Comments
ChintanShukla 1-Sep-14 3:57am    
Use Update Panel!

During work with our applications if we entered any values in textbox controls and click on a button in form we will see full postback of our page and we will lost all the controls values whatever we entered previously this happend because of postback. If we want to avoid this full postback of page and round trip to server we need to write much code instead of writing much code we can use ajax updatepanel control.
Ajax updatepanel will help us to avoid full postback of the page i.e., avoid refresh of the whole page content with postback and stop flickering of the page which is associated with a postback and allows only partial postbacks. By using Ajax updatepanel we can refresh only required part of page instead of refreshing whole page.
<asp:updatepanel id="UpdatePanel2" runat="server" xmlns:asp="#unknown">
 UpdateMode="Conditional">
 <contenttemplate>
<asp:textbox id="txtVal" runat="server" autopostback="True" ontextchanged="txtVal_TextChanged"/>
..........................................
 </asp:textbox></contenttemplate> 
 </asp:updatepanel>

Get More info here
understanding-asp-net-ajax-updatepanel-triggers[^]
 
Share this answer
 
v2
Hello ,
ASP.Net is a server side.Every Code you write executes on the server side.The page gets refreshed with every event & the complete page cycle follows.
In order to avoid postback ,you have to use ajax.if you are not familair with ajax . you have to refer following link:
Ajax Link

Tutorail on ajax

Ajax for beginers

Hope it helps you
 
Share this answer
 
v2
Right now in this scenario you can use Ispostback property of the page. and try not to hit the sql server in postbacks.

or if you don't want the page to sumbit to server then you have to use jquery or ajax.
 
Share this answer
 
To solve your problem, you must choose:
1. You can remove your code from page load or ensure it is not callet twice
2. Change the property of the dropdown list, so that it is not autopostback

But where to store the value you get then?
a. Session state (do you have session)
b. Page ViewState (do you have that)
c. Cookie (can you control if your users enable cookies?)

Essentially the challenges are that (a.) if your site maintains session state you will be able to serve significantly less traffic/ second. If you use ViewState (b.) your page roundtrips will carry a significantly bigger payload which will have to be sent to and from the server and (c.) cookies can be disabled.
So say we choose (a.) for persistance we have to solve(1.) the problem that every load will refresh your textbox value, you can make a session based member property to store it with. Presume you cannot change the autopostback mode of the dropdown. But I greatly recommend that you disable page viewstate, though if you had had it you could simply use the ispostback to only load controls on initial get.

C#
protected string SerialNo{
  get{
      string serial = Session["serialno"];
      if(string.isnullorempty(serial)){
         serial = GetNewSerialNumber();
         Session["serialno"] = serial;
      }
      return serial;
    }
}

Page_Load(object sender, eventargs e){
    MySerialNoTextBox.Text = SerialNo;
    if(!IsPostback){
      //Alternatively just load your serial number inhere, but that would require viewstate
      /*
      MySerialNoTextBox.Text = GetNewSerialNumber();
      */
    }
}


Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900