1

我正在创建一个数据表,然后将其链接到我的 ASP.NET 网页上的 GridView,此 GridView 中有两列,我希望将第二列显示为超链接字段。这是代码

 protected void btnSearch_Click(object sender, EventArgs e)
{

    DataTable SearchTable = new DataTable();
    DataColumn Title = new DataColumn("Title", typeof(System.String));
    DataColumn url = new DataColumn("url", typeof(System.String));
    SearchTable.Columns.Add(Title);
    SearchTable.Columns.Add(url);
    DataRow ResultRow = null;


    var bingContainer = new Bing.BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search/"));
    var accountKey = "MyKey";

    // the next line configures the bingContainer to use your credentials.
    bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);

    // now we can build the query
    var webQuery = bingContainer.Web(this.txtSearch.Text, null, null, null, null, null);

    var webResults = webQuery.Execute();

    foreach (var result in webResults)
    {


        ResultRow = SearchTable.NewRow();
        ResultRow["Title"] = result.Title;
        ResultRow["url"] = result.Url;  //I want this Field as a HyperLink

        SearchTable.Rows.Add(ResultRow);
    }

    grdResult.DataSource = SearchTable;
    grdResult.DataBind();


}

知道如何将“Url”字段作为超链接字段吗?

4

3 回答 3

0

由于您的字段是字符串类型,您可以将超链接添加为文本:

ResultRow["url"] = string.format("<a href='{0}'>{0}</a>", result.Url);

您最好让视图处理此问题并仅存储 url:

     <asp:TemplateField>
        <ItemTemplate>
            <a href='<%# Eval("url") %>'><%# Eval("url") %></a>
        </ItemTemplate>
     </asp:TemplateField>
于 2012-06-22T22:59:58.920 回答
0
DataTable SearchTable = new DataTable();
DataColumn Title = new DataColumn("Title", typeof(System.String));
DataColumn url = new DataColumn("url", typeof(System.String));
SearchTable.Columns.Add(Title);
SearchTable.Columns.Add(url);
DataRow ResultRow = null;


var bingContainer = new Bing.BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search/"));
var accountKey = "MyKey";

// the next line configures the bingContainer to use your credentials.
bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);

// now we can build the query
var webQuery = bingContainer.Web(this.txtSearch.Text, null, null, null, null, null);

var webResults = webQuery.Execute();

foreach (var result in webResults)
{


    ResultRow = SearchTable.NewRow();
    ResultRow["Title"] = result.Title;
    ResultRow["url"] = result.Url;  //I want this Field as a HyperLink

    SearchTable.Rows.Add(ResultRow);
}

grdResult.DataSource = SearchTable;
grdResult.DataBind();

这就是你如何将标题和 URL 与 Grid 绑定

<asp:GridView ID="grdResult" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField HeaderText="Title">
                <HeaderTemplate>
                    Title
                </HeaderTemplate>
                <ItemTemplate>
                    <%#DataBinder.Eval(Container.DataItem, "Title")%></span>
                </ItemTemplate>
                <FooterTemplate>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="URL">
                <HeaderTemplate>
                    URL
                </HeaderTemplate>
                <ItemTemplate>
                    <a href="<%#DataBinder.Eval(Container.DataItem, "url")%>" target="_blank">
                        <%#DataBinder.Eval(Container.DataItem, "url")%></a><br />
                </ItemTemplate>
                <FooterTemplate>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

希望这可以帮助。您也可以访问此链接,让您清楚了解使用 Bing API Buena Digital

于 2012-06-22T23:01:16.260 回答
0

<asp:HyperLinkField HeaderText="URL" DataTextField="URLName" runat="server" DataNavigateUrlFields ="URLName"//This must be present DataNavigateUrlFormatString ="{0}"/>

这应该可以解决您的问题或其他有类似问题的人

于 2014-10-14T13:54:15.137 回答