Gridview中RadioButtonList联动中“未将对象引用设置到对象的实例”

xsllovecys 2012-03-21 09:10:48
建站中,点击更新时怀疑RadioButtonList取值没成功,望各位大大帮忙看看
源错误:


行 147: {
行 148: string id = e.CommandArgument.ToString();
行 149: string audit = auditrbl.SelectedItem.Value.ToString();
行 150: string level = levelrbl.SelectedItem.Value.ToString();
行 151: string str = "UPDATE T_Member SET [F_Audit]=" + audit + ",[F_Level]=" + level + ";";

源文件: j:\Sam‘s Work1\MemberMain.aspx.cs 行: 149

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
MemberMain.GridView_RowCommand(Object sender, GridViewCommandEventArgs e) in j:\Sam‘s Work1\MemberMain.aspx.cs:149
System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs e) +108
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +112
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +166
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

<asp:TemplateField HeaderText="审核状态">
<ItemTemplate>
<asp:Label ID="lblaudit" runat="server" Text='<%# Eval("F_Audit") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="AuditRBL" runat="server" RepeatDirection="Vertical" AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="未通过"></asp:ListItem>
<asp:ListItem Value="1" Text="已通过"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="级别">
<ItemTemplate>
<asp:Label ID="lbllevel" runat="server" Text='<%# Eval("F_Level") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="LevelRBL" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="普通会员"></asp:ListItem>
<asp:ListItem Value="1" Text="ViP"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pagebind();
}
}
private void pagebind()
{
string str = "SELECT * FROM T_Member";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(str, olecon);
da.Fill(ds,"T_Member");
rowCount = ds.Tables["T_Member"].Rows.Count;
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[]{"Member_ID"};
GridView1.DataBind();
olecon.Close();
}
protected void GridView_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
numCount++;
if (e.Row.RowIndex == GridView1.EditIndex)
{
DataRowView rowItem = (DataRowView)e.Row.DataItem;
if (rowItem["Member_ID"] != DBNull.Value)
{

RadioButtonList auditrbl = (RadioButtonList)e.Row.FindControl("AuditRBL");
auditrbl.SelectedIndex = (Convert.ToInt32(rowItem["F_Audit"]));
}
if (rowItem["Member_ID"] != DBNull.Value)
{

RadioButtonList levelrbl = (RadioButtonList)e.Row.FindControl("LevelRBL");
levelrbl.SelectedIndex = (Convert.ToInt32(rowItem["F_Level"]));
}
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
int toLeft = TotalRowCount - numCount;
int numCols = GridView1.Rows[0].Cells.Count;

for (int i = 0; i < toLeft; i++)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
for (int j = 0; j < numCols; j++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
GridView1.Controls[0].Controls.AddAt(numCount + 1 + i, row);
}
}
}
protected void RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
pagebind();
}
protected void GridView_RowCommand(object sender,GridViewCommandEventArgs e)
{
RadioButtonList auditrbl = (RadioButtonList)GridView1.FindControl("AuditRBL");
RadioButtonList levelrbl = (RadioButtonList)GridView1.FindControl("LevelRBL");

if (e.CommandName == "update")
{
string id = e.CommandArgument.ToString();
string audit = auditrbl.SelectedItem.Value.ToString();
string level = levelrbl.SelectedItem.Value.ToString();
string str = "UPDATE T_Member SET [F_Audit]=" + audit + ",[F_Level]=" + level + ";";
olecmd = new OleDbCommand(str, olecon);
olecon.Open();
olecmd.ExecuteNonQuery();
olecon.Close();
}
}
...全文
129 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xsllovecys 2012-03-21
  • 打赏
  • 举报
回复
搞定了,谢谢各位!
jayrao5566 2012-03-21
  • 打赏
  • 举报
回复

foreach (GridViewRow grow in GridView1.Rows)
{
RadioButtonList auditrbl = (RadioButtonList)(grow .FindControl("AuditRBL"));
}
porschev 2012-03-21
  • 打赏
  • 举报
回复


看看MSDN给的RowCommand的例子。。。第一种第二种都可以。。。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
porschev 2012-03-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xsllovecys 的回复:]

引用 2 楼 porschev 的回复:
protected void GridView_RowCommand(object sender,GridViewCommandEventArgs e)
{
RadioButtonList auditrbl = (RadioButtonList)GridView1.FindControl("AuditRBL");
RadioButtonList ……
[/Quote]

给点击的按钮CommandName,CommandArgument

比如:
CommandName="Increase"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"

Command事件中
if(e.CommandName=="Increase")
{
int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = ProductsGridView.Rows[index];

RadioButtonList auditrbl = (RadioButtonList)row.FindControl("AuditRBL");
RadioButtonList levelrbl = (RadioButtonList)row.FindControl("LevelRBL");
}
xsllovecys 2012-03-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 porschev 的回复:]
protected void GridView_RowCommand(object sender,GridViewCommandEventArgs e)
{
RadioButtonList auditrbl = (RadioButtonList)GridView1.FindControl("AuditRBL");
RadioButtonList levelrbl = (Rad……
[/Quote]
e.Row.FindControl..在这个事件里没办法引用
porschev 2012-03-21
  • 打赏
  • 举报
回复

protected void GridView_RowCommand(object sender,GridViewCommandEventArgs e)
{
RadioButtonList auditrbl = (RadioButtonList)GridView1.FindControl("AuditRBL");
RadioButtonList levelrbl = (RadioButtonList)GridView1.FindControl("LevelRBL");

...
}

--------------------------------------->

红色部分改为

RadioButtonList auditrbl = (RadioButtonList)e.Row.FindControl("AuditRBL");
RadioButtonList levelrbl = (RadioButtonList)e.Row.FindControl("LevelRBL");
dalmeeme 2012-03-21
  • 打赏
  • 举报
回复
控件的id不一致,前台文件里定义的是AuditRBL,而代码里确是auditrbl
字母大小写问题。

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧