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

标签 c# asp.net

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

相关文章:

c# - 如何检测滚动查看器是否在 winrt 中到达底部

c# - Google 应用程序引擎 ASP.net

asp.net - B2C 和 Azure 应用服务的 CORS 问题

c# - 从 .xls 和 .ods 文件中读取数据

c# - 如何使用 linq dynamic 过滤子集合

c# - .Net 安装项目中的自定义对话框

c# - Swashbuckle 在 ASP.NET Core 中失败并出现 NotSupportedException 异常

c# - Foreach 和 Using 之间的主要区别

c# - 如何自动注入(inject)到所有子对象

asp.net - LastActivityDate 自定义成员(member)提供商