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.
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){
}
}
Hope this helps