Problem with dynamic change of control value

A

Airshow

Hi,

I have very simple Page_Load code in some code-behind class in ASP.NET (v.
1.1):

private void Page_Load(object sender, System.EventArgs e)
{
if ( IsPostBack )
DropDownList1.SelectedIndex = 5;
else
{
FillDropDownList( DropDownList1 ); // some method to fill
values in list
DropDownList1.SelectedIndex = 0;
}
}

Everything is fine with selection first time Web page is loaded -- first
item in the dropdown list is selected. Problem is that selection does not
change to 5 when the page is loaded next time in response to some client
action -- it is always 0 in client side when page is reloaded. Same problem
goes for other controls in the form -- neither labels nor text boxes texts
can be changed dynamicaly in Page_Load. Any help please?

Airshow
 
K

Karl Seguin

Airshow:
Works fine for me. Are you sure there are 6 items in the dropdownlist? if
you have 5 or less items, doing selectedindex=5 won't work :)

Also, you say "next time in response to some client action" what's the
client action? Are they simply hitting "f5" to reload the page or are they
hitting an control which causes postback?

if you take this basic example:
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" Runat="server" />
<asp:Button id="btn" Runat="server" />
</form>

<script runat="server">
private void Page_Load(object sender, System.EventArgs e) {
if ( IsPostBack )
DropDownList1.SelectedIndex = 5;
else {
FillDropDownList( DropDownList1 ); // some method to fill
DropDownList1.SelectedIndex = 0;
}
}
private void FillDropDownList(DropDownList ddl) {
ddl.Items.Add("1");
ddl.Items.Add("2");
ddl.Items.Add("3");
ddl.Items.Add("4");
ddl.Items.Add("5");
ddl.Items.Add("6");
}
</script>

it works.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top