[英]Dynamic drop down list's selected Index Changed event always return the first value
So, 所以,
I have a drop down list whish fill by a result of SQL query in my event page_Load. 我的事件page_Load中有一个由SQL查询结果填充的下拉列表。 What i want to do it's get the current value when the event selectedIndexChanges on my drop down list is fired and put it in a textbox. 当下拉列表中的事件selectedIndexChanges被触发并将其放在文本框中时,我想做的就是获取当前值。
Look my code below. 在下面看我的代码。
My drop down list definition: <asp:DropDownList ID="ddProfil" runat="server" Width="550" AutoPostBack="True" OnSelectedIndexChanged="ddProfil_SelectedIndexChanged" >
我的下拉列表定义: <asp:DropDownList ID="ddProfil" runat="server" Width="550" AutoPostBack="True" OnSelectedIndexChanged="ddProfil_SelectedIndexChanged" >
My page load event: protected void Page_Load(object sender, EventArgs e) { loadDDProfil(Request.QueryString["sitename"]); }
我的页面加载事件: protected void Page_Load(object sender, EventArgs e) { loadDDProfil(Request.QueryString["sitename"]); }
protected void Page_Load(object sender, EventArgs e) { loadDDProfil(Request.QueryString["sitename"]); }
Function which load the drop down list: 加载下拉列表的函数:
protected void loadDDProfil(string siteName)
{
SqlCommand requete = new SqlCommand();
requete.Connection = connWeb.ConnectionToDb;
requete.CommandType = System.Data.CommandType.Text;
string strReq = "ps_get_all_IndexProfil " + "MRF";
requete.CommandText = strReq;
DataTable dtPrf = connWeb.ExecuteQueryDB(requete);
SqlDataAdapter adapter = new SqlDataAdapter(requete);
adapter.Fill(dtPrf);
var dtSource = from p in dtPrf.AsEnumerable()
select new {
ind = p.Field("IndexProfil"),
DisplayedField = String.Format("{0} [ {1} ]", p.Field("NomProfil"), p.Field("Description"))
};
ddProfil.DataSource = dtSource;
ddProfil.DataValueField = "ind";
ddProfil.DataTextField = "DisplayedField";
ddProfil.DataBind();
ddProfil.SelectedIndexChanged += ddProfil_SelectedIndexChanged;
public void ddProfil_SelectedIndexChanged(object sender, EventArgs e)
{
string s = ddProfil.SelectedValue;
}
Thank you, for your help. 谢谢您的帮助。
I believe your answer is here https://taditdash.wordpress.com/2014/05/21/why-dropdownlist-selectedvalue-not-working-inside-selectedindexchanged-event/ 我相信您的答案在这里https://taditdash.wordpress.com/2014/05/21/why-dropdownlist-selectedvalue-not-working-inside-selectedindexchanged-event/
On your page load event try adding this: 在页面加载事件上,尝试添加以下内容:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadDDProfil(Request.QueryString["sitename"]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.