简体   繁体   English

为gridview中的每个数据设置不同的超链接

[英]Set different hyperlink for each data in the gridview

I am try to add hyperlinks to the stockName in the gridview from the database. 我尝试从数据库在gridview将超链接添加到stockName I manage to make the hyperlink to direct to a single page only. 我设法使超链接仅定向到单个页面。 How can i add different url hyperlink for different stockname? 如何为不同的股票名称添加不同的URL超链接?

The url i wanna to add for the 1st stockname is gentingdisplay.aspx , 2nd is pangkordisplay.aspx , 3rd is langkawidisplay.aspx and so on. 我想补充的第一stockname网址是gentingdisplay.aspx ,第二是pangkordisplay.aspx ,第三个是langkawidisplay.aspx等。

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataSourceID="SqlDataSource2" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
             <asp:BoundField DataField="StockID" HeaderText="Stock ID" 
                SortExpression="StockID" />
                <asp:HyperLinkField 
  DataNavigateUrlFields="StockName" 
  DataNavigateUrlFormatString="gentingdisplay.aspx?StockID=0" 
  HeaderText="Stock Name" 
  SortExpression="StockName" 
  DataTextField="StockName" />
            <asp:BoundField DataField="StockPrice" HeaderText="Stock Price" 
                SortExpression="StockPrice" />
            <asp:BoundField DataField="UnitAvailable" HeaderText="Unit Available" 
                SortExpression="UnitAvailable" />

As the documentation for DataNavigateUrlFields says, you can provide a comma-separated list of fields to use in the DataNavigateUrlFormatString . 正如DataNavigateUrlFields的文档所述 ,您可以提供一个逗号分隔的字段列表,以在DataNavigateUrlFormatString中使用。

So if I understand you correctly and you want the StockName to make up part of the hyperlink path and pass the StockID in the StockID querystring, you can use this in your HyperLinkField : 所以,如果我理解正确的话,你希望StockName弥补的超级链接路径的一部分,并通过StockID在StockID查询字符串,你可以在你使用这个HyperLinkField

<asp:HyperLinkField 
  DataNavigateUrlFields="StockName,StockID" 
  DataNavigateUrlFormatString="{0}display.aspx?StockID={1}" 
  HeaderText="Stock Name" 
  SortExpression="StockName" 
  DataTextField="StockName" />

Here {0} maps to StockName and {1} maps to StockID , using the standard String.Format syntax. 这里{0}映射到StockName{1}映射到StockID ,使用标准String.Format语法。

You probably want 你可能想要

  <asp:HyperLinkField DataNavigateUrlFields="StockName" 
  DataNavigateUrlFormatString="{0}display.aspx" 
  HeaderText="Link" 
  SortExpression="StockName" 
  DataTextField="StockName" />

However, I think it may bad practice to organize your site with the pages designed in this manner - It may be hard to maintain. 但是,我认为用这种方式设计的页面来组织网站可能是一种不好的做法-可能很难维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM