获取ASP.NET HyperLinkField字段的文本中的GridView [英] Get text of ASP.NET HyperLinkField in GridView
问题描述
我想获得一个HyperLinkField字段的在GridView的OnRowDelete事件文本(HyperLinkField字段的文字是我想要删除的行的主键)。据我所知,你不能使用code,我在下面放置获取文本;它仅适用于绑定列(对于HyperLinkFields,该字符串为)。不过,我一直无法找到得到这个文本的工作答案。我如何从一个HyperLinkField字段显示的文字? (VS2010瓦特/ ASP.NET 4.0和C#)
感谢您阅读!
GridView控件设计
< ASP:GridView控件ID =teamGridView=服务器的CssClass =GridView的RowStyle-的CssClass =rowStyle
AlternatingRowStyle-的CssClass =altRowStyleHeaderStyle-的CssClass =viewsHeader
OnRowEditing =Team_OnRowEditingOnRowDeleting =Team_OnRowDeletingOnRowUpdating =Team_OnRowUpdating
OnRowCancelingEdit =Team_OnRowCancelingEdit>
<柱体和GT;
< ASP:HyperLinkField字段的HeaderText =队名DataTextField =队名DataNavigateUrlFields =队名
DataNavigateUrlFormatString =Teams.aspx TEAM_NAME = {0}?/>
< ASP:BoundField的的HeaderText =队长的DataField =队长/>
< ASP:CommandField中可见=假的HeaderText =命令ShowEditButton =真的ShowDeleteButton =真/>
< /专栏>
< / ASP:GridView的>
的GridView填充code
使用(SqlConnection的连接=新的SqlConnection(WebConfigurationManager.ConnectionStrings [***。的ConnectionString))
{
//初始化GridView和数据
teamGridView.AutoGenerateColumns = FALSE;
如果(Convert.ToInt32(会话[的UserLevel])大于0)
{ teamGridView.Columns [2]。可见=真实的;
}
SqlDataAdapter的teamDataAdapter =新的SqlDataAdapter();
数据集teamDataSet =新的DataSet();
如果(请求[TEAM_NAME] == NULL)
{
如果请求没有具体的团队//显示强队之列
teamDataAdapter.SelectCommand =新的SqlCommand([队选择],连接);
teamDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
teamDataAdapter.Fill(teamDataSet);
teamGridView.DataSource = teamDataSet;
teamGridView.DataBind();
}
}
的GridView OnRowDeleting code
使用(SqlConnection的连接=新的SqlConnection(WebConfigurationManager.ConnectionStrings [***。的ConnectionString))
{
的SqlCommand teamDeleteCommand =新的SqlCommand([队删除],连接);
teamDeleteCommand.CommandType = CommandType.StoredProcedure;
teamDeleteCommand.Parameters.Add(TeamName,SqlDbType.NVarChar,50);
teamDeleteCommand.Parameters [0]。价值= teamGridView.Rows [e.RowIndex] .Cells [0]。文本;
的Response.Write(teamDeleteCommand.Parameters [0] .value的);
尝试
{
connection.Open();
teamDeleteCommand.ExecuteNonQuery();
}
赶上(SQLEXCEPTION EX)
{
抛出新的异常(团队删除错误);
}
}
而不是
teamGridView.Rows [e.RowIndex] .Cells [0]。文本;
尝试
((超链接)teamGridView.Rows [e.RowIndex] .Cells [0] .Controls [0])。文本
就无法避免,建议您更改您直接把所有的SQL在你的页面这样的方式。尝试扶着约ñ第发展。 MSDN和asp.NET网站和channel9.msdn具有良好的视频开始。此外, http://weblogs.asp.net/scottgu
块引用>I'm trying to get the text of a HyperLinkField in a GridView's OnRowDelete event (the HyperLinkField's text is the primary key of the row I wish to delete). I understand that you can't get the text using the code I've placed below; it only works for BoundFields (for HyperLinkFields, the string is ""). But, I've been unable to find a working answer for getting this text. How do I get the displayed text from a HyperLinkField? (VS2010 w/ ASP.NET 4.0 and C#)
Thanks for reading!
GridView Design
<asp:GridView ID="teamGridView" runat="server" CssClass="gridView" RowStyle-CssClass="rowStyle" AlternatingRowStyle-CssClass="altRowStyle" HeaderStyle-CssClass="viewsHeader" OnRowEditing="Team_OnRowEditing" OnRowDeleting="Team_OnRowDeleting" OnRowUpdating="Team_OnRowUpdating" OnRowCancelingEdit="Team_OnRowCancelingEdit"> <Columns> <asp:HyperLinkField HeaderText="Team Name" DataTextField="Team Name" DataNavigateUrlFields="Team Name" DataNavigateUrlFormatString="Teams.aspx?Team_Name={0}" /> <asp:BoundField HeaderText="Team Captain" DataField="Team Captains" /> <asp:CommandField Visible="false" HeaderText="Commands" ShowEditButton="true" ShowDeleteButton="true" /> </Columns> </asp:GridView>
GridView Populating Code
using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["***"].ConnectionString)) { // Initialize GridView and data teamGridView.AutoGenerateColumns = false; if (Convert.ToInt32(Session["UserLevel"]) > 0) { teamGridView.Columns[2].Visible = true; } SqlDataAdapter teamDataAdapter = new SqlDataAdapter(); DataSet teamDataSet = new DataSet(); if (Request["Team_Name"] == null) { // Show the list of teams if no specific team is requested teamDataAdapter.SelectCommand = new SqlCommand("[Team Select]", connection); teamDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; teamDataAdapter.Fill(teamDataSet); teamGridView.DataSource = teamDataSet; teamGridView.DataBind(); } }
GridView OnRowDeleting Code
using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["***"].ConnectionString)) { SqlCommand teamDeleteCommand = new SqlCommand("[Team Delete]", connection); teamDeleteCommand.CommandType = CommandType.StoredProcedure; teamDeleteCommand.Parameters.Add("TeamName", SqlDbType.NVarChar, 50); teamDeleteCommand.Parameters[0].Value = teamGridView.Rows[e.RowIndex].Cells[0].Text; Response.Write(teamDeleteCommand.Parameters[0].Value); try { connection.Open(); teamDeleteCommand.ExecuteNonQuery(); } catch (SqlException ex) { throw new Exception("Team Deletion Error"); } }
解决方案Instead of
teamGridView.Rows[e.RowIndex].Cells[0].Text;
Try
( (HyperLink) teamGridView.Rows[e.RowIndex].Cells[0].Controls[0] ).Text
Just can't avoid advising you to change the way you put all SQL directly in your page like this. Try leaning about N tier development. MSDN and asp.NET website and channel9.msdn have good videos to start with. Also, http://weblogs.asp.net/scottgu
这篇关于获取ASP.NET HyperLinkField字段的文本中的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!