我的gridview中有一个标题DDL,由于某种原因,它不会保留我选择的值,而是将gridview和标题绑定到" 起始位置 "
在优先级的DDL标题中,我选择了值' 99'但之后我的标题回到我ListItem
的起始位置(即优先级)
<HeaderTemplate>
<asp:DropDownList ID="ddlPriorityHeader" AutoPostBack="True" AppendDataBoundItems="True" OnSelectedIndexChanged="ddlHeader_SelectedIndexChanged" runat="server">
<asp:ListItem>Priority</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
我有一个RowDatabound
用于gridview但是我什么都没做,然后找到标题的DDL然后绑定DDL。
protected void gwActivity_RowDataBound(object sender, GridViewRowEventArgs e)
{
//.............. some code.....//
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT [Priority] FROM [BI_Planning].[dbo].[tblPriority]", con);
con.Open();
ddlPriority.DataSource = cmd.ExecuteReader();
ddlPriority.DataTextField = "Priority";
ddlPriority.DataBind();
}
}
我已将gridview
放入方法中:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewActivity();
}
}
我可以每次为我的网格视图绑定我的DDL吗?我被困在这里......
答案 0 :(得分:1)
您需要将ddlPriority
个所选文字存储在ViewState
之类的临时位置,以便在回发之间保持价值。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["PriorityText"] = "Priority";
BindGridviewActivity();
}
}
aftrt ddlPriority.DataBind();
将选择的文字设置为ViewState
值
ddlPriority.Items.FindByText(ViewState["PriorityText"].ToString()).Selected = true;
并在ddlHeader_SelectedIndexChanged
设置ViewState
到所选文字
protected void ddlHeader_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlHeader = (DropDownList)sender;
ViewState["PriorityText"] = ddlHeader.SelectedItem.Text;
}
查看此complete example了解详情