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

[C#]RadioButtonList控件取值有关问题

2012-01-19 
[C#]RadioButtonList控件取值问题我使用this.rbtnItems.SelectedValue取不到值.但使用Request.Form[rbtnI

[C#]RadioButtonList控件取值问题
我使用this.rbtnItems.SelectedValue取不到值.但使用Request.Form["rbtnItems"]却能取到.不知是什么原因

default.aspx
-------------------------------------------
  <form id="form1" runat="server">
  <table border="0" cellpadding="0" cellspacing="0" class="style1">
  <tr>
  <td bgcolor="#3333CC">
  <asp:Label ID="LabelTitle" runat="server" Text="Label"></asp:Label>
  </td>
  </tr>
  <tr>
  <td align="center" valign="middle">
  <asp:RadioButtonList ID="rbtnItems" runat="server" Height="53px" Width="672px">
  </asp:RadioButtonList>
  </td>
  </tr>
  <tr>
  <td align="center" bgcolor="Blue">
  <asp:Button ID="btnVote" runat="server" Text="投 票" onclick="btnVote_Click" />
&nbsp;<asp:Button ID="btnShow" runat="server" Text="结 果" />
  </td>
  </tr>
  </table>
  </form>

-------------------------------------------

default.aspx.cs
-------------------------------------------
  //创建Conn对象
  SqlConnection objConn = DB.createConnection();
  //打开Conn
  objConn.Open();
  //建立和查询
  SqlCommand cmd = new SqlCommand("select voteTitle from voteMaster where voteID=" + this.voteid, objConn);
  string strTitle = Convert.ToString(cmd.ExecuteScalar());
  this.LabelTitle.Text = strTitle;

  SqlCommand cmdItems = new SqlCommand("select * from voteDetails where voteId=" + this.voteid, objConn);
  SqlDataReader objRs = cmdItems.ExecuteReader();
  this.rbtnItems.DataSource = objRs;
  this.rbtnItems.DataTextField = "voteItem";
  this.rbtnItems.DataValueField = "voteDetailsID";
  this.rbtnItems.DataBind();

  objRs.Close();
  objConn.Close();


[解决办法]
//是否做了回传判断
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//绑定数据
}
}
[解决办法]
可以这样: 
<%@ 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())); 



}

热点排行