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!

share|improve this question

feedback

3 Answers

up vote 0 down vote accepted

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>");
share|improve this answer
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. – Deverill Feb 20 '12 at 18:55
Yes! You just call HideContent when you need it. – Sofian Hnaide Feb 20 '12 at 19:03
1  
This got me closer to my desired result. Thanks! – Deverill Feb 20 '12 at 19:54
feedback

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.

share|improve this answer
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? – Deverill Feb 20 '12 at 18:56
RegisterStartupScript renders the script and it will execute immediately, but you can also render out a Sys.Application.add_load(function() { HideContent(..); }); too – Brian Mains Feb 20 '12 at 19:08
Thank you - this helped too. – Deverill Feb 20 '12 at 19:54
feedback

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.

share|improve this answer
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. – Deverill Feb 20 '12 at 18:52
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 – enricoariel Feb 21 '12 at 8:33
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.