【问题标题】:How to create hyper links on rows of gridview that is derived from database in asp.net?如何在从 asp.net 中的数据库派生的 gridview 行上创建超链接?
【发布时间】:2017-01-25 22:02:39
【问题描述】:

我正在制作一个网站,其中我使用了网格视图表并从数据库表 category 中检索数据,如下所示:
Database table category

我想在行上创建链接,即家庭旅游、宗教旅游、冒险之旅、特殊活动旅游、国家公园,并且每当在表中创建新行时,超链接应该自动在其上创建......我应该怎么做请帮忙....谢谢
I want create link on data that is inside the red mark

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

试试这个,我为你开发:

.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();
        }

【讨论】:

    【解决方案2】:

    感谢您的帮助,非常感谢您的帮助@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]">
    

    This is and output of the code above :

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      相关资源
      最近更新 更多