当前位置: 代码迷 >> ASP.NET >> formview中的radiobuttonlist用法,该如何处理
  详细解决方案

formview中的radiobuttonlist用法,该如何处理

热度:2820   发布时间:2013-02-25 00:00:00.0
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>