C#中的GridView HyperLink字段 [英] GridView HyperLink field in C#

查看:117
本文介绍了C#中的GridView HyperLink字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的代码:

 < asp:HyperLinkField 
DataNavigateUrlFields =NameID
DataNavigateUrlFormatString =names.aspx?nameid = {0}
DataTextField =name
HeaderText =名称
ItemStyle-Width =100px
ItemStyle -Wrap =true/>

只需要名称id即可导航到下一页。我将如何包含
两个不在gridview中的其他参数。我使用的导航url必须使用已经存在于gridview中的关键字和数据库表中的其他两个参数。我尝试使用所有这些代码。没有什么对我有用。

 < asp:HyperLinkField DataTextField =KeywordDataNavigateUrlFields =Keyword
DataNavigateUrlFormatString =KeywordSrchSumDtl.aspx?关键字= {0}&状态= {1}和城市= {2}
HeaderStyle-VerticalAlign =BottomItemStyle-Horizo​​ntalAlign =center/>

我无法使用上面的代码,因为状态和城市不在gridview中,而是在我的数据表。



我也尝试使用下面的代码,但它不起作用

 < asp:TemplateField HeaderText =KeywordItemStyle-Horizo​​ntalAlign =CenterFooterStyle-Horizo​​ntalAlign =Center> 
< ItemTemplate>
< asp:HyperLink ID =linkrunat =serverNavigateUrl ='<%#KeywordSrchSumDtl.aspx?Keyword =Eval(Keyword)+& State =+ Request。 QueryString [State]%>'Text ='<%#Eval(Keyword)%>'>< / asp:HyperLink>
< / ItemTemplate>< / asp:TemplateField>

我也试过这个,

<$ p ()()()(){$ p> < asp:HyperLink ID =Link1runat =ServerNavigateUrl ='<%#redirectURL()+ Server.UrlEncode )%>'Text ='<%#DataBinder.Eval(Container.DataItem,Keyword)%>'>< / asp:HyperLink>< / ItemTemplate>< / asp:TemplateField>

.aspx.cs
returnKeywordSrchSumDtl.aspx?Keyword =+ // DONNO如何在这里调用关键字//
+& State =+ System.Web.HttpContext.Current.Request.QueryString [State]
+& City =+ System.Web.HttpContext.Current.Request.QueryString [City];



我不知道如何解决这个问题..请帮我解决这个问题。
Thanks。

解决方案使用 DataNavigateUrlFields 属性,逗号KeywordSrchSumDtl.aspx?Keyword = {0}& State = {1}&City = {2}

 < asp:HyperLinkField DataNavigateUrlFields =关键字,状态,城市
DataNavigateUrlFormatString =KeywordSrchSumDtl.aspx?Keyword = {0 }& State = {1}&City = {2}
Text =查看详情/>

几个例子:

< a href =http://techturf.wordpress.com/2007/10/15/passing-two-arguments-in-datanavigateurlformatstring-in-hyperlink-field-of-net-20-grid-view/ =在.NET 2.0 Grid-View的超链接字段中传递DataNavigateUrlFormatString中的两个参数

com / ShowArticle.aspx?ID = 147rel =noreferrer>使用ASP.NET将多个值从一个GridView传递到另一个页面

编辑:


$ b $

RowDataBound NavigateUrl GridView

 < asp:GridView ID =GridView1runat =server
AutoGenerateColumns =False
DataKeyNames =关键字
DataSourceID =SqlDataSource1
onrowdatabound =GridView1_RowDataBound>
< asp:TemplateField HeaderText =KeywordItemStyle-Horizo​​ntalAlign =CenterFooterStyle-Horizo​​ntalAlign =Center>
< ItemTemplate>
< asp:HyperLink ID =linkrunat =serverText ='<%#Eval(Keyword)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
.......
< / asp:GridView>

后面的代码:

  protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (超链接)e.Row.FindControl( 链接);
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);
}
}
}


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 im 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.

    <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.

I tried using the following code too, but it doesnt 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>

and 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
    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 Donno How to solve this.. Please Help me solve this problem. Thanks.

解决方案

Use the DataNavigateUrlFields property, comma-delimited value with the fields for parameters in "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

Pass Multiple Values from a GridView to Another Page using ASP.NET

EDIT:

Set NavigateUrl of HyperLink in RowDataBound event of GridView

<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); 
    } 
 } 
}

这篇关于C#中的GridView HyperLink字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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