问题描述
的构想
步骤1. DropDownList中充满了对的Page_Load项目。
步骤2.我选择一个项目,它发起回传。在这回发刷新页面,然后再OnSelectedIndexChanged火灾和ddl1_Select功能运行。
问题
如果我填静态与.aspx的(HTML)文件数据的DropDownList,它工作正常。
但是,如果我填它code-背后的OnSelectedIndexChanged事件永远不会触发和DDL1_Select程序不启动。页面只是回发,并跳过我的步骤。为什么没有这个事件火灾和如何使它工作?
ASPX:
<%@页面语言=C#的MasterPageFile =〜/ Master.masterAutoEventWireup =真
codeFILE =mypage.aspx.cs继承='我的空间标题=我的空间的EnableViewState =假%GT;...< ASP:DropDownList的ID =ddl1=服务器OnSelectedIndexChanged =ddl1_Select的AutoPostBack =真/>
ASPX.CS:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
ddl1.Items.Add(1);
ddl1.Items [0]。价值=1;
ddl1.Items.Add(2);
ddl1.Items [1]。价值=2;
ddl1.Items.Add(3);
ddl1.Items [2]。价值=3; 如果(会话[NewSelection]!= NULL)//注1
{
。字符串itemValue =会话[NewSelection]的ToString();
ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByValue(itemValue));
会话[NewSelection] = NULL; //见注1
}
}保护无效DDL1_Select(对象o,EventArgs的发送)
{
会话[NewSelection] = ddl1.SelectedValue;
Page.Response.Redirect(?mypage.aspx测试=+会话[NewSelection]的ToString());
}
注1 即可。这些指令从会话变量取旧索引用于prevent ddl1。所以,当我在ddl1选择一个项目,首先进入回发。在这回发ddl1从会话变量需要旧索引和我的新选择后丢失。这些指示我prevent它。但它并没有任何区别,除非主要问题是解决了。
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(!的IsPostBack)
{
ddl1.Items.Add(1);
ddl1.Items [0]。价值=1;
ddl1.Items.Add(2);
ddl1.Items [1]。价值=2;
ddl1.Items.Add(3);
ddl1.Items [2]。价值=3; 如果(会话[NewSelection]!= NULL)//注1
{
。字符串itemValue =会话[NewSelection]的ToString();
ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByValue(itemValue));
会话[NewSelection] = NULL; //见注1
}
}
}
THE CONCEPTION
Step 1. The DropDownList is filled with items on Page_Load.
Step 2. I select an item and it initiates postback. During this postback Page refreshes first, then OnSelectedIndexChanged fires and "ddl1_Select" function runs.
THE PROBLEM
If I statically fill the DropDownList with data in Aspx (HTML) file, it works properly. But, if I fill it in code-behind, the OnSelectedIndexChanged event never fires and DDL1_Select procedure doesn't start. Page just posts back and skips my procedure. Why doesn't this event fire and how to make it work?
ASPX:
<%@ Page Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true"
CodeFile="mypage.aspx.cs" Inherits="mypage" Title="mypage" EnableViewState="False" %>
...
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ddl1_Select" AutoPostBack="True"/>
ASPX.CS:
protected void Page_Load(object sender, EventArgs e)
{
ddl1.Items.Add("1");
ddl1.Items[0].Value = "1";
ddl1.Items.Add("2");
ddl1.Items[1].Value = "2";
ddl1.Items.Add("3");
ddl1.Items[2].Value = "3";
if (Session["NewSelection"] != null) // see note 1
{
string itemValue = Session["NewSelection"].ToString();
ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByValue(itemValue));
Session["NewSelection"] = null; // see note 1
}
}
protected void DDL1_Select(object o, EventArgs e)
{
Session["NewSelection"] = ddl1.SelectedValue;
Page.Response.Redirect("mypage.aspx?test=" + Session["NewSelection"].ToString());
}
Note 1. Those instructions are used to prevent ddl1 from taking old index from session variable. So, after I select an item in ddl1, first goes postback. During this postback ddl1 takes old index from session variable and my new selection is lost after that. With these instructions I prevent it. But it doesn't make any difference unless the main problem is resolved.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddl1.Items.Add("1");
ddl1.Items[0].Value = "1";
ddl1.Items.Add("2");
ddl1.Items[1].Value = "2";
ddl1.Items.Add("3");
ddl1.Items[2].Value = "3";
if (Session["NewSelection"] != null) // see note 1
{
string itemValue = Session["NewSelection"].ToString();
ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByValue(itemValue));
Session["NewSelection"] = null; // see note 1
}
}
}
这篇关于DropDownList的错误 - OnSelectedIndexChanged火灾只有当DDL填入HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!