2017-03-16 24 views
0

これは非常に単純な問題のようですが、私の一日を殺しました。 私は更新可能なグリッドビューを持っており、列の編集モードにドロップダウンリストを置いています。 DDLの項目は静的に設定されます。しかし、選択された項目は常に最初の項目のままです。これをどうすれば解決できますか?ここgridviewのドロップダウンリストで、選択された値は変更されません

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のデータバインディングコードを見ずですが、私はあなたがIsPostBackプロパティのチェックにdababindingをラップしていないと思います。

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

あなたがこれをしなかった場合は、GridViewのは、ゼロから再構築され、DropDownListのは、それのデフォルトの位置に設定されます。

関連する問題