【问题标题】:Retrieve links stored in DB and show it in GridView检索存储在 DB 中的链接并在 GridView 中显示
【发布时间】:2017-07-20 07:38:28
【问题描述】:

需要一点帮助。 我有一个表,其中一些列中存储了链接。我正在将表格与页面上存在的 GridView 绑定。我需要 GridView 的单元格作为链接。

我已经尝试过这段代码。

<asp:GridView runat="server" ID="gvAutomationTesting" CssClass="table table-bordered table-hover table-responsive" AutoGenerateColumns="false">
                        <Columns>
                           <asp:TemplateField HeaderText="Link 01" SortExpression="Link01">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lb01" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Link01") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

我得到的输出有一个标签但没有 href 属性,因为即使我尝试点击它也不会重定向到所需的页面。

【问题讨论】:

  • 我已经用我写的代码更新了帖子。请检查并提供帮助。

标签: asp.net gridview hyperlink


【解决方案1】:

希望下面的代码 sn-ps 对您有所帮助,使用模板字段您可以将链接按钮分配给网格列


<div>
            <asp:GridView id="mygrid">
                    <asp:TemplateField headertext="number" datatype="string" filterexpression="CUSTOMERNUMBER"
                      sortexpression="CUSTOMERNUMBER"> 
            <itemtemplate>
                <asp:LinkButton ID="lnkbtn" runat="server" Text='<%# DataBinder.Eval(container.DataItem, "CUSTOMERNUMBER") %>' />
                <%--CUSTOMERNUMBER is your DB column name--%>
            </itemtemplate>
            </asp:TemplateField>
            
            <asp:boundfield headertext="Customer" datafield="CUSTOMERNAME" datatype="string" filterexpression="CUSTOMERNAME"
                             sortexpression="CUSTOMERNAME"> 
            </asp:boundfield>
                 
        </asp:GridView>
        </div>

【讨论】:

  • 我尝试了您的代码并更新了帖子。请再检查一次,因为我没有得到想要的输出。
【解决方案2】:

替换

<asp:LinkButton ID="lb01" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Link01") %>' />

<a ID="lb01" runat="server" href='<%# DataBinder.Eval(Container.DataItem, "Link01")' ><%# DataBinder.Eval(Container.DataItem, "Link01") %> </a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多