从模板字段 gridview 获取值 - C#
我正在使用 gridview 来显示从数据库查询的表...
在此 gridview 中我还添加了一个按钮字段和 3 个模板字段...
当我按下按钮字段时,会获取数据库中的数据,但是 从模板字段插入的手动数据不是......似乎获取了空值。
从 xml 文件中,我触发以下事件:
OnRowCommand="GridView1_SelectedIndexChanged1"
并有以下方法来捕获该事件:
protected void GridView1_SelectedIndexChanged1(object sender, GridViewCommandEventArgs e)
{
int x = Convert.ToInt32(e.CommandArgument);
GridView1.SelectRow(x);
GridViewRow row = GridView1.Rows[x];
Response.Write(row.Cells[0].Text + "\t");
Response.Write(row.Cells[1].Text + "\t");
Response.Write(row.Cells[2].Text + "\t");
Response.Write(row.Cells[3].Text + "\t");
Response.Write(row.Cells[4].Text + "\t");
Response.Write(row.Cells[5].Text + "\t");
Response.Write(row.Cells[6].Text + "\t");
Response.Write(row.Cells[7].Text + "\t");
Response.Write(row.Cells[8].Text + "\t");
}
关于如何从该模板字段获取值的任何想法?我是否需要捕获另一个事件而不是 GridViewCommandEventArgs?如果是这样,我应该从 xml 部分抛出什么事件?
谢谢,
I am using a gridview to display a table queried from a database...
In this gridview I also added a buttonfield, and 3 template fields...
When i press the buttonfield the data from the database is acquired, but the
manual data inserted from the template fields are not.. It seems that null values are acquired.
From the xml file i am firing the following event:
OnRowCommand="GridView1_SelectedIndexChanged1"
and have the following method to catch that event:
protected void GridView1_SelectedIndexChanged1(object sender, GridViewCommandEventArgs e)
{
int x = Convert.ToInt32(e.CommandArgument);
GridView1.SelectRow(x);
GridViewRow row = GridView1.Rows[x];
Response.Write(row.Cells[0].Text + "\t");
Response.Write(row.Cells[1].Text + "\t");
Response.Write(row.Cells[2].Text + "\t");
Response.Write(row.Cells[3].Text + "\t");
Response.Write(row.Cells[4].Text + "\t");
Response.Write(row.Cells[5].Text + "\t");
Response.Write(row.Cells[6].Text + "\t");
Response.Write(row.Cells[7].Text + "\t");
Response.Write(row.Cells[8].Text + "\t");
}
Any ideas on how i can get the values from that template field? do i need to catch another event rather than GridViewCommandEventArgs? If so what event should i throw from the xml part?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以找到添加到该行的控件,然后使用 text 属性获取它的值。)\\将其转换为 textBox,然后使用 .Text 获取它的值。
类似于 row.FindControl(
GridViewRow row = GridView1.Rows[x];
Response.Write((TextBox)row.FindControl('txtbox1')).Text;
You can find the control u added on to the row and then use the text property to get the value of it.
Something similar to
row.FindControl(<urIdgoeshere>)\\cast it to textBox
and then use .Text to get the value of it .GridViewRow row = GridView1.Rows[x];
Response.Write((TextBox)row.FindControl('txtbox1')).Text;
我正在与同样的事情作斗争,我曾经将 Cells[] 代码与 asp BoundField 一起使用。
现在我必须切换到模板字段,因为我需要页脚中的按钮等。
我正在拔头发,我使用了 Ashley 建议的相同代码,然后得到 System.NullReferenceException: 对象引用未设置到对象的实例。
我在行命令事件中执行此操作。
三天以来一直这样:(
编辑:
我的问题的快速解决方案:我已经在页脚上创建了给定字段的 ID,但我没有给我的 ItemTemplate 提供 ID,即我在那里唯一的东西是 <%# Eval("Description")%> ;
如果不在代码后面提供某种 ID,则似乎无法选择此数据。我很愚蠢,后来没有注意到这个简单的事实。
I am fighting with the same, I used to use the Cells[] code with asp BoundField.
Now i had to switch to template fields as I need buttons etc in the footer.
I am pulling my hair out, I use the same code as suggested by Ashley, and I get System.NullReferenceException: Object reference not set to an instance of an object.
I am doing this within the row command event.
Been this way since 3 days :(
Edit:
Quick Solution to my issue: I've created the ID given Field on the footer, and I had not given and ID to my ItemTemplate, i.e the only thing I had in there was <%# Eval("Description")%>
There looks like to be NO WAY to select this data without giving it some kind of ID in code behind. I was stupid and had not noticed this simple fact later on.