2017-03-16 90 views
0

这似乎是一个非常简单的问题,但它已经杀死了我的一天。 我有一个可更新的gridview,我把一个下拉列表放在其中一列的编辑模式下。 DDL中的项目是静态设置的。但选定的项目始终保持第一项。我怎样才能解决这个问题?gridview中的dropdownlist,所选值不变

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 [email protected] , pending='false' ,approved='true' , declined='false' where [email protected]"; 
    cmd.Parameters.AddWithValue("@role", role); 
    cmd.Parameters.AddWithValue("@ID", userID); 
    cmd.ExecuteNonQuery(); 
    GridView1.EditIndex = -1; 
    gridFill(); 
} 

这里是GridView控件(只有DDL部分)的ASPX:

<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> 

回答

0

这是没有看到你的GridView的数据绑定代码,但是我觉得你没有包裹dababinding中的IsPostBack检查。

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

如果你不这样做,在GridView将从头开始重建和DropDownList的将被设置为它的默认位置。

相关问题