添加链接列到ASP.NET的GridView [英] Adding Link Column to ASP.NET GridView

查看:183
本文介绍了添加链接列到ASP.NET的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要输出的头条新闻是可点击的列表。到目前为止,我可以把它打印出来头条新闻的名单,因为我拖着,并在设计视图放弃了NewsHeadline表中2010年VS你怎么想我应该在榜上无名元素可点击?我找了一个URL属性,但我没有看到它。我需要在包装一个< A HREF?

 < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
        的DataSourceID =SqlDataSource1
        EmptyDataText =有没有数据记录显示。>
        <柱体和GT;
            < ASP:BoundField的数据字段=NewsHeadline的HeaderText =NewsHeadline
                SORTEX pression =NewsHeadline/>
        < /专栏>
    < / ASP:GridView的>  < ASP:SqlDataSource的ID =SqlDataSource1=服务器
        的ConnectionString =下;%$的ConnectionStrings:ConnectionString1%>中
        的ProviderName =下;%$的ConnectionStrings:ConnectionString1.ProviderName%>中
        的SelectCommand =SELECT [NewsHeadline] FROM [NewsTable]>< / ASP:SqlDataSource的>
   < /表及GT;


解决方案

您需要从绑定列列类型更改为超链接列。

 < ASP:HyperLinkField字段HEADERTEXT =NewsHeadline
      datatextfield =NewsHeadline
      datanavigateurlfield =NewsURL
      datanavigateurlformatstring =HTTP:// {0}/>

除了使这一变化,你需要确保你选择的URL或东西,你可以用它来创建链接到新闻文章。在上面的例子中,我假设的网址是你可以从你的SQL源抢。如果它是一个ID,只需键入像这样的网址休息...〜/ MyNewsPage.aspx?NewSID的= {0}...

I want to output a list of news headlines that are clickable. So far I can get it to print out a list of headlines because I dragged and dropped the NewsHeadline table in designer view in VS 2010. How do you think I should the make the list elements clickable? I looked for a URL attribute but I did not see it. Do I need to wrap in a < a href ?

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:BoundField DataField="NewsHeadline" HeaderText="NewsHeadline" 
                SortExpression="NewsHeadline" />
        </Columns>
    </asp:GridView>

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [NewsHeadline] FROM [NewsTable]"></asp:SqlDataSource>
   </form>

解决方案

You need to change the column type from a BoundColumn to a Hyperlink column.

   <asp:hyperlinkfield headertext="NewsHeadline"
      datatextfield="NewsHeadline"
      datanavigateurlfield="NewsURL" 
      datanavigateurlformatstring="http://{0}" />

In addition to making this change, you'll need to make sure that you are selecting the URL or something you can use to create the link to the news article. In the example above, I'm assuming the URL is something you can grab from your SQL source. If it is an ID, simply type out the rest of the url like this... "~/MyNewsPage.aspx?NewsID={0}"...

这篇关于添加链接列到ASP.NET的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆