注册 登录
编程论坛 ASP.NET技术论坛

[求助]关于RadioButtonList中的性别代码应该怎么样写

vidhl 发布于 2007-09-06 11:09, 2228 次点击
想问哈大家
用RadioButtonList来设置性别,
那个代码应该怎么样写
int sex=Convert.ToInt32("RadioButtonList1.SelectedItem.Value");
但浏览的时候他出出错了
16 回复
#2
vidhl2007-09-06 11:36

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;
namespace Web96
{
/// <summary>
/// zhuce 的摘要说明。
/// </summary>
public class zhuce : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtzhanghao;
protected System.Web.UI.WebControls.TextBox txtpass;
protected System.Web.UI.WebControls.TextBox txtrepass;
protected System.Web.UI.WebControls.TextBox txtmail;
protected System.Web.UI.WebControls.TextBox txtwen;
protected System.Web.UI.WebControls.TextBox txtda;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.CompareValidator CompareValidator2;
protected System.Web.UI.WebControls.DropDownList DropDownList4;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void LinkButton1_Click(object sender, System.EventArgs e)
{
string users=txtzhanghao.Text;
string pass=txtpass.Text;
string mail=txtmail.Text+DropDownList4.SelectedItem.Value;
string wenti=txtwen.Text;
string huida=txtda.Text;
string birth=DropDownList1.SelectedItem.Value+"-"+DropDownList2.SelectedItem.Value+"-"+DropDownList3.SelectedItem.Value;
int sex=Convert.ToInt32("RadioButtonList1.SelectedItem.Value");
string sql="insert into zhuce(users,pass,sex,birth,mail,wenti,huida) values('"+users+"','"+pass+"','"+sex+"','"+birth+"','"+mail+"','"+wenti+"','"+huida+"')";
SqlConnection con=new SqlConnection("server=.;database=model;uid=sa;pwd=");
con.Open();
SqlCommand cmd=new SqlCommand(sql,con);
int i=cmd.ExecuteNonQuery();
if(i>0)
Response.Redirect("yes.aspx");
else
Response.Redirect("no.aspx");

}

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Redirect("");
}
}
}

#3
vidhl2007-09-06 11:38
大家帮我看看性别的那块是不是对的
我浏览的时候他说是
int sex=Convert.ToInt32("RadioButtonList1.SelectedItem.Value");
的格式不正确
#4
bygg2007-09-06 13:49

int sex=Convert.ToInt32("RadioButtonList1.SelectedItem.Value");
改成:
int sex=Convert.ToInt32(RadioButtonList1.SelectedItem.Value);

还有注意一下你的RadioButtonList1.SelectedItem.Value是个什么类型的值...

#5
vidhl2007-09-06 20:32

还是不可以啊
我开始就是用
int sex=Convert.ToInt32(RadioButtonList1.SelectedItem.Value);
试了的
他说是

输入字符串的格式不正确。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.FormatException: 输入字符串的格式不正确。

源错误:

行 72:             string huida=txtda.Text;
行 73:             string birth=DropDownList1.SelectedItem.Value+"-"+DropDownList2.SelectedItem.Value+"-"+DropDownList3.SelectedItem.Value;
行 74:             int sex=Convert.ToInt32(RadioButtonList1.SelectedItem.Value);
行 75:             string sql="insert into zhuce(users,pass,sex,birth,mail,wenti,huida) values('"+users+"','"+pass+"','"+sex+"','"+birth+"','"+mail+"','"+wenti+"','"+huida+"')";
行 76:             SqlConnection con=new SqlConnection("server=.;database=model;uid=sa;pwd=");


#6
vidhl2007-09-06 20:34
这是我的界面
注 册
帐号:

帐号不能为空

密码:

确认密码:

2次输入的密码不相同

性别:

出生日期:

- -

邮箱:

邮箱不能为空

问题:

为找回密码的重要途径 不能为空

回答:

确 认

#7
vidhl2007-09-06 20:36
.....出错
男女那里是用的RadioButtonList
#8
bygg2007-09-07 09:55
你这里的男和女并不是RadioButtonList啊...
你看一下RadioButtonList1.SelectedItem.Value是个什么值...
#9
BINLEE2007-09-07 14:35
感觉这个男女用RadioButton用就可以了吧,而不用RadioButtonList吧,另外要把两个RadioButton的GroupName属性值都改成同样的这样就不能同时选了...
#10
Liu0182007-09-07 16:10
int sex=Convert.ToInt32("RadioButtonList1.SelectedItem.Value");里面是数值,或许你输入的“男""女"之类的字符吧。
肯定报错了。用radiobutton应该不错
#11
guoxhvip2007-09-09 23:41
int sex=Convert.ToInt32("RadioButtonList1.SelectedItem.Value");这样转换肯定是报错的了 他会把"RadioButtonList1.SelectedItem.Value"看成一个常量字符串来处理
#12
梦灵儿2007-09-10 20:06

你这么做是做不出来的.除非定义一个变量是0或则是1
要不还有一种方法就是把它的文本设置为:男,值设置为1然后取的时候就取值这样就可以取出来了

#13
vidhl2007-09-10 22:44

呵呵 谢谢了
我那个value的植忘了设置
出来了

#14
jclman2007-09-10 22:54

最好用RadioButton,但如果用RadioButtonList1就的遍历一下,直接是无法取值的
[CODE]
private void LinkButton1_Click(object sender, System.EventArgs e)
{
string users=txtzhanghao.Text;
string pass=txtpass.Text;
string mail=txtmail.Text+DropDownList4.SelectedItem.Value;
string wenti=txtwen.Text;
string huida=txtda.Text;
string birth=DropDownList1.SelectedItem.Value+"-"+DropDownList2.SelectedItem.Value+"-"+DropDownList3.SelectedItem.Value;

for(int i=0;i<RadioButtonList1.Items.Count;i++)
{
if(RadioButtonList1.Items[i].Selected)
{
int sex=Convert.ToInt32("RadioButtonList1.Item[i].Value");
}
}


string sql="insert into zhuce(users,pass,sex,birth,mail,wenti,huida) values('"+users+"','"+pass+"','"+sex+"','"+birth+"','"+mail+"','"+wenti+"','"+huida+"')";
SqlConnection con=new SqlConnection("server=.;database=model;uid=sa;pwd=");
con.Open();
SqlCommand cmd=new SqlCommand(sql,con);
int i=cmd.ExecuteNonQuery();
if(i>0)
Response.Redirect("yes.aspx");
else
Response.Redirect("no.aspx");

}

[/CODE]

#15
zw3798688722007-09-11 09:35
男女是个字符串,你要把它强专成int类型,怎么肯能啊
#16
jclman2007-09-11 13:15
那要看你把RadioButtonList1中的Items所对应的VALUE值怎么设置了,你把他设置成1,0不就行了吗.
#17
cyyu_ryh2007-09-11 14:18
把他的VALUE值设成0或1
1