关于DataList中RadioButtonList的数据绑定?
程序为简单的在线考试:
数据库表Subject记录题库信息
[sID][int] identity(1,1) primary key, --索引ID
[sSort][varchar](100), --问题分类
[sQustion][varchar](1000), --问题队列
[sOption][varchar](2000), --选项队列
[sKey][varchar](50), --答案队列
[sScore][varchar](5), --问题分值
[sWritetime][datetime]default getdate(), --录入时间
[sCount][int]default 0 --被抽选次数
====================================
例如数据库插入值如下:
[sID] :1
[sSort] :文学类
[sQustion] :《全唐诗》收录的是唐代多少首诗?
[sOption] :A.10000多首|B.50000多首|C.4万2千多首
[sKey] :C
[sScore] :5
[sWritetime] :2005 6-23
[sCount] :0
====================================
程序文件default.aspx:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="testingForsinanj._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>default</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">TABLE {
FONT-SIZE: 12px
}
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<center>
<form id="default" method="post" runat="server">
----------------------------------------------------------------
<asp:datalist id="userItem" runat="server" Width="700px">
<HeaderTemplate>
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="700" align="center" border="0">
<TR>
<TD width="40%" height="25">答题者:
<asp:label id=userName runat="server" Text=''>
</asp:label></TD>
<TD width="40%" height="25">登录时间:
<asp:label id=logtime runat="server" Text=''>
</asp:label></TD>
<TD width="20%" height="25">时间:
<asp:label id=usingtime runat="server" Text=''>
</asp:label></TD>
</TR>
</TABLE>
<HR align="center" width="90%" noShade SIZE="1">
<TABLE id="Table2" cellSpacing="1" cellPadding="0" width="100%" border="0">
<TR style="COLOR: #ffffff" bgColor="#006699">
<TD align="center" width="40" height="20">编号</TD>
<TD align="center">问题</TD>
</TR>
</TABLE>
</FONT>
</HeaderTemplate>
<ItemTemplate>
<FONT face="宋体">
<TABLE id="Table8" cellSpacing="1" cellPadding="0" width="100%" bgColor="#efefef" border="0">
<TR bgColor="#ffffff">
<TD align="center" width="40" height="20"><%# DataBinder.Eval(Container.DataItem, "sID","{0}") %></TD>
<TD><%# DataBinder.Eval(Container.DataItem, "sQustion","{0}") %></TD>
</TR>
<TR bgColor="#ffffff">
<TD style="HEIGHT: 26px" width="40" height="26"></TD>
<TD style="HEIGHT: 26px">
<asp:RadioButtonList id="myOptions" runat="server" RepeatDirection="Horizontal"></asp:RadioButtonList></TD>
</TR>
</TABLE>
</FONT>
</ItemTemplate>
</asp:datalist>
-------------------------------------------------------------------
</form>
</center>
</body>
</HTML>
============================================
后值文件default.aspx.cs:
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;namespace testing
{
public class _default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList userItem; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//if(!IsPostBack)
{
String conn=ConfigurationSettings.AppSettings["conn"];
String sql="select sID,sQustion,sOption from Subject order by sID desc";
SqlConnection myconn=new SqlConnection(conn);
SqlCommand mycomm=new SqlCommand(sql,myconn);
myconn.Open();
SqlDataReader dr=mycomm.ExecuteReader();
while(dr.Read())
{
userItem.DataSource=dr;
========================
?问题:如何将字段[sOption]值以“|”拆分出来后绑定到
“<asp:RadioButtonList id="myOptions" runat="server" RepeatDirection="Horizontal">”
========================
userItem.DataBind();
}
dr.Close();
myconn.Close();
}
} #region Web Form Designer generated code
.............
}
========================================
1、问题:如何将字段[sOption]值以“|”拆分出来后绑定到
“<asp:RadioButtonList id="myOptions" runat="server" RepeatDirection="Horizontal">”
我用((RadioButtonList)userItem.FindControl("myOptions")).DataSource提示“未将对象引用设置到对象的实例”
2、绑定后如何获得用户选择的是哪一个选项?
========================================
望大虾们帮忙解决,谢谢!
程序为简单的在线考试:
数据库表Subject记录题库信息
[sID][int] identity(1,1) primary key, --索引ID
[sSort][varchar](100), --问题分类
[sQustion][varchar](1000), --问题队列
[sOption][varchar](2000), --选项队列
[sKey][varchar](50), --答案队列
[sScore][varchar](5), --问题分值
[sWritetime][datetime]default getdate(), --录入时间
[sCount][int]default 0 --被抽选次数
====================================
例如数据库插入值如下:
[sID] :1
[sSort] :文学类
[sQustion] :《全唐诗》收录的是唐代多少首诗?
[sOption] :A.10000多首|B.50000多首|C.4万2千多首
[sKey] :C
[sScore] :5
[sWritetime] :2005 6-23
[sCount] :0
====================================
程序文件default.aspx:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="testingForsinanj._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>default</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">TABLE {
FONT-SIZE: 12px
}
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<center>
<form id="default" method="post" runat="server">
----------------------------------------------------------------
<asp:datalist id="userItem" runat="server" Width="700px">
<HeaderTemplate>
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="700" align="center" border="0">
<TR>
<TD width="40%" height="25">答题者:
<asp:label id=userName runat="server" Text=''>
</asp:label></TD>
<TD width="40%" height="25">登录时间:
<asp:label id=logtime runat="server" Text=''>
</asp:label></TD>
<TD width="20%" height="25">时间:
<asp:label id=usingtime runat="server" Text=''>
</asp:label></TD>
</TR>
</TABLE>
<HR align="center" width="90%" noShade SIZE="1">
<TABLE id="Table2" cellSpacing="1" cellPadding="0" width="100%" border="0">
<TR style="COLOR: #ffffff" bgColor="#006699">
<TD align="center" width="40" height="20">编号</TD>
<TD align="center">问题</TD>
</TR>
</TABLE>
</FONT>
</HeaderTemplate>
<ItemTemplate>
<FONT face="宋体">
<TABLE id="Table8" cellSpacing="1" cellPadding="0" width="100%" bgColor="#efefef" border="0">
<TR bgColor="#ffffff">
<TD align="center" width="40" height="20"><%# DataBinder.Eval(Container.DataItem, "sID","{0}") %></TD>
<TD><%# DataBinder.Eval(Container.DataItem, "sQustion","{0}") %></TD>
</TR>
<TR bgColor="#ffffff">
<TD style="HEIGHT: 26px" width="40" height="26"></TD>
<TD style="HEIGHT: 26px">
<asp:RadioButtonList id="myOptions" runat="server" RepeatDirection="Horizontal"></asp:RadioButtonList></TD>
</TR>
</TABLE>
</FONT>
</ItemTemplate>
</asp:datalist>
-------------------------------------------------------------------
</form>
</center>
</body>
</HTML>
============================================
后值文件default.aspx.cs:
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;namespace testing
{
public class _default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList userItem; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//if(!IsPostBack)
{
String conn=ConfigurationSettings.AppSettings["conn"];
String sql="select sID,sQustion,sOption from Subject order by sID desc";
SqlConnection myconn=new SqlConnection(conn);
SqlCommand mycomm=new SqlCommand(sql,myconn);
myconn.Open();
SqlDataReader dr=mycomm.ExecuteReader();
while(dr.Read())
{
userItem.DataSource=dr;
========================
?问题:如何将字段[sOption]值以“|”拆分出来后绑定到
“<asp:RadioButtonList id="myOptions" runat="server" RepeatDirection="Horizontal">”
========================
userItem.DataBind();
}
dr.Close();
myconn.Close();
}
} #region Web Form Designer generated code
.............
}
========================================
1、问题:如何将字段[sOption]值以“|”拆分出来后绑定到
“<asp:RadioButtonList id="myOptions" runat="server" RepeatDirection="Horizontal">”
我用((RadioButtonList)userItem.FindControl("myOptions")).DataSource提示“未将对象引用设置到对象的实例”
2、绑定后如何获得用户选择的是哪一个选项?
========================================
望大虾们帮忙解决,谢谢!
解决方案 »
- dataSet如何获取存储过程返回的第二个结果集
- 急求.NET一个画曲线图的例子
- 求一段sql语句?
- 如何实现窗口关闭提示?给分。。
- exchange开发问题一则
- 如何置RadioButtonList中某一Radio为false或disabled之类的功能?
- 如何将DATAGRID中的记录导入到WORD和EXCEL中,如何打印DATAGRID中的内容?
- 如何让DataGrid中<HeaderTemplate >的列与<ItemTemplate >对齐?
- 求助 如何动态添加控件并动态添加其属性
- 自动分发邮件由哪些方法?
- 请各位帮小妹看看这个问题下载对话框的问题怎么实现?多谢!
- DataList只显示偶数行的内容是怎么回事?我的设置不对么?
源码如下:Default.aspx<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="Test1._Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>default</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">TABLE { FONT-SIZE: 12px }
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<center>
<form id="default" method="post" runat="server">
----------------------------------------------------------------
<asp:datalist id="userItem" runat="server" Width="700px">
<HeaderTemplate>
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="700" align="center" border="0">
<TR>
<TD width="40%" height="25">答题者:
<asp:label id="userName" runat="server" Text=''></asp:label></TD>
<TD width="40%" height="25">登录时间:
<asp:label id="logtime" runat="server" Text=''></asp:label></TD>
<TD width="20%" height="25">时间:
<asp:label id="usingtime" runat="server" Text=''></asp:label></TD>
</TR>
</TABLE>
<HR align="center" width="90%" noShade SIZE="1">
<TABLE id="Table2" cellSpacing="1" cellPadding="0" width="100%" border="0">
<TR style="COLOR: #ffffff" bgColor="#006699">
<TD align="center" width="40" height="20">编号</TD>
<TD align="center">问题</TD>
</TR>
</TABLE>
</FONT>
</HeaderTemplate>
<ItemTemplate>
<FONT face="宋体">
<TABLE id="Table8" cellSpacing="1" cellPadding="0" width="100%" bgColor="#efefef" border="0">
<TR bgColor="#ffffff">
<TD align="center" width="40" height="20"><%# DataBinder.Eval(Container.DataItem, "sID","{0}") %></TD>
<TD>
<%# DataBinder.Eval(Container.DataItem, "sQustion","{0}") %>
<input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "sID")%>' NAME="SelectedID"/>
</TD>
</TR>
<TR bgColor="#ffffff">
<TD style="HEIGHT: 26px" width="40" height="26"></TD>
<TD style="HEIGHT: 26px">
<asp:RadioButtonList id="myOptions" AutoPostBack="True" runat="server" RepeatDirection="Horizontal"></asp:RadioButtonList></TD>
</TR>
</TABLE>
</FONT>
</ItemTemplate>
</asp:datalist>
-------------------------------------------------------------------
<br>
<asp:Button id="btnSave" runat="server" Text="查看"></asp:Button>
</form>
</center>
</body>
</HTML>
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace Test1
{
/// <summary>
/// _Default 的摘要说明。
/// </summary>
public class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnSave;
protected System.Web.UI.WebControls.DataList userItem;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
BindGrid();
}
} private void BindGrid()
{
String conn = "Data Source=(local);Initial Catalog=nanami;User Id=sa;Password=123;";
String sql = "select sID,sQustion,sOption from Subject order by sID desc";
SqlConnection myconn=new SqlConnection(conn);
SqlDataAdapter da = new SqlDataAdapter(sql,myconn);
DataSet ds = new DataSet();
da.Fill(ds,"Subject");
userItem.DataSource = ds.Tables["Subject"].DefaultView;
userItem.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.userItem.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.userItem_ItemDataBound);
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnSave_Click(object sender, System.EventArgs e)
{
RadioButtonList rbtl;
foreach(DataListItem oDataListItem in userItem.Items)
{
rbtl = (RadioButtonList)oDataListItem.FindControl("myOptions");
string sID = ((HtmlInputHidden)oDataListItem.FindControl("SelectedID")).Value;
Response.Write(string.Format("第{0}题选中的是 ",sID));
if(rbtl.SelectedIndex != -1) //判断是否有没有选取的记录
{
Response.Write(rbtl.SelectedItem.Text + "<br>");
}
else
{
Response.Write("<font color=red>该题还没有选取答案</font><br>");
}
}
} private void userItem_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList rtblOption = (RadioButtonList)e.Item.FindControl("myOptions");
string strOption = ((DataRowView)e.Item.DataItem).Row.ItemArray[2].ToString();
string[] tmpOption = strOption.Split('|');
for(int i = 0; i<tmpOption.Length;i++)
{
rtblOption.Items.Add(tmpOption[i]);
}
}
} }
}
http://dotnet.aspx.cc/ShowDetail.aspx?id=60B8B07E-A3DF-4AE5-B5F8-71204C7C8793