C# dropdownlist在gridview中,选定值不变

C# dropdownlist在gridview中,选定值不变,c#,asp.net,gridview,C#,Asp.net,Gridview,这似乎是一个很简单的问题,但它扼杀了我的一天。 我有一个可更新的gridview,我在其中一列的编辑模式下放置了一个dropdownlist。DDL中的项是静态设置的。但所选项目始终保持第一个项目。我怎样才能解决这个问题 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string userID = ((Label)GridView1.Rows[e.RowIndex].Fin

这似乎是一个很简单的问题,但它扼杀了我的一天。 我有一个可更新的gridview,我在其中一列的编辑模式下放置了一个dropdownlist。DDL中的项是静态设置的。但所选项目始终保持第一个项目。我怎样才能解决这个问题

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string userID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lbluserID")).Text;

    string role = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue;

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = DBSettings.sqlConn;
    cmd.CommandText = "Update tbl_users set role=@role , pending='false' ,approved='true' , declined='false' where ID=@ID";
    cmd.Parameters.AddWithValue("@role", role);
    cmd.Parameters.AddWithValue("@ID", userID);
    cmd.ExecuteNonQuery();
    GridView1.EditIndex = -1;
    gridFill();
}
以下是gridView的aspx(仅ddl部分):

<asp:TemplateField HeaderText="Role">
    <EditItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            <asp:ListItem Value="">--Select--</asp:ListItem>
            <asp:ListItem Value="Admin" Text="Admin"></asp:ListItem>
            <asp:ListItem Value="Editor" Text="Editor"></asp:ListItem>
            <asp:ListItem Value="Viewer" Text="Viewer"></asp:ListItem>
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text="pending"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

这没有看到您的GridView数据绑定代码,但我认为您没有在IsPostBack检查中包装数据绑定

if (!Page.IsPostBack)
{
    GridView1.DataSource = yourSource;
    GridView1.DataBind();
}
如果没有这样做,GridView将从头开始重建,DropDownList将设置为其默认位置