在gridview的template中放入textbox读值的问题

carol2010cn 2008-05-19 05:31:34
在gridview的template中放入textbox,现在想把textbox.text的值读出来。
我用了gridview.row().cells().controls()
后面就不会写了。
...全文
644 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
amandag 2008-05-20
  • 打赏
  • 举报
回复
无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.DropDownList
==
这里已经说得很清楚了,你找到的不是DropDownList,而是个LiteralControl

强烈建议你使用FindControl方法根据控件的id找到控件

Dim mytxt As DropDownList
Dim mytxt2 As DropDownList

mytxt = CType(Me.GridView1.Rows(i).FindControl("对应的DropDownList的ID 可能是DropDownList1"), DropDownList)
mytxt2 = CType(Me.GridView1.Rows(i).FindControl("对应的DropDownList的ID 可能是DropDownList2"), DropDownList)
con3(k) = mytxt.Text + "到" + mytxt2.Text
k = k + 1
carol2010cn 2008-05-20
  • 打赏
  • 举报
回复
无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.DropDownList”。
报了这个错误。
我的源码: Dim mytxt As DropDownList
Dim mytxt2 As DropDownList

mytxt = CType(Me.GridView1.Rows(i).Cells(j).Controls(0), DropDownList)
mytxt2 = CType(Me.GridView1.Rows(i).Cells(j).Controls(1), DropDownList)
con3(k) = mytxt.Text + "到" + mytxt2.Text
k = k + 1
http://hi.baidu.com/aq120/blog/item/3df341ad23d0820a4b36d62f.html
这里写了个asp的这个问题,说是空格的问题,我现在也不知道怎么改了。
amandag 2008-05-20
  • 打赏
  • 举报
回复
Dim l As LinkButton = CType(e.Row.Cells[0].Controls[0], LinkButton)
amandag 2008-05-20
  • 打赏
  • 举报
回复
但我还是想知道那个的方法
==
那个方法?

mytxt = CType(Me.GridView1.Rows(i).Cells(j).Controls(0), DropDownList) ?
这个方法不好,因为你不能肯定控件集合的顺序
carol2010cn 2008-05-20
  • 打赏
  • 举报
回复
(LinkButton)e.Row.Cells[0].Controls[0];
这种写法在vb.net里不对阿。
应该是强制转换的,怎么转呢。
carol2010cn 2008-05-20
  • 打赏
  • 举报
回复
谢谢大家了,我这个问题基本解决了,最后想问问,用gridview.Rows[0].Cells[2].Controls[0])).Text.Trim();//三种
这个的时候,我在control后面打.text可是没有,这个control的方法应该怎么读出text呢
carol2010cn 2008-05-20
  • 打赏
  • 举报
回复
恩,用findcontrol的方法OK了,谢谢您。但我还是想知道那个的方法,不管怎样,分数还是给您吧。
希望知道答案的继续跟下帖,也方便以后的小菜鸟(还有我哦)学习。
amandag 2008-05-19
  • 打赏
  • 举报
回复
一楼的我写在一个按钮的点击事件里了
==
按钮是在GridView里的还是在GridView外的,

如果是在GridView里面你这个按钮一般在GridView的RowCommand事件中处理(除非你要做的操作是删除和操作)

如果是在GridView外面的,那你打算取那一行的记录了,凭什么取这一行?是因为前面有复选框么?

所以楼主麻烦你说清楚,如果是取所有行的

TextBox t;
foreach (GridViewRow row in GridView1.Rows)//GridView1时你的GridView的id
{
t = row.FindControl("TextBox1") as TextBox;//这里TextBox1是你的模版列内部的文本框id
if (t != null)
{
Response.Write(t.Text);
}
}


carol2010cn 2008-05-19
  • 打赏
  • 举报
回复
一楼的我写在一个按钮的点击事件里了。
二楼的请写完整点,这个问题真的不会。
三楼的不会是,msdn里copy的吧。我一运行就说textbox1.text错误,呵呵。我没有msdn,所以也说清楚点吧。
蝶恋花雨 2008-05-19
  • 打赏
  • 举报
回复

protected void grSort_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
((TextBox)e.Row.FindControl("TextBox1")).Text= "";//一种
TextBox txtName=e.Row.Cells[5].FindControl("TextBox1") as TextBox ;
txtName.Text="";第二种
}

}
gridview.Rows[0].Cells[2].Controls[0])).Text.Trim();//三种
kenwu300692 2008-05-19
  • 打赏
  • 举报
回复

dim Textbox1 as TextBox=Gridview1.Findcontrol("Textbox1")
TextBox1.text
S170393163 2008-05-19
  • 打赏
  • 举报
回复
((TextBox )gridview.Rows [].FindControl (textbox的id)).Text
amandag 2008-05-19
  • 打赏
  • 举报
回复
现在想把textbox.text的值读出来
======
要看你打算在什么对象的什么事件中取值,方法各不相同

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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