首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

[请问]RadioButton怎么取值并插入数据库

2012-01-23 
[请教]RadioButton如何取值并插入数据库c#代码(用于Repeater1显示题目和选项):protectedvoidPage_Load(obj

[请教]RadioButton如何取值并插入数据库
c#代码(用于Repeater1显示题目和选项):
protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                string   connstring   =   ConfigurationManager.ConnectionStrings[ "Conn "].ConnectionString;
                SqlConnection   wtConn   =   new   SqlConnection(connstring);
                wtConn.Open();
                SqlCommand   Command   =   new   SqlCommand( "select   qus,auc01,auc02,auc03,auc04   from   wenti ",   wtConn);
                SqlDataReader   reader   =   Command.ExecuteReader();
                Repeater1.DataSource   =   reader;
                Repeater1.DataBind();
                reader.Close();
                wtConn.Close();
        }
---------------------------------------
asp.net页面代码:
<asp:Repeater   ID= "Repeater1 "   runat= "server ">
                        <ItemTemplate>
<!--问题-->
<%#   Eval( "qus ")   %>
<!--选项-->
                                <asp:RadioButton   ID= "RadioButton1 "   GroupName= "rb "   runat= "server "   Text= "A "   value= ' <%#   Eval( "aus01 ")   %> '   />
                                <asp:RadioButton   ID= "RadioButton2 "   GroupName= "rb "   runat= "server "   Text= "B "   value= ' <%#   Eval( "aus02 ")   %> '   />
                                <asp:RadioButton   ID= "RadioButton3 "   GroupName= "rb "   runat= "server "   Text= "C "   value= ' <%#   Eval( "aus03 ")   %> '   />
                                <asp:RadioButton   ID= "RadioButton4 "   GroupName= "rb "   runat= "server "   Text= "D "   value= ' <%#   Eval( "aus04 ")   %> '   />
                        </ItemTemplate>
                </asp:Repeater>
---------------------------------------
IE里显示出的效果(问题和选项都是从数据表中读出的,选项的value根据问题的不同有所差异):

问题1         ○A○B○C○D
问题2         ○A○B○C○D
问题3         ○A○B○C○D
问题4         ○A○B○C○D

提交Button
---------------------------------------

请问如何把每题选的value插入到表jieguo的相应字段中,jieguo表里的字段分别为:



id     qus01     qus02     qus03     qus04
1           A             C             B             …
2           …           …           …           …
3           …           …           …           …
4           …           …           …           …

如果 "问题1 "选的 "A ",qus01就插入 "A ", "问题2 "选的 "C ",qus02就插入 "C ", "问题3 "选的 "B ",qus03就插入 "B "……以此类推。请问录入的代码该如何写呢,谢谢

[解决办法]
可以这样:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default18.aspx.cs " Inherits= "Default18 " %>

<!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 ">
<asp:Repeater ID= "Repeater1 " runat= "server " OnItemDataBound= "Repeater1_ItemDataBound " >
<ItemTemplate>
<div>
<!--问题-->
<asp:Label ID= "q " runat= "server "> <%# Eval( "qus ") %> </asp:Label>

<!--选项-->

<asp:RadioButtonList ID= "RadioButton1 " runat= "server " RepeatDirection= "Horizontal "> </asp:RadioButtonList>
</div>
</ItemTemplate>
<AlternatingItemTemplate>

<div>
<!--问题-->
<asp:Label ID= "q " runat= "server "> <%# Eval( "qus ") %> </asp:Label>
<!--选项-->
<asp:RadioButtonList ID= "RadioButton1 " runat= "server " RepeatDirection= "Horizontal "> </asp:RadioButtonList>
</div>

</AlternatingItemTemplate>
</asp:Repeater>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class Default18 : System.Web.UI.Page
{
string[] s = { "A ", "B ", "C ", "D " };
protected void Page_Load( object sender, EventArgs e )
{
if (!IsPostBack)
{
string connstring = "Data Source=(local);Initial Catalog=Book;User Id=sa;Password=; ";
SqlConnection wtConn = new SqlConnection(connstring);
wtConn.Open();


SqlCommand Command = new SqlCommand( "select qus,auc01,auc02,auc03,auc04 from wenti ", wtConn);
SqlDataReader reader = Command.ExecuteReader();
Repeater1.DataSource = reader;
Repeater1.DataBind();
reader.Close();
wtConn.Close();

}
}
protected void Button1_Click( object sender, EventArgs e )
{
foreach (RepeaterItem i in Repeater1.Items)
{
Response.Write( " <hr/> 问题: " + ((Label)i.FindControl( "q ")).Text);
Response.Write( "选择: " + ((RadioButtonList)i.FindControl( "RadioButton1 ")).SelectedValue);
}
}
protected void Repeater1_ItemDataBound( object sender, RepeaterItemEventArgs e )
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
System.Data.Common.DbDataRecord d = (System.Data.Common.DbDataRecord)e.Item.DataItem;
RadioButtonList r = (RadioButtonList)e.Item.FindControl( "RadioButton1 ");
for (int i = 0 ; i < 4 ; i++)
{
r.Items.Add(new ListItem(s[i], d[i + 1].ToString()));
}
}
}
}

热点排行