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");
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…