gridview中更新时出现异常,帮忙看看!急~~~~~~~~~~

Abcsj 2008-06-18 11:36:01
gridview中更新时出现异常

无法将类型为“System.Web.UI.WebControls.DataControlLinkButton”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。


源错误:


行 96: void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
行 97: {
行 98: string aa = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
行 99:
行 100: GridView1.EditIndex = -1;
SetBind();
}

源文件: d:\web\Default355.aspx.cs 行: 98
...全文
779 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinianjun 2010-11-20
  • 打赏
  • 举报
回复
我也遇到这样的问题到底怎么办啊,高手指点一下啊
mengxj85 2008-06-18
  • 打赏
  • 举报
回复
string aa = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
列用错了,应该是用
<asp:BoundField ></asp:BoundField>
这个数据才是Textbox类型的
amandag 2008-06-18
  • 打赏
  • 举报
回复
1. 没有贴你的页面代码
2. 使用TemplateField而不是BoundField,这样可以使用FindControl精确地找到你的控件
3.
已经有了
<asp:CommandField ShowEditButton="True" HeaderText="编辑" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
下面四句都不需要
GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
4. ASP.NET可以使用内置的SqlDataSource之类的控件完成此操作,无须手写代码

Abcsj 2008-06-18
  • 打赏
  • 举报
回复
就相当与使用到gridview提供的编辑\删除功能,
但出现上面的错位,各位帮忙看看
Abcsj 2008-06-18
  • 打赏
  • 举报
回复
先谢谢各位!我是在源代码里写:
<asp:CommandField ShowEditButton="True" HeaderText="编辑" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />


部分代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.SetBind();
}
GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);

}


void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();

//authorsbll.Delete(id);//删除,函数自己写

SetBind();//重新绑定数据源
}

void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string aa = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());

GridView1.EditIndex = -1;
SetBind();
}

hubblebubblepig 2008-06-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Abcsj 的回复:]
知道是那个错误,但不知道该怎么改,指点指点下
[/Quote]
那你就要数数你要找的那个TextBox是第几个Control了吖...
或者 你找到的那个单元格子是不是你以为的那个格子
asge8900 2008-06-18
  • 打赏
  • 举报
回复
对上面的说的队,((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])的问题,你可以用<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:TextBox ID="TextBox " runat="server" CausesValidation="True" CommandName="Update" Text="编辑">
</asp:TextBox >
</ItemTemplate>
</asp:TemplateField>
代替就不会出现那个问题了
freexiaoyu 2008-06-18
  • 打赏
  • 举报
回复
这是是LinkButton按钮列,它怎么可能转成TextBox
Abcsj 2008-06-18
  • 打赏
  • 举报
回复
知道是那个错误,但不知道该怎么改,指点指点下
sytu_hzj 2008-06-18
  • 打赏
  • 举报
回复
不懂,帮忙顶顶,现在连顶贴的句子都不能太短了,晕
hubblebubblepig 2008-06-18
  • 打赏
  • 举报
回复
多明显的错误啊 都给你说得清清楚楚了的
无法将类型为“System.Web.UI.WebControls.DataControlLinkButton”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”
你代码里面
GridView1.Rows[e.RowIndex].Cells[0].Controls[0]找到的是一个DataControlLinkButton而不是TextBox
Abcsj 2008-06-18
  • 打赏
  • 举报
回复
谢谢关注,去掉后,出现这样的异常:
此 OracleParameterCollection 的 Count=5 的索引 -1 无效
levenwood 2008-06-18
  • 打赏
  • 举报
回复
or_comm.Parameters["NAME "].Value = st_st_colvalue;
把冒号去掉试试
Abcsj 2008-06-18
  • 打赏
  • 举报
回复
各位,帮帮忙看看哦,就是对GridView进行修改\删除功能
小弟实在是没法了
Abcsj 2008-06-18
  • 打赏
  • 举报
回复
各位帮帮忙.弄了快一个下午了都没弄好.我的代码如下:

public partial class Default355 : System.Web.UI.Page
{
private string sConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.SetBind();
}
GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);

}

private void SetBind()
{
DataSet ds=new DataSet();
string sqlstring="select * from RISKROOTANALYSIS";
using (OracleConnection conn = new OracleConnection("Data Source=ppp.gnpjvc.cgnpc.com.cn;User ID=maximo;Password=xdf002"))
{
OracleDataAdapter da = new OracleDataAdapter(sqlstring, conn);
da.Fill(ds);
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
/// 功 能:GridView删除事件
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();
.......
.......
SetBind();//重新绑定数据源
}
/// 功 能:GridView编辑事件
void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
SetBind();
}

/// 功 能:GridView取消事件
void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
SetBind();
}

/// 功 能:GridView更新事件
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//string aa = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim());
GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
string st_orastr = "UPDATE RISKROOTANALYSIS SET UNITID = :UNITID, REMARK = :REMARK, WORK = :WORK, NAME = :NAME WHERE ANALYSISNODEID = :ANALYSISNODEID";
OracleConnection conn = new OracleConnection(sConnectionString);
OracleCommand or_comm = new OracleCommand(st_orastr, conn);
try
{
//添加各种数据参数
or_comm.Parameters.Add(new OracleParameter(":UNITID ",OracleType.NVarChar, 200));
or_comm.Parameters.Add(new OracleParameter(":REMARK ", OracleType.NVarChar, 200));
or_comm.Parameters.Add(new OracleParameter(":WORK ", OracleType.NVarChar, 200));
or_comm.Parameters.Add(new OracleParameter(":NAME ", OracleType.NVarChar, 200));
or_comm.Parameters.Add(new OracleParameter(":ANALYSISNODEID ", OracleType.NVarChar, 200));

string st_st_colvalue = ((TextBox)gvr.Cells[2].Controls[0]).Text;
or_comm.Parameters[":UNITID "].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)gvr.Cells[4].Controls[0]).Text;
or_comm.Parameters[":REMARK "].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)gvr.Cells[5].Controls[0]).Text;
or_comm.Parameters[":WORK "].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)gvr.Cells[6].Controls[0]).Text;
or_comm.Parameters[":NAME "].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)gvr.Cells[3].Controls[0]).Text;
or_comm.Parameters[":ANALYSISNODEID "].Value = st_st_colvalue;
//or_comm.Parameters[":ANALYSISNODEID "].Value = this.GridView1.DataKeys[e.RowIndex].Value;
//打开连接
conn.Open();
or_comm.ExecuteNonQuery();
}
catch
{
}
//GridView1.UpdateRow((GridView1.Rows[e.RowIndex].Cells[2].Controls[0]), true);
GridView1.EditIndex = -1;
SetBind();
}
}



上面的主键是ANALYSISNODEID ,但是也可以改的.看看是前面的sql语句写错没
把这个//or_comm.Parameters[":ANALYSISNODEID "].Value = this.GridView1.DataKeys[e.RowIndex].Value;注释掉,调试时候出现"ORA-01036: 非法的变量名/编号"

62,254

社区成员

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

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

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

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