- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图在 GridView 的 OnRowDelete 事件中获取 HyperLinkField 的文本(HyperLinkField 的文本是我希望删除的行的主键)。我知道您无法使用我在下面放置的代码获取文本;它仅适用于 BoundFields(对于 HyperLinkFields,字符串为“”)。但是,我一直无法找到获取此文本的有效答案。如何从 HyperLinkField 获取显示的文本? (带有 ASP.NET 4.0 和 C# 的 VS2010)
感谢阅读!
GridView 设计
<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 填充代码
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 代码
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");
}
}
最佳答案
代替
teamGridView.Rows[e.RowIndex].Cells[0].Text;
尝试
( (HyperLink) teamGridView.Rows[e.RowIndex].Cells[0].Controls[0] ).Text
只是无法避免建议您像这样改变将所有 SQL 直接放入页面的方式。尝试学习 N 层开发。 MSDN 和 asp.NET 网站以及 channel9.msdn 有很好的视频可以作为开始。另外,http://weblogs.asp.net/scottgu
关于c# - 在 GridView 中获取 ASP.NET HyperLinkField 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5176774/
我有以下 HyperLinkField: 我需要超链接文本说点击这里但没有按预期工作。它显示了实际的 SysID。 最佳答案 删除 DataTextField 定义,它应该可以工作: 因
我希望人们单击链接(从 asp:HyperlinkField 生成)并让它调用服务器上的方法,而不是将用户重定向到某个地方。有人知道该怎么做吗? 谢谢, 马特 最佳答案 我会使用 asp:Comman
我有一个 HyperLinkField,我用数据表中的 url 填充它,数据表中的字段称为 EncodedAbsUrl。 但是,我想改为将此链接连接到代码隐藏方法 我现在在做什么 var encode
我有一个绑定(bind)到数据集 (ds) 的 GridView (gv)。 Columns[1] 绑定(bind)到 ds 中名为 orderFilename 的字段; Columns[6] 是一个
我试图在单击 asp gridview 的超链接字段时执行一些 javascript。 javascript 执行得很好,但是当 iframe 在显示之前闪烁时,页面会回发(javascript 只是
我遇到一个问题,Asp.net GridView 中的超链接字段不接受将打开弹出对话框的 Javascript 函数。 我正在使用以下代码片段 但是,当我使用页面网址时,它可以
有人可以向我解释如何在使用 C# 生成的 GridView 代码中将边界域 ( Emp_ID ) 包含到 HyperLinkField ( IDNumber ) 中吗?这样 url 字符串将是 '..
我正在使用 asp.net 并且正在使用 gridview。我将我的 GridView 中的字段之一设置为代表类别名称的 HyperLinkField。我从数据库中提取名称和 ID,但我永远无法确定它
我有一个 ASP.NET Web 窗体应用程序。在我的应用程序中,我有一个运行顺利的GridView。我有几个文本字段,最后一个是 . 现在我想通过放置一个简单链接而不是 hyperlinkfiel
我有一个页面可以按名称搜索产品。在许多页面中,我都使用产品代码。如果用户不知道产品代码,我让他去这个页面,按名字搜索,然后选择一个结果,回到他来自的页面。 在按名称搜索的结果中,我设置了一个将重定向到
我正在使用 HyperLinkField在 gridview 里面,我想链接到另一个 URL + 一个 ID。
我试图在 GridView 的 OnRowDelete 事件中获取 HyperLinkField 的文本(HyperLinkField 的文本是我希望删除的行的主键)。我知道您无法使用我在下面放置的代
我有一个这样映射的路线: routes.MapPageRoute("section-page-id", "{section}/{page}/{id}", "~/{section}/{page}.as
我正在尝试绑定(bind) GridView HyperLinkField,以便将绑定(bind)的列用作 URL 中的参数值。非常标准的东西 - 没什么花哨的,但是当绑定(bind)列包含冒号时绑定
我是一名优秀的程序员,十分优秀!