我在gridview中没有触发下拉列表的选定索引更改事件时遇到问题。我经历了SO Thread。它并不完全适合我。我有如下的实现。
.ASPX
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL1_SelectedIndexChanged">
<asp:ListItem Text="Review" Value="Review" Selected="True">Review</asp:ListItem>
<asp:ListItem Text="Level1" Value="lvl1">Send Back to Level1</asp:ListItem>
</asp:DropDownList>
.CS
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
}
protected void grdPoll_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(Page.IsPostBack)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DDL1") as DropDownList;
if(ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
}
}
}
}
当我只保留if(!Page.IsPostBack)
阻止时,它可以正常工作。但我也希望else
阻止。实施方式出了什么问题。能否请您提出解决方案
答案 0 :(得分:2)
问题是阻止!Page.IsPostBack阻止,这不是你说的其他事件。您在回发后再次绑定网格,导致丢失被触发的事件。您不必再次绑定它以在网格中进行更改。
删除此代码。
{
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
答案 1 :(得分:0)
试试这个:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
//DataBindGrid(); //remove DataBindGrid(); from else
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
DataBindGrid();
}
答案 2 :(得分:-1)
将事件名称“Page_Load”替换为“Page_PreRender”