gpt4 book ai didi

c# - 在 GridView 中获取 ASP.NET HyperLinkField 的文本

转载 作者:太空狗 更新时间:2023-10-29 20:46:58 26 4
gpt4 key购买 nike

我试图在 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

http://gurustop.net

关于c# - 在 GridView 中获取 ASP.NET HyperLinkField 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5176774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com