【问题标题】:dropdownlist in gridview , selected Value doesn't changegridview 中的下拉列表,选择的值不会改变
【发布时间】:2017-03-17 06:18:21
【问题描述】:

这似乎是一个非常简单的问题,但它已经扼杀了我的一天。 我有一个可更新的网格视图,并在其中一列的编辑模式下放置了一个下拉列表。 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>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

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

    if (!Page.IsPostBack)
    {
        GridView1.DataSource = yourSource;
        GridView1.DataBind();
    }
    

    如果您不这样做,则 GridView 将从头开始重建,并且 DropDownList 将设置为其默认位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 1970-01-01
      相关资源
      最近更新 更多