在 RowDataBound 事件上从网格视图中获取单元格的值 [英] Getting value from a cell from a gridview on RowDataBound event

查看:29
本文介绍了在 RowDataBound 事件上从网格视图中获取单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string percentage = e.Row.Cells[7].Text;

我正在尝试用我的 GridView 做一些动态的事情,所以我已经将一些代码连接到 RowDataBound 事件.我试图从一个特定的单元格中获取值,它是一个 TemplateField.但是上面的代码似乎总是返回一个空字符串.

I am trying to do some dynamic stuff with my GridView, so I have wired up some code to the RowDataBound event. I am trying to get the value from a particular cell, which is a TemplateField. But the code above always seems to be returning an empty string.

有什么想法吗?

澄清一下,这里有一个冒犯的单元格:

To clarify, here is a bit the offending cell:

<asp:TemplateField HeaderText="# Percentage click throughs">
<ItemTemplate>
    <%# AddPercentClickThroughs((int)Eval("EmailSummary.pLinksClicked"), (int)Eval("NumberOfSends")) %>
</ItemTemplate>
</asp:TemplateField>

在相关说明中,有没有人知道是否有更好的方法来选择行中的单元格.把 cell[1] 放进去很麻烦.我不能做cell["mycellname"],所以如果我决定改变我的单元格的顺序,错误就不会出现?

On a related note, does anyone know if there is a better way of selecting the cell in the row. It sucks putting in cell[1]. Couldn't I do cell["mycellname"], so if I decide to change the order of my cells, bugs wont appear?

推荐答案

首先你需要将你的代码包装在一个 LabelLiteral 控件中,以便你可以引用它适当地.发生的事情是系统无法跟踪它,因为没有与文本相关的控件.将其内容添加到视图状态是控件的责任.

First you need to wrap your code in a Label or Literal control so that you can reference it properly. What's happening is that there's no way for the system to keep track of it, because there's no control associated with the text. It's the control's responsibility to add its contents to viewstate.

你需要使用 gridView.FindControl("controlName");获得该行的控制权.从那里你可以得到它的属性,包括 Text.

You need to use gridView.FindControl("controlName"); to get the control in the row. From there you can get at its properties including Text.

您还可以获取相关行的 DataItem 属性并将其转换为适当的类型并直接提取信息.

You can also get at the DataItem property of the Row in question and cast it to the appropriate type and extract the information directly.

这篇关于在 RowDataBound 事件上从网格视图中获取单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆