问题
I m making a website in which i have used grid view table and retrieved data from database table category as shown down :
Database table category
I want to create link on rows i.e family tours ,religious tour ,Adventure Tours,Special Event Tours,National Park and whenever new row is created in table hyperlink should be created automatically on it .... How should I do it Please do help .... thank you
I want create link on data that is inside the red mark
回答1:
Try this, I develop for you:
.aspx
<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Page
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("page") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Navigation
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hdn_navigation" Value='<%# Bind("navigation") %>' runat="server" />
<asp:LinkButton ID="LinkButton1" CommandName="navigation" runat="server">Click Me</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind .cs:
private void load_gridView()
{
DataTable dt = new DataTable();
dt.Columns.Add("page");
dt.Columns.Add("navigation");
DataRow dr = dt.NewRow();
dr["page"] = "family tours";
dr["navigation"] = "family_tours.aspx";
dt.Rows.Add(dr);
DataRow dr2 = dt.NewRow();
dr2["page"] = "religious tour";
dr2["navigation"] = "religious_tour.aspx";
dt.Rows.Add(dr2);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
HiddenField hdn_navigation = (HiddenField)row.FindControl("hdn_navigation");
if (e.CommandName.ToString() == "navigation")
{
Response.Redirect(hdn_navigation.Value);
}
}
calling function from Page_Load():
protected void Page_Load(object sender, EventArgs e)
{
load_gridView();
}
回答2:
Thank you for your help really appreciate your help @Rana Ali I have got a more simplified method that can be done by just writing this code I hope this will help others as well :)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" GridLines="None" Height="300px"
Width="100px">
<Columns>
<asp:BoundField DataField="Cat_name" HeaderText="Category"
SortExpression="Cat_name" >
<HeaderStyle Font-Bold="True" Font-Italic="True"
Font-Names="Lucida Calligraphy" Font-Size="X-Large" ForeColor="#3399FF" />
<ItemStyle Font-Bold="True" Font-Italic="True" Font-Size="Small" />
</asp:BoundField>
<asp:HyperLinkField DataTextFormatString="View" DataTextField="Cat_url" DataNavigateUrlFields="Cat_url" HeaderText="" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ToursandTravelsConnectionString %>"
SelectCommand="SELECT [Cat_name], [Cat_url] FROM [category]">
This is and output of the code above :
来源:https://stackoverflow.com/questions/41826758/how-to-create-hyper-links-on-rows-of-gridview-that-is-derived-from-database-in-a