datalist中checkbox项的后台取值取不到的问题
<asp:DataListID="DataList1"runat="server"HorizontalAlign="Left"RepeatColumns="5"Repea...
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Left"
RepeatColumns="5" RepeatDirection="Horizontal">
<HeaderTemplate><table><tr></HeaderTemplate>
<ItemTemplate><td align="left" >
<asp:CheckBox ID="itemCheckBox" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"TERMNO") %>' />
</td>
</ItemTemplate>
<FooterTemplate></tr></table></FooterTemplate>
</asp:DataList>
<asp:Button ID="btnRelease" runat="server" Text="Button"
onclick="btnRelease_Click" />
想实现,当点击button时,把datalist中checkbox是选中状态的添加至数据库中,测试时发现执行时checkbox都是未选中状态,怎么回事?
button点击事件
protected void btnRelease_Click(object sender, EventArgs e)
{
string s = "zzzzzzzzzzz";
foreach (DataListItem item in this.DataList1.Items)
{
CheckBox cbx = item.FindControl("itemCheckBox") as CheckBox;
if ( cbx.Checked )
{
s +=cbx.Text; }
}
Response.Write(s);
}
最终结果s = "zzzzzzzzzzz";而不是选中checkbox的text值
高手赐教 展开
RepeatColumns="5" RepeatDirection="Horizontal">
<HeaderTemplate><table><tr></HeaderTemplate>
<ItemTemplate><td align="left" >
<asp:CheckBox ID="itemCheckBox" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"TERMNO") %>' />
</td>
</ItemTemplate>
<FooterTemplate></tr></table></FooterTemplate>
</asp:DataList>
<asp:Button ID="btnRelease" runat="server" Text="Button"
onclick="btnRelease_Click" />
想实现,当点击button时,把datalist中checkbox是选中状态的添加至数据库中,测试时发现执行时checkbox都是未选中状态,怎么回事?
button点击事件
protected void btnRelease_Click(object sender, EventArgs e)
{
string s = "zzzzzzzzzzz";
foreach (DataListItem item in this.DataList1.Items)
{
CheckBox cbx = item.FindControl("itemCheckBox") as CheckBox;
if ( cbx.Checked )
{
s +=cbx.Text; }
}
Response.Write(s);
}
最终结果s = "zzzzzzzzzzz";而不是选中checkbox的text值
高手赐教 展开
2个回答
展开全部
页面Page_Load事件中加
if(!IsPostBack)
{
//绑定方法
}
示例代码:
#Test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!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:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text=<%# Eval("Aa")%> />
</ItemTemplate>
</asp:DataList>
</div>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</p>
</form>
</body>
</html>
--------------------------------------------
#Test.aspx.cs
using System.Collections;
public partial class Test : System.Web.UI.Page
{
public struct Data
{
private string aa;
public string Aa
{
get { return aa; }
set { aa = value; }
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data data = new Data();
ArrayList arr = new ArrayList();
data.Aa = "aa";
arr.Add(data);
data.Aa = "bb";
arr.Add(data);
data.Aa = "cc";
arr.Add(data);
this.DataList1.DataSource = arr;
this.DataList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string value = "aaaaaa";
value = "";
for (int i = 0; i < DataList1.Items.Count; i++)
{
if ((DataList1.Items[i].FindControl("CheckBox1") as CheckBox).Checked)
{
value += (DataList1.Items[i].FindControl("CheckBox1") as CheckBox).Text;
}
}
Response.Write(value);
}
}
if(!IsPostBack)
{
//绑定方法
}
示例代码:
#Test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!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:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text=<%# Eval("Aa")%> />
</ItemTemplate>
</asp:DataList>
</div>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</p>
</form>
</body>
</html>
--------------------------------------------
#Test.aspx.cs
using System.Collections;
public partial class Test : System.Web.UI.Page
{
public struct Data
{
private string aa;
public string Aa
{
get { return aa; }
set { aa = value; }
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data data = new Data();
ArrayList arr = new ArrayList();
data.Aa = "aa";
arr.Add(data);
data.Aa = "bb";
arr.Add(data);
data.Aa = "cc";
arr.Add(data);
this.DataList1.DataSource = arr;
this.DataList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string value = "aaaaaa";
value = "";
for (int i = 0; i < DataList1.Items.Count; i++)
{
if ((DataList1.Items[i].FindControl("CheckBox1") as CheckBox).Checked)
{
value += (DataList1.Items[i].FindControl("CheckBox1") as CheckBox).Text;
}
}
Response.Write(value);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询