Selected Value of a DropDownList is never the good one

T

timor.super

Hi group,

I have a problem with my dropdownList. First, you have to know that
I'm working with the ViewState setted to false.

Consider this code, reproducing lightly my problem :

An aspx page with an ascx control (html code ommited for clarity) :

<%@ Page Language="C#" EnableViewState="false" AutoEventWireup="false"
CodeBehind="Default.aspx.cs" Inherits="testDropdown.Default" %>
<%@ Register Src="~/Test.ascx" TagName="Test" TagPrefix="Test" %>
<form id="form1" runat="server">
<Test:Test runat="server" id="TestControl" />
</form>

with code behind :

protected override void OnInit(EventArgs e)
{
EnableViewState = false;

TestControl.Property1 = "text1";
TestControl.Property2 = "text2";
TestControl.Property3 = "text3";

base.OnInit(e);
}

Ascx control (note that dropdown has AutoPostBack="true") :

<%@ Control Language="C#" AutoEventWireup="false"
CodeBehind="Test.ascx.cs" Inherits="testDropdown.Test" %>

<asp:DropDownList ID="myDropDown" runat="server" AutoPostBack="true"/>
<asp:Label ID="myLabel" runat="server" />

code behind :

public partial class Test : UserControl
{
private string _property1;
public string Property1
{
set { _property1 = value; }
}
private string _property2;
public string Property2
{
set { _property2 = value; }
}
private string _property3;
public string Property3
{
set { _property3 = value; }
}

protected override void OnLoad(EventArgs e)
{
myDropDown.Items.Add(_property1);
myDropDown.Items.Add(_property2);
myDropDown.Items.Add(_property3);

base.OnLoad(e);

if (IsPostBack)
{
string ctlName = Request.Params.Get("__EVENTTARGET");
if (!string.IsNullOrEmpty(ctlName))
if (ctlName.Contains(myDropDown.ID))
ItemChange(myDropDown, new EventArgs());
}
}

protected void ItemChange(object sender, EventArgs e)
{
myLabel.Text = string.Format("Choosen value : {0}",
((DropDownList)sender).SelectedValue);
}

}


My problem is when I run the sample and choose a value, it's always
the first one that is selected, I don't understand why.


Thanks in advance for your help.

Best regards,
Timor
 
B

bruce barker

you need to add the values to the dropdown in OnInit not OnLoad, as postback
data is applied between these to events.

-- bruce (sqlwork.com)
 
T

timor.super

you need to add the values to the dropdown in OnInit not OnLoad, as postback
data is applied between these to events.

-- bruce (sqlwork.com)

hi bruce,

thanks for your answer

My test brings me to the same result as you pointed, but my problem is
that I can't add values in the OnInit, because, as the values of the
ascx are setted in the OnInit of the aspx, if I add in the Onit of the
ascx, the properties are empty because it appears too earl.

How can I do ?
 
M

Milosz Skalecki [MCAD]

Populate the drop down in the Page_PreRender event handler:

protected void Page_PreRender(object sender, EventArgs e)
{
myDropDown.Items.Add(_property1);
myDropDown.Items.Add(_property2);
myDropDown.Items.Add(_property3);
}

In addition, it can be implemented in much simpler way:
Expose two properties:

public ListItemCollection Properties
{
get
{
return this.myDropDownList.Items;
}
}

public string SelectedProperty
{
get
{
return myDropDownList.SelectedValue;
}
set
{
myDropDownList.SelectedValue = value;
}
}

protected void Page_PreRender(object sender, EventArgs e)
{
myLabel.Text = SelectedProperty;
}

and then in the containing page
public void Page_Load(object sender, EventArgs e)
{
myUserControl.Properties.Add("property1");
myUserControl.Properties.Add("property2");
myUserControl.Properties.Add("property3");
}

HTH
 
T

timor.super

Populate the drop down in the Page_PreRender event handler:

protected void Page_PreRender(object sender, EventArgs e)
{
myDropDown.Items.Add(_property1);
myDropDown.Items.Add(_property2);
myDropDown.Items.Add(_property3);

}

In addition, it can be implemented in much simpler way:
Expose two properties:

public ListItemCollection Properties
{
get
{
return this.myDropDownList.Items;
}

}

public string SelectedProperty
{
get
{
return myDropDownList.SelectedValue;
}
set
{
myDropDownList.SelectedValue = value;
}

}

protected void Page_PreRender(object sender, EventArgs e)
{
myLabel.Text = SelectedProperty;

}

and then in the containing page
public void Page_Load(object sender, EventArgs e)
{
myUserControl.Properties.Add("property1");
myUserControl.Properties.Add("property2");
myUserControl.Properties.Add("property3");

}

HTH

Thanks for your answer,

but my web app has enableViewstate setted to false, so with your
method, selected value is always the first value of the drop down.

Bruce said that asp.net load post back datas between OnInt and OnLoad
event, so maybe should I set my properties in the user control
constructor.

Or is there another way ?

best regards
 

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

Top