如何为包括NavigateUrl以超链接Session变量 [英] How to include Session variable in NavigateUrl in hyperlink

查看:354
本文介绍了如何为包括NavigateUrl以超链接Session变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我这样做过,但不记得的语法。我怎样包括nagivateUrl会话变量中的超链接?

我试过这样:

 < ASP:超链接ID =lnkMyLink=服务器文本=我的链接
 NavigateUrl ='<%#http://absoluteURL.org?param=+
 会话[myParameterValue]的ToString()%GT;'>< / ASP:超链接>

和这样的:

 < ASP:超链接ID =lnkMyLink=服务器文本=我的链接
 NavigateUrl ='<%#的String.Format(http://absoluteURL.org?param={0},
 会话[myParameterValue]的ToString())%GT;'>< / ASP:超链接>


解决方案

由于你使用数据绑定格式(<%#),你需要调用超链接 .DataBind()从codebehind方法。

您需要您的Page_Load方法看起来是这样的:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    lnkMyLink.DataBind();
}

唯一要记住,使用数据绑定这样的事情,即没有具体数据绑定,可能是人谁能够维护您在未来的code有点混乱。虽然,为什么你做到了,凡是能导致未来的混乱应该从code除去更多钞票的地方,它会是相当快速和容易确定你做了什么和。因此,一个潜在的更好的选择将是把以下在的Page_Load

  lnkMyLink.NavigateUrl =
    的String.Format(http://absoluteURL.org?param={0},会话[myParameterValue]);

I'm sure I've done this before, but can't remember the syntax. How do I include a session variable in nagivateUrl in a hyperlink?

I've tried this:

<asp:HyperLink ID="lnkMyLink" runat="server" Text="My Link" 
 NavigateUrl='<%# "http://absoluteURL.org?param=" +
 Session["myParameterValue"].ToString()%>'></asp:HyperLink>

and this:

<asp:HyperLink ID="lnkMyLink" runat="server" Text="My Link" 
 NavigateUrl='<%# String.Format("http://absoluteURL.org?param={0}",
 Session["myParameterValue"].ToString()) %>'></asp:HyperLink>

解决方案

Because you've used the data binding format (<%#), you need to call the HyperLinks .DataBind() method from your codebehind.

You need your Page_Load method to look something like this:

protected void Page_Load(object sender, EventArgs e)
{
    lnkMyLink.DataBind();
}

The only thing to bear in mind that using Data Binding for something like this, i.e. not specifically Data Binding, may be a bit confusing for anyone who has to maintain your code in the future. Whilst it'll be fairly quick and easy to determine what you've done and why you've done it, anything that can cause future confusion should be removed from your code where posible. Therefore a potentially better option would be to put the following in your Page_Load:

lnkMyLink.NavigateUrl = 
    string.Format("http://absoluteURL.org?param={0}", Session["myParameterValue"]);

这篇关于如何为包括NavigateUrl以超链接Session变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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