简体   繁体   English

通过GridView中的超链接传递数据

[英]Pass data via hyperlink in gridview

I would like to pass data from a database column to another page via a hyperlink in the gridview, the data i want to pass is just a load of text. 我想通过网格视图中的超链接将数据从数据库列传递到另一页,我要传递的数据只是文本的负载。 this is the code i have 这是我的代码

<asp:HyperLinkField DataTextField="FullText" 
        DataTextFormatString="View Text" NavigateUrl="~/Abstract.aspx" 
        Target="_blank" />

This appears to work as far as opening up the correct page, but im unsure as to how to view the text on the new page, most help topics tell you how to pass the data to a new grid view, but i would just like to view the data within the page or a box or whatever will work. 就打开正确的页面而言,这似乎可行,但是不确定如何在新页面上查看文本,大多数帮助主题都告诉您如何将数据传递到新的网格视图,但是我只想查看页面或框内的数据或其他有效的方法。

Thanks 谢谢

If my understanding is correct you just want to display the text passed as a query string to the new page, if this is correct, just read the query string and display it in a label. 如果我的理解是正确的,那么您只想显示作为查询字符串传递到新页面的文本,如果这是正确的,则只需阅读查询字符串并将其显示在标签中即可。

In order for this to work, you need to specify the query string in the link inside your grid, your link would have to look something like; 为了使它起作用,您需要在网格内部的链接中指定查询字符串,您的链接必须类似于:

~/Abstract.aspx?d=your+text

In your datagrid: 在您的数据网格中:

    <asp:TemplateColumn>
        <ItemTemplate>
            <asp:HyperLink
                NavigateUrl='<%# "~/Abstract.aspx?d=" + HttpUtility.UrlEncode(DataBinder.Eval(Container, "DataItem.Id").ToString()) %>' 
                runat="server"
                Text="Product" />
        </ItemTemplate>
    </asp:TemplateColumn>

In the target page you would have something like: 在目标页面中,您将具有以下内容:

string text = string.Empty;
if (this.Request.QueryString["d"] == null)
   text = "Not found";
else
   text = Server.UrlDecode(this.Request.QueryString["d"]);

// encode the text to avoid XSS (cross-site scripting)
this.myLabel.Text = Server.HtmlEncode(text);

On the grid 在网格上

<asp:HyperLinkField DataTextField="FullText" 
    DataTextFormatString="View Text" 
    NavigateUrl='<%# "~/Abstract.aspx?ft=" +  System.Web.HttpUtility.UrlEncode(Eval("FullText").ToString() %>'
    Target="_blank" />

On the page you want to read the param you would have 在页面上,您想阅读参数

string fullText = Request.QueryString["ft"];
if (string.IsNullOrEmpty(fullText))
{
    fullText = HttpUtility.UrlDecode(fullText);
}

You might want to pass a key parameter through querystring to pull data you want to view. 您可能希望通过querystring传递键参数以提取要查看的数据。

If that is not what you want please make your question more explanatory. 如果那不是您想要的,请使您的问题更具解释性。

You can pass ID in session or in query string ( Abstract.aspx?textid=1 ). 您可以在会话或查询字符串( Abstract.aspx?textid=1 )中传递ID。 Read this id in page load event get data from database and display. 在页面加载事件中读取此ID,从数据库获取数据并显示。

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

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