Get ASP.NET HyperLinkField Text in a GridView

I am trying to get HyperLinkField text in the GridView OnRowDelete event (HyperLinkField text is the main key of the row I want to delete). I understand that you cannot get text using the code that I posted below; it works only for BoundFields (for HyperLinkFields, the string is ""). But I could not find a working answer to get this text. How to get display text from HyperLinkField? (VS2010 with 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 Fill 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");
        }
    }
+5
source share
3 answers

Instead

teamGridView.Rows[e.RowIndex].Cells[0].Text;

Try

( (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

+20
((HyperLink)teamGridView.Rows[e.RowIndex].Cells[0].Controls[0] ).Text 

.

((HyperLink)teamGridView.Rows[e.RowIndex].Cells[0].Controls[0] ).NavigateUrl

.

+1

Another alternative method to get the text value of a hyperlink:

Server.HtmlDecode((teamGridView.Rows[e.RowIndex].Cells[0].Controls[0] as HyperLink).Text)
0
source

All Articles