C# C中的GridView超链接字段#
请看下面的代码:C# C中的GridView超链接字段#,c#,gridview,hyperlink,C#,Gridview,Hyperlink,请看下面的代码: <asp:HyperLinkField DataNavigateUrlFields="NameID" DataNavigateUrlFormatString="names.aspx?nameid={0}" DataTextField="name" HeaderText="Name" ItemStyle-Width="100px" ItemStyle-Wrap="true" /> 我不知道如何解决这个问题。您可
<asp:HyperLinkField
DataNavigateUrlFields="NameID"
DataNavigateUrlFormatString="names.aspx?nameid={0}"
DataTextField="name"
HeaderText="Name"
ItemStyle-Width="100px"
ItemStyle-Wrap="true" />
我不知道如何解决这个问题。您可以尝试使用
string.Format
方法
NavigateUrl='<%# String.Format("KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}", DataBinder.Eval(Container.DataItem, "Keyword"), Request.QueryString["State"], Request.QueryString["City"]) %>'
NavigateUrl=''
您可以使用字符串[]从代码隐藏中初始化DataNavigateUrlFields:
yourHyperLinkField.DataNavigateUrlFields = new string[] { "Keyword", "State", "City" };
使用
DataNavigateUrlFields
属性,逗号分隔的值与“关键字srchsumdtl.aspx?关键字={0}&State={1}&City={2}”中的参数字段一起使用。
代码隐藏:
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);
}
}
}
最后,它通过以下代码进行导航
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;
}
}
}
感谢大家的帮助。有时我们需要在Gridview、datagrid或任何数据列表控件中使用超链接传递多个参数,然后我们可以使用以下代码:-
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;
}
**代码:-**
您的代码没有将关键字列绑定到表。所以我现在加了一句。但没有显示输出。。谢谢。。。!!我得到了输出。。谢谢……)在所选数据源上找不到名为“State”的字段或属性。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.Web.HttpException:在所选数据源上找不到名为“State”的字段或属性。源错误:第40行:dv=bindGridView();第41行:GridView1.DataSource=dv;第42行:GridView1.DataBind();第43行:第44行:}州和城市不在gridview中。但如果这些字段不在表ryt中,则它将不起作用??只有关键字,如果您查看后面的代码,您将看到其他参数的值实际上来自请求对象。我不知道它不接受其他两个参数。这就是它重定向-->&State=&City的方式=
<asp:HyperLinkField DataNavigateUrlFields="Keyword,State,City"
DataNavigateUrlFormatString="KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"
Text="View Details" />
<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>
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);
}
}
}
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;
}
}
}
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;
}