0

我在一个名为 Home 的 ASP 页面中有一个名为“SEND”的超链接,它是:

<asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" runat="server" 
                            NavigateUrl='<%# Eval("Post_ID", "~/RCA.aspx?Post_ID={0}") %>' 
                            Text="SEND"></asp:HyperLink>
                    </ItemTemplate>

当用户单击超链接时,它会转到另一个名为 RCA 的页面,在此页面中有一个按钮,代码如下:

<asp:Button ID="btnRCA" runat="server" onclick="Button1_Click" 
                  Text="Assign RCA" Width="147px" />

所以我希望这个按钮只有在点击主页中的超链接时才可见。我计划在 RCA 页面中有另一个按钮或控件,当单击时或在有人离开页面之前,它们将使其不可见,他们必须通过单击其他控件使其不可见按钮。有人可以帮我弄这个吗?谢谢

4

2 回答 2

1

在你的第二页上page_load,试试这个:

 protected void Page_Load(object sender, EventArgs e)
  {
        if (Request.QueryString["Post_ID"] != null)
          {
             btnRca.Visible = true;    
          }
   }

我不知道在其他情况下您想如何处理此按钮的可见性,但这应该可以回答您的特定问题。

于 2012-10-02T23:47:01.670 回答
1

使用 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”字符串。

于 2012-10-03T02:02:08.180 回答