0

I know there are a few hundred answers to questions like this on StackOverflow but I just am not 'getting it'.

I have a shopping cart in C# / asp.net. On a product page is a "Reserve" button. When the button is clicked the first time I want it to hide the product details panel (named ProdDet) and show the calendar panel. My challenges are:

  1. I can not do it as an OnClientClick because I'm using an OnClick to do other processing and can't get them both to behave together.

  2. I don't understand a lot of what I've read. I am fairly new to ASP.NET and C# and could use a little handholding.

  3. There are several places in my code I'd like to use HidePanel and ShowPanel

My relevant pseudo-code (working and not):

Server-side:

public void CalendarButton_Click(object sender, EventArgs e)
{
    some processing 
    if (everything ok) {
       CalendarLiteral.Text += "<iframe...";
       Hide_Panel(); // not working.
    }
}

public void HidePanel()
{
       Page.ClientScript.RegisterStartupScript(
         ...just not getting it or even sure this is the right thing to do.
       );
}

Client-side in head section:

<script type="text/javascript">
      function HideContent(d) {
      document.getElementById(d).style.display = "none";
      Alert("Hiding " + d);
      }
</script>

It's not terribly bad but some of this server/client/.NET stuff is not easy to self-teach when coming from a straight C background. Any help is appreciated!

3 Answers 3

3

You can always write out javascript using:

Page.ClientScript.RegisterStartupScript(key, "HideContent('" + ControlID.ClientID + "');", true);

Or:

ScriptManager.RegisterStartupScript(..);

Parameters listed may not be exact, but this is essentially what you can do. If you have the reference to the control on the server, you can call a method on the client this way.

Sign up to request clarification or add additional context in comments.

2 Comments

As in comment to Sofian's answer, do I just use the RegisterStartupScript when I want it to hide or is this a "on page load" type of function?
RegisterStartupScript renders the script and it will execute immediately, but you can also render out a Sys.Application.add_load(function() { HideContent(..); }); too
0

You need to call the function HideContent with the right arrgument. Register a script that would do exactly that.

Page.ClientScript.RegisterStartupScript(this.GetType(),"Script name",
"<script type=text/javascript> 
HideContent('foo') 
</script>");

3 Comments

So at any point in the page I can emit the RegisterStartupScript command and have it "execute" on the front end at that time? Thanks.
Yes! You just call HideContent when you need it.
This got me closer to my desired result. Thanks!
0

I'm not sure I'm getting what you need, but why don't you wrap things inside different ...

<asp:Panel ID="pnl1"...

where you can set in your code behind pnl1.visible = false or true.

2 Comments

I tried this at first but the panel contains a few text boxes and when I hide and then show them they lose their values since they are recreated. That's why I'm looking to do it via JS.
in this case you can create a server side Div: <div id="div1" runat="server"> which you can reference from your code behind and use standard css commands (display:none) to show/hide

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.