在ASP中使控件可见/不可见

时间:2012-10-02 23:42:29

标签: asp.net

我在名为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的页面,在这个页面中有一个Button,这里是代码:

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

所以我希望只有在单击HOME页面中的超链接时才能看到此按钮。我计划在RCA页面中设置另一个按钮或控件,使其在单击时不可见,或者在有人离开页面之前,通过单击其他控件使其不可见按钮。有人可以帮我弄这个吗?谢谢

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

使用QueryString参数。

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

然后让我们稍后说你要从其他一些页面重定向并让按钮可见,就像来自Home的重定向一样:

Response.Redirect("RCA.aspx?Post_ID=1234&ShowButton=1"); //button will be visible

如果您不喜欢弄乱您的网址,或者您认为让用户眼睛明白地看到您正在做的事情看起来很俗气,那么您不一定需要使用“ShowButton”。您可以说?Post_ID = 1234&amp; 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”字符串。