保留gridview中的下拉值

时间:2014-02-06 09:33:54

标签: c# asp.net 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的列有一个选中的值No.但是当我点击复选框时,值为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;
    }

1 个答案:

答案 0 :(得分: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