读书人

asp.net 使用radiobuttonlist的困惑 (

发布时间: 2012-01-30 21:15:58 作者: rapoo

asp.net 使用radiobuttonlist的困惑 (使用关键词New创建实例)
折腾了一天了,精疲力尽了已经。贴到这里求救,救命啊!
页面部分:
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:RadioButtonList ID= "RadioButtonList1 " runat= "server ">
</asp:RadioButtonList> </div>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
</form>
</body>
</html> cs部分方案一:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.RadioButtonList1.Items.Add(new ListItem( "test1 ", "test1 "));
this.RadioButtonList1.Items.Add(new ListItem( "test2 ", "test2 "));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(this.RadioButtonList1.SelectedItem.Value);
}cs部分方案二:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.RadioButtonList1.Items.Add(new ListItem( "test1 ", "test1 "));
this.RadioButtonList1.Items.Add(new ListItem( "test2 ", "test2 "));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl = (RadioButtonList)Page.FindControl( "RadioButtonList1 ");
Response.Write(rbl.SelectedItem.Value);
}
尝试阿尝试 ,无尽的尝试,最终还是一个“请使用关键词New创建实例”,已经无语了,高手路过,请抽时间帮帮小弟,现在都共产主义社会了,总该有点共产主义精神吧?


[解决办法]
把你的方案一改了一下
如下代码
CS代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.RadioButtonList1.Items.Add(new ListItem( "test1 ", "test1 "));


this.RadioButtonList1.Items.Add(new ListItem( "test2 ", "test2 "));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(this.RadioButtonList1.SelectedItem.Value);

for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected)
{
Literal1.Text += RadioButtonList1.Items[i].Value;
}


}

}
前台:
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:RadioButtonList ID= "RadioButtonList1 " runat= "server ">
</asp:RadioButtonList> <asp:Literal ID= "Literal1 " runat= "server "> </asp:Literal> </div>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
</form>
</body>
</html>
[解决办法]
因为页面回传以后,你刚才动态添加的两项就没有了
[解决办法]
我好象没怎么改代码,怎么运行正常呢...

//aspx
<%@ Page Language= "C# " AutoEventWireup= "true " CodeBehind= "Default.aspx.cs " Inherits= "WebDemo.Default " %>

<!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:RadioButtonList ID= "RadioButtonList1 " runat= "server ">
</asp:RadioButtonList>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
</form>
</body>
</html>

//aspx.cs
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.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebDemo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.RadioButtonList1.Items.Add(new ListItem( "test1 ", "test1 "));
this.RadioButtonList1.Items.Add(new ListItem( "test2 ", "test2 "));
}
}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(this.RadioButtonList1.SelectedItem.Value);
}
}
}

[解决办法]
肯定不是版本的问题..
[解决办法]
1.
看代码应该是没有问题的,
比较可能出现在,VS自动维护的另一个partial 类,此类声明你拖到的.aspx 上面的服务器控件,有可能此文件已经损坏,
LZ 可以尝试重新建立页面,拷贝 .aspx 和 .aspx.cs 过来试试

2.
其实,你应该告诉我们是编译错误,还是运行时错误,错误时哪一行等信息
便于分析,你省时,我也省力

Try it!

Good Luck!



@amandag(高歌)
因为页面回传以后,你刚才动态添加的两项就没有了

-----------
这里 postback 之后,那两项应该是存在的!

读书人网 >asp.net

热点推荐