简体   繁体   中英

Get text of ASP.NET HyperLinkField in GridView

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

http://gurustop.net

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

will give the text or displayed value of the field. Alternatively we can try

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

for getting the target link.

另一种获取超链接文本值的替代方法:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM