0

使用基于下拉列表的选定值的数据构建表。此表包含每行的复选框(默认选中)。在按钮回发时,我只想获取选中复选框的行。

在初始加载和按钮回发之后,这按预期工作。但是,当您在下拉列表的 OnSelectedIndexChanged 之后执行按钮回发时,会再次选中所有复选框。

我知道我必须让它在页面生命周期中工作,但不能让它正常工作。

我使用的代码如下:DynamicDropDownList.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicDropDownList.aspx.cs" Inherits="LittleApplication.DynamicDropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList runat="server" ID="ddlControl" OnSelectedIndexChanged="DropDownChanged" AutoPostBack="True"></asp:DropDownList><br/><br/>
        <asp:Table runat="server" ID="tblControl"></asp:Table><br/><br/>
        <asp:Button runat="server" ID="btnControl" Text="Save" 
            onclick="btnControl_Click" /><br/><br/>
        <asp:Label runat="server" ID="lblControl"></asp:Label>
    </div>
    </form>
</body>
</html>

下拉列表.aspx.cs

using System;
using System.Web.UI.WebControls;

namespace LittleApplication
{
    public partial class DynamicDropDownList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDropDownList();
            }
            LoadTable();
        }

        private void LoadDropDownList()
        {
            ddlControl.Items.Add(new ListItem { Text = "First", Value = "1" });
            ddlControl.Items.Add(new ListItem { Text = "Second", Value = "2" });
        }

        private void LoadTable()
        {
            tblControl.Rows.Clear();

            if (ddlControl.SelectedValue == "1")
            {
                var firstCheck = new CheckBox {Checked = true};
                var firstCellCheck = new TableCell();
                firstCellCheck.Controls.Add(firstCheck);
                var firstCellText = new TableCell();
                firstCellText.Text = "First One";
                var firstRow = new TableRow();
                firstRow.Cells.Add(firstCellCheck);
                firstRow.Cells.Add(firstCellText);
                tblControl.Rows.Add(firstRow);

                var secondCheck = new CheckBox {Checked = true};
                var secondCellCheck = new TableCell();
                secondCellCheck.Controls.Add(secondCheck);
                var secondCellText = new TableCell();
                secondCellText.Text = "First Two";
                var secondRow = new TableRow();
                secondRow.Cells.Add(secondCellCheck);
                secondRow.Cells.Add(secondCellText);
                tblControl.Rows.Add(secondRow);
            }
            else
            {
                var firstCheck = new CheckBox { Checked = true };
                var firstCellCheck = new TableCell();
                firstCellCheck.Controls.Add(firstCheck);
                var firstCellText = new TableCell();
                firstCellText.Text = "Second One";
                var firstRow = new TableRow();
                firstRow.Cells.Add(firstCellCheck);
                firstRow.Cells.Add(firstCellText);
                tblControl.Rows.Add(firstRow);

                var secondCheck = new CheckBox { Checked = true };
                var secondCellCheck = new TableCell();
                secondCellCheck.Controls.Add(secondCheck);
                var secondCellText = new TableCell();
                secondCellText.Text = "Second Two";
                var secondRow = new TableRow();
                secondRow.Cells.Add(secondCellCheck);
                secondRow.Cells.Add(secondCellText);
                tblControl.Rows.Add(secondRow);   
            }
        }

        protected void DropDownChanged(object sender, EventArgs e)
        {
            lblControl.Text = String.Empty;
            LoadTable();
        }

        protected void btnControl_Click(object sender, EventArgs e)
        {
            lblControl.Text = String.Empty;
            foreach (TableRow row in tblControl.Rows)
            {
                if (((CheckBox)row.Cells[0].Controls[0]).Checked)
                {
                    lblControl.Text += row.Cells[1].Text + "<br/>";
                }
            }
        }
    }
}

要查看我描述的行为:在页面加载时,取消选中“第一个”并单击“保存”。文本“前两个”出现在“保存”按钮下(这是正确的)。将下拉列表值更改为“第二”。取消选中“第二个”,然后单击“保存”。显示文本“Second One Second Two”,其中只有“Second Two”。

感谢您的建议。

编辑 我应该补充一点,这个例子模拟了实际应用程序中发生的事情的行为。它可能需要更多关于实际工作的反馈。下拉列表包含多个值。基于此选择,从数据库中获取不同的数据。该数据在显示的列数等方面也有所不同。LoadTable 会动态地将这些不同的数据添加到表中。它们唯一的共同点是使用复选框。因此,解决方案应该对 LoadDropDownList 和 LoadTable 函数进行尽可能少的更改。

4

1 回答 1

0

以下是完整代码:

using System;
using System.Web.UI.WebControls;

namespace LittleApplication
{
    public partial class DynamicDropDownList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
if (Convert.ToString(ViewState["Generated"]) == "true")
            {
                LoadTable();
            }

            if (!IsPostBack)
            {
                LoadDropDownList();
                LoadTable();
                HideShowRow();
            }


        }

        private void LoadDropDownList()
        {
            ddlControl.Items.Add(new ListItem { Text = "First", Value = "1" });
            ddlControl.Items.Add(new ListItem { Text = "Second", Value = "2" });
        }

        private void LoadTable()
        {
            // tblControl.Rows.Clear();
            tblControl.EnableViewState = true;

            //first Options rows generation
            var firstCheck = new CheckBox { Checked = true };
            var firstCellCheck = new TableCell();
            firstCellCheck.Controls.Add(firstCheck);
            var firstCellText = new TableCell();
            firstCellText.Text = "First One";
            var firstRow = new TableRow();
            firstRow.Cells.Add(firstCellCheck);
            firstRow.Cells.Add(firstCellText);
            tblControl.Rows.Add(firstRow);

            var secondCheck = new CheckBox { Checked = true };
            var secondCellCheck = new TableCell();
            secondCellCheck.Controls.Add(secondCheck);
            var secondCellText = new TableCell();
            secondCellText.Text = "First Two";
            var secondRow = new TableRow();
            secondRow.Cells.Add(secondCellCheck);
            secondRow.Cells.Add(secondCellText);
            tblControl.Rows.Add(secondRow);

            //Second Options rows generation and intially making it hidden
            firstCheck = new CheckBox { Checked = true };
            firstCellCheck = new TableCell();
            firstCellCheck.Controls.Add(firstCheck);
            firstCellText = new TableCell();
            firstCellText.Text = "Second One";
            var thirdRow = new TableRow();
            thirdRow.Cells.Add(firstCellCheck);
            thirdRow.Cells.Add(firstCellText);
            tblControl.Rows.Add(thirdRow);

            secondCheck = new CheckBox { Checked = true };
            secondCellCheck = new TableCell();
            secondCellCheck.Controls.Add(secondCheck);
            secondCellText = new TableCell();
            secondCellText.Text = "Second Two";
            var forthRow = new TableRow();
            forthRow.Cells.Add(secondCellCheck);
            forthRow.Cells.Add(secondCellText);
            tblControl.Rows.Add(forthRow);
        }

        private void HideShowRow()
        {
            if (ddlControl.SelectedValue == "1")
            {
                //Display table row first and Second
                tblControl.Rows[0].Visible = true;
                tblControl.Rows[1].Visible = true;

                tblControl.Rows[2].Visible = false;
                tblControl.Rows[3].Visible = false;
                ((CheckBox)tblControl.Rows[2].Cells[0].Controls[0]).Checked = false;
                ((CheckBox)tblControl.Rows[3].Cells[0].Controls[0]).Checked = false;
            }
            else if (ddlControl.SelectedValue == "2")
            {
                //Display table row third and forth
                tblControl.Rows[0].Visible = false;
                tblControl.Rows[1].Visible = false;
                ((CheckBox)tblControl.Rows[0].Cells[0].Controls[0]).Checked = false;
                ((CheckBox)tblControl.Rows[1].Cells[0].Controls[0]).Checked = false;

                tblControl.Rows[2].Visible = true;
                tblControl.Rows[3].Visible = true;
            }
        }

        protected void DropDownChanged(object sender, EventArgs e)
        {
            lblControl.Text = String.Empty;
            //LoadTable();
            HideShowRow();
        }

        protected void btnControl_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(ViewState["Generated"]) != "true")
            {
                LoadTable();
                ViewState["Generated"] = "true";
            }
            lblControl.Text = String.Empty;
            foreach (TableRow row in tblControl.Rows)
            {
                if (((CheckBox)row.Cells[0].Controls[0]).Checked)
                {
                    lblControl.Text += row.Cells[1].Text + "<br/>";
                }
            }
        }
     }
}
于 2013-11-08T14:32:52.800 回答