C# 中的 GridView 超链接字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12823422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GridView HyperLink field in C#
提问by Sam
Take a look at the following code:
看看下面的代码:
<asp:HyperLinkField
DataNavigateUrlFields="NameID"
DataNavigateUrlFormatString="names.aspx?nameid={0}"
DataTextField="name"
HeaderText="Name"
ItemStyle-Width="100px"
ItemStyle-Wrap="true" />
It takes only the name id to navigate to the next page. How will I include the two other parameters which are not in the gridview. The navigate URL I'm using has to take the keyword which is already present in the gridview and the other two parameters from the database table. I tried using all these codes. Nothing did work for me.
导航到下一页只需要名称 id。我将如何包含不在 gridview 中的其他两个参数。我使用的导航 URL 必须采用 gridview 中已经存在的关键字和数据库表中的其他两个参数。我尝试使用所有这些代码。没有什么对我有用。
<asp:HyperLinkField DataTextField="Keyword" DataNavigateUrlFields="Keyword"
DataNavigateUrlFormatString="KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"
HeaderStyle-VerticalAlign="Bottom" ItemStyle-HorizontalAlign="center" />
I cant use the above the code because the state and city are not in the GridView but available in my data table.
我不能使用上面的代码,因为州和城市不在 GridView 中,但在我的数据表中可用。
I tried using the following code too, but it doesn't work:
我也尝试使用以下代码,但它不起作用:
<asp:TemplateField HeaderText="Keyword" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink ID="link" runat="server" NavigateUrl='<% # "KeywordSrchSumDtl.aspx?Keyword="Eval("Keyword")+"&State="+Request.QueryString["State"]%>' Text='<%# Eval("Keyword") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I also tried this:
我也试过这个:
<asp:HyperLink ID="Link1" runat="Server" NavigateUrl='<%#redirectURL()+Server.UrlEncode((Eval("Keyword")).ToString())%>' Text='<%# DataBinder.Eval(Container.DataItem,"Keyword") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
.aspx.cs
.aspx.cs
return "KeywordSrchSumDtl.aspx?Keyword=" +
//I DONNO HOW TO CALL THE KEYWORD HERE//
+ "&State=" + System.Web.HttpContext.Current.Request.QueryString["State"]
+ "&City=" + System.Web.HttpContext.Current.Request.QueryString["City"];
I don't know how to solve this.
我不知道如何解决这个问题。
采纳答案by chridam
Use the DataNavigateUrlFieldsproperty, comma-delimited value with the fields for parameters in "KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"
将DataNavigateUrlFields属性、逗号分隔的值与中的参数字段一起使用"KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"
<asp:HyperLinkField DataNavigateUrlFields="Keyword,State,City"
DataNavigateUrlFormatString="KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"
Text="View Details" />
A couple of examples:
几个例子:
Passing two arguments in DataNavigateUrlFormatString in hyperlink field of .NET 2.0 Grid-View
在 .NET 2.0 Grid-View 的超链接字段中的 DataNavigateUrlFormatString 中传递两个参数
Pass Multiple Values from a GridView to Another Page using ASP.NET
使用 ASP.NET 将多个值从 GridView 传递到另一个页面
EDIT:
编辑:
Set NavigateUrlof HyperLink in RowDataBoundevent of GridView
事件中NavigateUrl的超链接集RowDataBoundGridView
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="Keyword"
DataSourceID="SqlDataSource1"
onrowdatabound="GridView1_RowDataBound">
<asp:TemplateField HeaderText="Keyword" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink ID="link" runat="server" Text='<%# Eval("Keyword") %>' />
</ItemTemplate>
</asp:TemplateField>
.......
</asp:GridView>
Code behind:
后面的代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("link");
if (hl != null)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
string keyword = drv["Keyword"].ToString();
string state = Request.QueryString["State"];
string city = Request.QueryString["City"];
hl.NavigateUrl = "~/KeywordSrchSumDtl.aspx?Keyword=" + keyword + "&State=" + Server.UrlEncode(state) + "&City=" + Server.UrlEncode(city);
}
}
}
回答by Aghilas Yakoub
You can try with string.Formatmethod
你可以试试string.Format方法
NavigateUrl='<%# String.Format("KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}", DataBinder.Eval(Container.DataItem, "Keyword"), Request.QueryString["State"], Request.QueryString["City"]) %>'
回答by Vladimirs
You may initialize DataNavigateUrlFields from Code Behind with string[]:
您可以使用 string[] 从代码隐藏初始化 DataNavigateUrlFields:
yourHyperLinkField.DataNavigateUrlFields = new string[] { "Keyword", "State", "City" };
回答by Sam
Finally it Navigates by the following code,
最后它通过以下代码导航,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("Link");
if (hl != null)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
string keyword = drv["Keyword"].ToString().Trim();
string state = strState.ToString().Trim();
string city = strCity.ToString().Trim();
hl.NavigateUrl = "KeywordSrchSumDtl.aspx?Keyword=" + keyword + "&Geo=" + geo + "&Site=" + site;
}
}
}
Thank u guys for the help.
谢谢你们的帮助。
回答by mschoudhary
Some time we need to pass multiple parameters with hyperlink in Gridview, datagrid or any data list control then we can use following code:-</br>
**CODE:-**
<asp:GridView ID="gvFin" runat="server" CellPadding="1" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField ItemStyle-Width="4%" HeaderStyle-Width="4%" SortExpression="CDL"
HeaderText="CDL#" HeaderStyle-Font-Bold="true">
<ItemTemplate>
<asp:HyperLink ID="lnk1" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"TestValue") %>'
NavigateUrl='<%# "javascript:ShowACP(\"" + DataBinder.Eval(Container.DataItem, "ID") + "\",\"" + DataBinder.Eval(Container.DataItem,"ACCOUNTPLAN") + "\");" %>' ForeColor="Blue" / </ItemTemplate>
</asp:TemplateField>
**JavaScript Function**
function ShowACP(id, acplabel)
{ if (acplabel == "No")
{
window.location = "#";
}
else</br>
window.location = "Default.aspx?gid=" + id;
}

