【问题标题】:Retain dropdown values in gridview在gridview中保留下拉值
【发布时间】:2014-02-06 09:33:53
【问题描述】:

我有一个网格,我使用复选框来编辑网格行。在复选框上单击如何保留下拉值?

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    首先创建一个这样的函数

     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

    【讨论】:

    • 我不会从数据库中提取值。我正在对下拉值进行硬编码。
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多