datagrid
有的时候想取不是编辑状态的值,这个时候通常不会使用模板列中放置TextBox通过e.Item.FindControl取值,有的网友问为什么取不到?这要看你前台的列怎么写的了,下面分别写出四种情况和这四种情况下面的取值方式:

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="True">

<Columns>

<asp:BoundColumn DataField="vName" HeaderText="姓名0"></asp:BoundColumn>

<asp:TemplateColumn HeaderText="姓名1">

<ItemTemplate>

姓名

</ItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="姓名2">

<ItemTemplate>


<%

#DataBinder.Eval(Container.DataItem,"vName")%>

</ItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="姓名3">

<ItemTemplate>

<asp:Label ID="name" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"vName")%>'>

</asp:Label>

</ItemTemplate>

</asp:TemplateColumn>

<asp:ButtonColumn Text="删除" ButtonType="PushButton" CommandName="del"></asp:ButtonColumn>

</Columns>

</asp:DataGrid>
列出了4种大家可能遇到的列:
(1)绑定列
(2)模板列中直接放静态的字符串
(3)模板列中直接放绑定的字符串
(4)模板列中通过Label放绑定的字符串
下面说明在按下了删除按钮以后的四种取值的方式:

if(e.CommandName=="del")


{

Response.Write(DataGrid1.Columns[0].HeaderText+":"+e.Item.Cells[0].Text+"
");

Response.Write(DataGrid1.Columns[1].HeaderText+":"+e.Item.Cells[1].Text.Trim()+"
");

Response.Write(DataGrid1.Columns[2].HeaderText+":"+((DataBoundLiteralControl)e.Item.Cells[2].Controls[0]).Text.Trim()+"
");

Response.Write(DataGrid1.Columns[3].HeaderText+":"+((Label)e.Item.Cells[3].FindControl("name")).Text+"
");

}
(1)第一种用绑定列的方式可以直接通过Cells[i].Text取
(2)第二种方式也直接可以取,但是讨厌的vs.net总是会在前台把列中的内容换行,所以这里还需要对输出的东西进行去除首尾的空格
(3)第三种方式是网友问的最多的,不能直接通过Cells[i].Text取到值,可以通过上面给出的方法来取,同样需要去除首尾的空格
(4)第四种方式也是最简单的方式,当然你可以直接写e.Item.FindControl("name")