如何取得DataGrid绑定列和模板列中的值
有的时候想取不是编辑状态的值,这个时候通常会使用模板列中放置TextBox通过e.Item.FindControl取值,有的网友问为什么取不到?这要看你前台的列怎么写的了,下面分别写出四种情况和这四种情况下面的取值方式:
1
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="True">
2
<Columns>
3
<asp:BoundColumn DataField="vName" HeaderText="姓名0"></asp:BoundColumn>
4
<asp:TemplateColumn HeaderText="姓名1">
5
<ItemTemplate>
6
姓名
7
</ItemTemplate>
8
</asp:TemplateColumn>
9
<asp:TemplateColumn HeaderText="姓名2">
10
<ItemTemplate>
11
<%#DataBinder.Eval(Container.DataItem,"vName")%>
12
</ItemTemplate>
13
</asp:TemplateColumn>
14
<asp:TemplateColumn HeaderText="姓名3">
15
<ItemTemplate>
16
<asp:Label ID="name" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"vName")%>'>
17
</asp:Label>
18
</ItemTemplate>
19
</asp:TemplateColumn>
20
<asp:ButtonColumn Text="删除" ButtonType="PushButton" CommandName="del"></asp:ButtonColumn>
21
</Columns>
22
</asp:DataGrid>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
列出了4种大家可能遇到的列:
(1)绑定列
(2)模板列中直接放静态的字符串
(3)模板列中直接放绑定的字符串
(4)模板列中通过Label放绑定的字符串
下面说明在按下了删除按钮以后的四种取值的方式:
1
if(e.CommandName=="del")
2
{
3
Response.Write(DataGrid1.Columns[0].HeaderText+":"+e.Item.Cells[0].Text+"<br>");
4
Response.Write(DataGrid1.Columns[1].HeaderText+":"+e.Item.Cells[1].Text.Trim()+"<br>");
5
Response.Write(DataGrid1.Columns[2].HeaderText+":"+((DataBoundLiteralControl)e.Item.Cells[2].Controls[0]).Text.Trim()+"<br>");
6
Response.Write(DataGrid1.Columns[3].HeaderText+":"+((Label)e.Item.Cells[3].FindControl("name")).Text+"<br>");
7
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
(1)第一种用绑定列的方式可以直接通过Cells[i].Text取
(2)第二种方式也直接可以取,但是讨厌的vs.net总是会在前台把列中的内容换行,所以这里还需要对输出的东西进行去除首尾的空格
(3)第三种方式不能直接通过Cells[i].Text取到值,可以通过上面给出的方法来取,同样需要去除首尾的空格
(4)第四种方式也是最简单的方式,当然你可以直接写e.Item.FindControl("name")