2014-02-06 90 views
1

我有一個網格,我使用複選框來編輯網格行。在複選框上點擊我如何保留下拉值?在gridview中保留下拉值

<Columns> 
<asp:TemplateField> 
    <HeaderTemplate> 
     <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true"  OnCheckedChanged="OnCheckedChanged" /> 
    </HeaderTemplate> 
    <ItemTemplate> 
     <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" /> 
    </ItemTemplate> 
</asp:TemplateField> 

<asp:TemplateField HeaderText="Scope"> 
    <HeaderStyle HorizontalAlign="Center" Wrap="False" CssClass="header"> 
    </HeaderStyle> 
    <ItemTemplate> 
     <asp:Label ID="lblScope" runat="server" Text='<%# Bind("Scope") %>'></asp:Label> 
     <asp:DropDownList ID="ddlCMS" Visible="false" runat="server"> 
       <asp:ListItem>Yes</asp:ListItem> 
       <asp:ListItem>No</asp:ListItem> 
     </asp:DropDownList> 
    </ItemTemplate> 
    <ItemStyle Wrap="false" CssClass="header" /> 
    </asp:TemplateField> 
</Columns> 

我使用下面的代碼來檢查和取消選中要編輯的網格行。所以當我點擊複選框時,我無法零售gridview選擇的下拉值。例如:第三行有一個名爲scope的列,它有一個選定的值號。但是當我點擊複選框時,值爲Yes,因爲這是我在下拉列表中綁定的順序。

protected void OnCheckedChanged(object sender, EventArgs e) 
    { 
     bool isUpdateVisible = false; 
     CheckBox chk = (sender as CheckBox); 
     if (chk.ID == "chkAll") 
     { 
      foreach (GridViewRow row in Updates.Rows) 
      { 
       if (row.RowType == DataControlRowType.DataRow) 
       { 
        row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked; 
       } 
      } 
     } 
     CheckBox chkAll = (Updates.HeaderRow.FindControl("chkAll") as CheckBox); 
     chkAll.Checked = true; 
     foreach (GridViewRow row in Updates.Rows) 
     { 
      if (row.RowType == DataControlRowType.DataRow) 
      { 
       bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked; 
       for (int i = 1; i < row.Cells.Count; i++) 
       { 
        if (row.Cells[i].Controls.OfType<Label>().ToList().Count > 0) 
        { 
         row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked; 
        } 
        if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0) 
        { 
         row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked; 
        } 
        if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0) 
        { 
         row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked; 
        } 
        if (isChecked && !isUpdateVisible) 
        { 
         isUpdateVisible = true; 
        } 
        if (!isChecked) 
        { 
         chkAll.Checked = false; 
        } 
       } 
      } 
     } 
     btnSave.Visible = isUpdateVisible; 
    } 

回答

0

首先創建這樣一個功能,這

private DataSet GetData(string query) 
{ 
    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
    SqlCommand cmd = new SqlCommand(query); 
    using (SqlConnection con = new SqlConnection(conString)) 
    { 
     using (SqlDataAdapter da = new SqlDataAdapter()) 
     { 
      cmd.Connection = con; 
      da.SelectCommand = cmd; 
      using (DataSet ds = new DataSet()) 
      { 
       da.Fill(ds); 
       return ds; 
      } 
     } 
    } 
} 

和GridView控件的RowDataBound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //Find the DropDownList in the Row 
     DropDownList ddlRole = (e.Row.FindControl("ddlRole") as DropDownList); 
     ddlRole.DataSource = GetData("SELECT RoleType FROM TableName"); 
     ddlRole.DataTextField = "RoleType"; 
     ddlRole.DataValueField = "RoleType"; 
     ddlRole.DataBind(); 

     //Add Default Item in the DropDownList 
     ddlRole.Items.Insert(0, new ListItem("Please select")); 

     //Select the role of user in DropDownList 
     string role = (e.Row.FindControl("lblRole") as Label).Text; 
     ddlRole.Items.FindByValue(role).Selected = true; 
    }  
} 

確定可能是你可以嘗試的ViewState這個林不知道,可能是這些鏈接會幫助你

This

Here

+0

我不打算從數據庫中提取值。我很難編碼下拉值。 – Learner