我正在制作一个网站,其中我使用了网格视图表,并从数据库表类别中检索数据,如下所示:
我想在行上创建链接,如家庭旅游、宗教旅游、冒险旅游、特别活动旅游、国家公园,每当在表中创建新行时,应自动在其上创建超链接.我该怎么做请帮忙..。谢谢
发布于 2017-01-25 14:02:39
谢谢你的帮助,真的很感谢你的帮助@Rana Ali,我有一个更简单的方法,可以通过编写这段代码来完成,我希望这也能帮助到其他人:)
<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]">发布于 2017-01-24 12:14:04
试试这个,我是为你开发的:
.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>.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);
}
}从Page_Load()调用函数:
protected void Page_Load(object sender, EventArgs e)
{
load_gridView();
}https://stackoverflow.com/questions/41826758
复制相似问题