2017-03-16 85 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的將被設置爲它的默認位置。

相關問題