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" %>
<aspropDownList 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
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" %>
<aspropDownList 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