使用 QueryString 参数。
主页.aspx
//When linked to RCA.aspx from Home.aspx, a parameter called ShowButton=1 is included
//in the URL.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Post_ID", "~/RCA.aspx?Post_ID={0}&ShowButton=1") %>'
Text="SEND"></asp:HyperLink>
RCA.aspx
//By default, since you want the button to NOT appear for all incoming traffic EXCEPT
//that which came from Home.aspx, the button's Visible property is set to false.
<asp:Button ID="btnRCA" runat="server" onclick="Button1_Click"
Text="Assign RCA" Width="147px" Visible="false" />
RCA.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//If the querystring has a parameter called ShowButton and it's equal to "1",
//then set the button to Visible = true.
//Else, do nothing, keeping the button in it's default, Visible=false state.
//By putting this in an "IsPostback == false" check, you can guarantee that this will
//only happen on first page_load, and won't be triggered again even if you do other
//actions in the page that cause Postback
//For example, if you don't use this !IsPostback check, and you end up creating some
//new function that causes the button to be hidden again, but then you make a
//selection from a dropdown list that causes postback, you will trigger the call to
//make the button Visible again, even though that's probably what you don't want at
//this point, since your other new function set it to Visible = false.
if (!IsPostback)
{
if (Request.QueryString["ShowButton"] == "1")
{
RCAbtn.Visible = true;
}
if (Request.QueryString["Post_ID"] != null)
{
//do whatever you need to with the post ID
}
}
}
SomeOtherPage.aspx.cs
Response.Redirect("RCA.aspx?Post_ID=1234"); //button will be invisible
然后让我们稍后说您想从其他页面重定向并让按钮可见,例如来自主页的重定向:
Response.Redirect("RCA.aspx?Post_ID=1234&ShowButton=1"); //button will be visible
如果您不喜欢弄乱您的 URL,或者您觉得将您所做的事情如此清晰地呈现给用户的眼睛看起来很俗气,那么您不一定需要使用“ShowButton”。您可以说 ?Post_ID=1234&fkai3jfkjhsadf=1,然后检查您的查询字符串中是否有“fkai3jfkjhsadf”。有时我喜欢这样做,因为从用户的角度来看,这让我看起来像是在做一些真正技术性和加密的事情,而不仅仅是用简单的英语传递一堆基本指令:) 缺点是你需要跟踪您自己的查询字符串参数。
编辑:
如果您想获取仅包含 Post_ID 而没有其他内容的 URL,您可以执行以下操作:
string currenturl = Request.Url.ToString(); //get the current URL
string urlToSend = currenturl.Substring(0, currenturl.IndexOf("?")); //cut off the QueryString entirely
urlToSend += "?Post_ID=" + Request.QueryString["Post_ID"]; //re-append the Post_ID
请注意,如果 URL 没有 QueryString,您对 Substring 的调用将导致异常,因此请以最适合您的方式(try/catch 等)对其进行修补。
之后,您应该可以在 mailMessage.Body 中使用“urlToSend”字符串。