private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Response.Write(RadioButtonList1.SelectedItem.Text+"<br>"+RadioButtonList1.SelectedItem.Value);
}寫再這個事件中
{
Response.Write(RadioButtonList1.SelectedItem.Text+"<br>"+RadioButtonList1.SelectedItem.Value);
}寫再這個事件中
AutoPostBack="True"
在事件中调用变量RadioButtonList1.SelectedItem.Text
如果改变所选值时触发事件则AutoPostBack="True"
都很简单,看你要什么功能,什么时候获取值
{
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataSet dtSet;
OleDbConnection mySQLConnection = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("database\\dx.mdb"));
OleDbDataAdapter dtCmd = new OleDbDataAdapter("select user_info.user_xm from dxhy_info,user_info where dxhy_info.user_id2 = user_info.id and dxhy_info.user_id1=1",mySQLConnection); dtSet = new DataSet();
dtCmd.Fill(dtSet);
RadioButtonList1.DataSource = dtSet.Tables[0].DefaultView;
RadioButtonList1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{ if (RadioButtonList1.SelectedIndex > -1)
{
Label1.Text = "你选择了:" +RadioButtonList1.SelectedItem.Text;
}
}
}写成代码分离的形式后他的提示是
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 61:
行 62: private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
行 63: {
行 64:
行 65: if (RadioButtonList1.SelectedIndex > -1)
只要解决了分不够可以再加!!!
我就在前台放了个RadioButtonList1还有个Label1
后台就是如上面所示,
按道理講沒錯
private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (RadioButtonList1.SelectedIndex > -1)
{
Label1.Text = "你选择了:" +RadioButtonList1.SelectedItem.Value;
}
}
因为RadioButtonList1上面显示数据未绑定我是这样绑定的不知道对不对
DataSet dtSet;
OleDbConnection mySQLConnection = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("database\\dx.mdb"));
OleDbDataAdapter dtCmd = new OleDbDataAdapter("select user_info.user_xm from dxhy_info,user_info where dxhy_info.user_id2 = user_info.id and dxhy_info.user_id1=1",mySQLConnection); dtSet = new DataSet();
dtCmd.Fill(dtSet);
RadioButtonList1.DataSource = dtSet.Tables[0].DefaultView;
RadioButtonList1.DataBind();在属性上设置了DataValueField="user_xm" AutoPostBack="True" DataTextField="user_xm" DataTextFormatString="user_xm"user_xm是数据表中的一个属性
那么我查询后user_xm这个属性的值却又可以显示出来。
这是为什么?
不因该啊~~
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
OleDbConnection mySQLConnection = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("database\\dx.mdb"));
OleDbDataAdapter dtCmd = new OleDbDataAdapter("select user_info.user_xm from dxhy_info,user_info where dxhy_info.user_id2 = user_info.id and dxhy_info.user_id1=1",mySQLConnection); dtSet = new DataSet();
dtCmd.Fill(dtSet); RadioButtonList1.DataTextField="user_xm"; RadioButtonList1.DataValueField="user_xm";
RadioButtonList1.DataSource = dtSet.Tables[0].DefaultView;
RadioButtonList1.DataBind();}
}
<asp:radiobuttonlist id="RadioButtonList1" runat="server" AutoPostBack="True"></asp:radiobuttonlist>
user_xm必須是綁定的表的字段
<asp:radiobuttonlist id="RadioButtonList1" style="Z-INDEX: 102; LEFT: 248px; POSITION: absolute; TOP: 96px"runat="server" DataValueField="user_xm"
AutoPostBack="True"
DataTextField="user_xm"
DataTextFormatString="user_xm"
DataMember="user_xm"></asp:radiobuttonlist>
{
if(!IsPostBack)
{
。
RadioButtonList1.DataTextField="user_xm"; RadioButtonList1.DataValueField="user_xm";
RadioButtonList1.DataSource = dtSet.Tables[0].DefaultView;
RadioButtonList1.DataBind();
ViewState["SelItem"]=RadioButtonList1.selectedItem.Value;}
private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (RadioButtonList1.SelectedIndex > -1)
{
ViewState["SelItem"]=ViewState["SelItem"]=RadioButtonList1.selectedItem.Value;
Label1.Text = "你选择了:" +Convert.ToString(ViewState["SelItem"]);
}
}}
还是一样能显示我查询出来的结果可就是点击不能取值,并且显示数据未绑定。
{
Label1.Text = "你选择了:" + RadioButtonList1.SelectedItem.Text;
}
改后一点击RadioButtonList1就出错
提示如下:
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 73: private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
行 74: {
行 75: Label1.Text = "你选择了:" + RadioButtonList1.SelectedItem.Text;
行 76: }
行 77:
源文件: c:\inetpub\wwwroot\webapplication5\txb.aspx.cs 行: 75 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
WebApplication5.txb.RadioButtonList1_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication5\txb.aspx.cs:75
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
System.Web.UI.WebControls.RadioButtonList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
RadioButtonList1.DataTextField = "id"
RadioButtonList1.DataValueField = "id"
RadioButtonList1.DataBind()这是我的一个源代码你可以相应改改里面的sql语句和绑定字段再试试。以下是后台代码的。
Imports System.Data.OleDb
Public Class test
Inherits System.Web.UI.Page
Protected WithEvents Label1 As System.Web.UI.WebControls.Label Dim mydb As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.mappath("data/db1.mdb")
Dim myConn As OleDb.OleDbConnection = New OleDb.OleDbConnection(mydb)#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub
Protected WithEvents RadioButtonList1 As System.Web.UI.WebControls.RadioButtonList '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
'初使调用数据绑定过程
If Not IsPostBack Then
Call DataGridBind()
End If
End Sub Sub DataGridBind()
'通过DATASET与DATAGRID控件数据绑定
Dim mySql As String = "SELECT * FROM user_info order by id desc"
Dim myDa As OleDbDataAdapter = New OleDbDataAdapter(mySql, myConn)
Dim myDataSet As DataSet = New DataSet myDa.Fill(myDataSet, "user") RadioButtonList1.DataSource = myDataSet.Tables("user").DefaultView
RadioButtonList1.DataTextField = "id"
RadioButtonList1.DataValueField = "id"
RadioButtonList1.DataBind()
End Sub Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
Label1.Text = RadioButtonList1.SelectedItem.Value End Sub
End Class以下是前台设计中的一些属性:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="test.aspx.vb" Inherits="haha.test"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:RadioButtonList id="RadioButtonList1" style="Z-INDEX: 101; LEFT: 320px; POSITION: absolute; TOP: 160px"
runat="server" AutoPostBack="True"></asp:RadioButtonList>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 328px; POSITION: absolute; TOP: 112px" runat="server">Label</asp:Label>
</form>
</body>
</HTML>
RadioButtonList1.DataValueField = "id"注:其中datatextfield是外面显示的文本值,而datavaluefield是实际值。
原来是没有加IsPostBack