高手请进来看看这个关于DataList的问题?
请问怎么读出checkbox是否选中的状态呀?页面代码
<%@ Page language="c#" Codebehind="WebForm8.aspx.cs" AutoEventWireup="false" Inherits="ItwwwAdmin.WebForm8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm8 </title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<STYLE type="text/css">.style2 { FONT-WEIGHT: bold; COLOR: #ffffff }
TD { FONT-SIZE: 12px }
.style3 { FONT-SIZE: 12px }
.style4 { FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #ffffff }
BODY { MARGIN: 0px }
</STYLE>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="1"
cellPadding="1" width="100%" border="1">
<TR>
<TD width="20%"> </TD>
<TD width="60%">
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<TABLE id="Table2" style="WIDTH: 512px; HEIGHT: 24px" cellSpacing="1" cellPadding="1" width="512"
border="1">
<TR>
<TD width="30%"> <%# DataBinder.Eval(Container.DataItem, "CsdlName") %> </TD>
<TD width="70%">
<asp:DataList id="DataList3" runat="server" DataSource = ' <%# DataBinder.Eval(Container.DataItem, "Myrelations") %>' RepeatColumns="5">
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" Text=' <%# DataBinder.Eval(Container.DataItem, "[\"CsxlName\"]")%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:DataList>
</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:DataList> </TD>
<TD width="20%"> </TD>
</TR>
<TR>
<TD width="20%"> </TD>
<TD align="center" width="60%">
<asp:Button id="Button1" runat="server" Text="查询"> </asp:Button> </TD>
<TD width="20%"> </TD>
</TR>
<TR>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>
程序代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using ItwwwAdminDll;
namespace ItwwwAdmin
{
/// <summary>
/// WebForm8 的摘要说明。
/// </summary>
public class WebForm8 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Button Button1;
Conn conn = new Conn();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
string sql1 = "select * from Ncsdl where Cpxlid = 1";
SqlDataAdapter da1 = new SqlDataAdapter(sql1,conn.myConn);
string sql2 = "select * from Ncsxl where Cpxlid = 1";
SqlDataAdapter da2 = new SqlDataAdapter(sql2,conn.myConn);
DataSet ds = new DataSet();
da1.Fill(ds,"Ncsdl");
da2.Fill(ds,"Ncsxl");
ds.Relations.Add("Myrelations", ds.Tables["Ncsdl"].Columns["Csdlid"],ds.Tables["Ncsxl"].Columns["Csdlid"]);
DataList1.DataSource = ds.Tables["Ncsdl"];
Page.DataBind();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
foreach (Control c in DataList1.Controls)
{
CheckBox chk = (CheckBox)c.FindControl("CheckBox1");
if (chk.Checked == true)
{
Response.Write(" <script language=javascript>");
Response.Write("window.alert('"+chk.Text+"!!')");
Response.Write(" </script>");
}
}
}
}
} --------------------编程问答-------------------- foreach (DataListItem item in DataList1.Items)
{
CheckBox chk = (CheckBox)item .FindControl("CheckBox1");
}
补充:.NET技术 , ASP.NET