简体   繁体   中英

ASP.NET/C# trying to use page_load method on a form with #A on end of web address through link

I am needing to use the same form for displaying multiple things and I realize that when I add links like:

    <a href="#A">A</a>

it has the same form and web address, but with a #A at the end of the address.I thought I could use this for displaying multiple things on the same form. My idea is to have C# code in the page_load method to detect what the web address is and use a conatins method for the url string and detect if there is #A to change the content of the form. Here is an example:

C# code:

    protected void Page_Load(object sender, EventArgs e)
    {
         string url = HttpContext.Current.Request.Url.AbsoluteUri;
         if(url.Contains("#A"))
         {
              div1.Visible = false; //content 1
              div2.visible = true;  //content 2
         }
    }

asp.net code:

    <a href="#A">A</a>
    <div ID="div1" runat="server">
        content 1
    </div>
    <div ID="div2" runat="server">
        content 2
    </div>

I have tried to put the Page_Load method in a script tag, but still didn't work. I guess since the url is different the cs code is not valid? I know it goes through the page_load method once, before I click on the link. Also I do use a method that gives me the controls of div1 and div2, so that is not the problem. I thank everyone in advance for your help! Also if my way is not the way to do the job then please tell me any way possible to achieve what I am trying to do.

edit: I can't use a button to replace a link... maybe a asp:hyperlink?

That's an HTML hyperlink you're using and it won't cause a postback thus page_load will never get called when you click it.

I would suggest if you want to show an hide divs that you use client side JavaScript. Alternatively you could (for example) use an asp.net button control which will cause a postback.

I would suggest scrapping the anchor with an href approach in favor of this:

Use the ASP.NET server controls, along with their click event handlers to manage the visibility of controls on your page.

In your Page_Load , make it so the page has an initial state of showing controls, like this:

protected void Page_Load(object sender, EventArgs e)
{
     if(!Page.IsPostBack)
     {
          div1.Visible = false; //content 1
          div2.visible = true;  //content 2
     }
}

Now instead of an anchor tag, you can use an ASP.NET Button or LinkButton to cause a postback to the server, like this:

<asp:Button id="Button1" 
       Text="Click here to change page to B"
       OnClick="Button1_Click" 
       runat="server"/>

Now you have the event handler code which would change the visibility of controls, like this:

protected void Button1_Click(Object sender, EventArgs e)
{
    div1.Visible = true; //content 1
    div2.visible = false;  //content 2
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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