正在构建一个表,其中的数据基于下拉列表的选定值。 此表包含每行的复选框(默认情况下已选中)。 在按钮回发上,我只想获取选中复选框的行。
在初始加载和按钮回发后,这按预期工作。 但是当您在下拉列表的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>
DropDownList.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/>";
}
}
}
}
}
要查看我描述的行为: 在页面加载时,取消选中“First One”并单击“保存”。 “前两个”文本显示在“保存”按钮下(这是正确的)。 将下拉列表值更改为“秒”。 取消选中“Second One”,然后点击“保存”。 显示了“Second One Second Two”文本,其中只有“Second Two”。
感谢您的建议。
修改 我应该补充一点,这个例子模拟了实际应用程序中发生的事情的行为。 它可能需要一些关于实际工作的更多反馈。 下拉列表包含多个值。 根据此选择,从数据库中提取不同的数据。 该数据在显示的列数等方面也不同。 LoadTable将动态地将这些不同的数据添加到表中。 他们唯一的共同点是使用复选框。 因此,解决方案应该对LoadDropDownList和LoadTable函数进行最小的更改。
答案 0 :(得分: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/>";
}
}
}
}
}