读书人

formview中的radiobuttonlist用法,该如

发布时间: 2012-03-22 17:43:57 作者: rapoo

formview中的radiobuttonlist用法
[code=C#][/code]
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
RadioButtonList rb = (RadioButtonList)FormView1.FindControl("S_answer");
string s = rb.SelectedValue.ToString();
Label1.Text = s.ToString();
}


怎么显示的是“未将对象引用设置到对象的实例。”

急急!!问题解决马上结贴!非诚勿扰、、、

[解决办法]

C# code
RadioButtonList rb = (RadioButtonList)FormView1.FindControl("S_answer");//这块你确定你找到了这个东西了 ?//使用的时候也不判断一下是不是null?//调试一下  看看你这块是不是得到这个东西了  要是没有找到  重新看看你找哪个控件的代码string s = rb.SelectedValue.ToString();
[解决办法]
RadioButtonList 用(SelectedValue.ToString();)这个貌似不对吧 ~~~


我记得应该是 :SelectedItem.Text;




[解决办法]
你的
RadioButtonList
放在了
<EditItemTemplate></EditItemTemplate>
里面了吗

参见
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.formview.itemupdating.aspx
[解决办法]
探讨

引用:

C# code

RadioButtonList rb = (RadioButtonList)FormView1.FindControl("S_answer");
//这块你确定你找到了这个东西了 ?
//使用的时候也不判断一下是不是null?
//调试一下 看看你这块是不是得到这个东西了 要是没有找到 重新看看你找哪个控件的代……

[解决办法]
你应该是写错了,下面的测试是完全没问题的

C# code
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)  {    RadioButtonList RadioButtonList1 = FormView1.FindControl("RadioButtonList1") as RadioButtonList;    Response.Write(RadioButtonList1.SelectedValue);    //为了测试。不继续执行    e.Cancel = true;  }</script><html xmlns="http://www.w3.org/1999/xhtml"><head>  <title></title></head><body>  <form runat="server">  <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DefaultMode="Edit"    OnItemUpdating="FormView1_ItemUpdating">    <EditItemTemplate>      <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">        <asp:ListItem Value="A">A</asp:ListItem>        <asp:ListItem Value="B">B</asp:ListItem>        <asp:ListItem Value="C">C</asp:ListItem>        <asp:ListItem Value="D">D</asp:ListItem>      </asp:RadioButtonList>      <asp:Button ID="x" runat="server" CommandName="Update" Text="Update" />    </EditItemTemplate>  </asp:FormView>  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Provider=SQLOLEDB;Data Source=.;Persist Security Info=True;Password=xx;User ID=sa;Initial Catalog=xxxxx"    ProviderName="System.Data.OleDb" SelectCommand="SELECT Title FROM [Article]"    UpdateCommand="Update xxx"></asp:SqlDataSource>  </form></body></html> 

读书人网 >asp.net

热点推荐