我已经在下拉菜单中设置了AutoPostBack = true
,每次我现在从下拉菜单中选择一个项目时,它都会重新加载页面并将下拉菜单中的项目设置回第一项。当我删除autopostback = true时,这会将值保留在下拉列表中,但不会触发'OnSelectedIndexChanged'事件。我正在尝试将新选择的项目放入一个变量,该变量当前在以下方法中,但是由于某种原因,它不会触发此事件。
protected void Senior_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
DropDownList duty = (DropDownList)gvr.FindControl("SeniorDropDownList");
newseniorinitial = duty.SelectedItem.Text;
newseniorvalue = duty.SelectedItem.Value;
}
ASPX:
<asp:DropDownList ID="SeniorDropDownList" AutoPostBack="true" runat="server" Visible ="false" OnSelectedIndexChanged="Senior_SelectedIndexChanged"></asp:DropDownList>
上面的下拉代码位于gridview的一列中,并显示在<EditItemTemplate>
标记内,并且gridview周围包裹着一个更新面板。为什么我的OnSelectedIndexChanged
不会开火?
我的下拉列表正在RowDataBound
方法中填充,该方法在以下if语句中填充gridview时调用。
if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
{
...(Gets sql data into a datatable then does the following)
Label seniorlbl = (e.Row.FindControl("SeniorLabel") as Label);
DropDownList seniorddl = (e.Row.FindControl("SeniorDropDownList") as DropDownList);
seniorddl.Visible = true;
seniorlbl.Visible = true;
seniorddl.DataSource = dt;
seniorddl.DataTextField = "Initials";
seniorddl.DataValueField = "SeniorityOrder";
seniorddl.DataBind();
seniorddl.SelectedItem.Text = currentinitial;
}
页面加载:
protected void Page_Load(object sender, EventArgs e)
{
duplicatevalidation.Visible = false;
if (!Page.IsPostBack)
{
GetPivotView();
}
}
GridView方法:
protected void GetPivotView()
{
string connectionstring = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
SqlConnection cn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("[dbo].Rota", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
DataTable ds = new DataTable();
da.Fill(ds);
PivotView.DataSource = ds;
PivotView.DataBind();
}