-4

I have an asp.net element (form) with a next button, I want when the user click on the button the first div hide and the second div shows by using a c# code.

13
  • 2
    Sound like the sort of thing you should be doing in javascript. If you want to do it in c# you will have to do it server side so post back the form basically. Aug 20, 2015 at 14:27
  • Do you have search ?
    – binard
    Aug 20, 2015 at 14:28
  • Why do you want to involve a post-back to the server just to hide an element? Is this WebForms or MVC? What have you tried? Where are you stuck?
    – David
    Aug 20, 2015 at 14:29
  • possible duplicate of How to hide a div from code (c#)
    – Slappywag
    Aug 20, 2015 at 14:32
  • Ben Robinson yes I want to do it on server side
    – Saif
    Aug 20, 2015 at 14:34

2 Answers 2

2

Something like this should do it

markup:

<div id="firstdiv" runat="server"></div>
<div id="seconddiv" runat="server"></div>

C#:

protected void MyButton_Click(object sender, EventArgs e)
{
    firstdiv.Visible = false;
    seconddiv.Visible = true;
}

You can expand from this as required.

0
1

Add runat = "server" and id to the <div> you want to show/hide. Then in the server-side C# code you can hide, like this:

var theDiv = YourDivById as HtmlGenericControl;

if (theDiv != null)
{
    theDiv.Style.Add ("Display", "none");
}

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