locked
Change CSS class in runtime RRS feed

  • Question

  • User1420911473 posted

    I have just started working with the ASP.net framework for a school project, so am very new to how it all works. I have tried to reading other post, but i have not been able to figure it out.

    What i want to do: I have div which works as a pop up box, which needs to pop up after the user presses a button.  

    How i have tried to do it: 

    • My pop up box, has an id, which has been set to hidden. 
    • My button is in a form, and calls a function in my controller, which calls a function in my model
    • In the model i use this code to try and add a class to the pop up box, called hidden, which should hide it.
    • The code runs, with no errors, but the box does not get class. 

    Html code 

    <div id="TwoFactorPopUp" name="TwoFactorPopUp" runat="server">
        .... stuff
    <\div>

    Model code 

    HtmlGenericControl popup = new HtmlGenericControl("TwoFactorPopUp");
    popup.Attributes["class"] = "hidden"; 

    Edit: i have tried with debugs, and it seems like popup does not contain anything, tried and print id and inner html, and it just prints an empty line 

    Sunday, November 10, 2019 11:38 AM

All replies

  • User753101303 posted

    Hi,

    The first part shows how to create a control inside an ASPX page. Then you show how to create a control by code. For now it seems you perhaps create another control by code that you don't use and so the code doesn't have any effect?

    If properly defined in your ASPX page your code could be just TwoFactorPopUp.Attributes["class"]="hidden"; to change the existing control (rather tthen creating a new one you don't use).

    The id property is used to expose your "runat" control as a page member so that your server side code can interact with controls defined as part of your ASPX page.

    Sunday, November 10, 2019 5:33 PM
  • User1420911473 posted

    Okay, so just to make sure i understand. Having given my Div both runat="server" and an ID, i should be able to just refer to that object with the ID name ? Because when i try and just write TwoFactorPopUp.Attributes["class"]="hidden", i get a TwoFactorPopUp  does not exist in the current context. So i'm guessing there is something am missing ? 

    Sunday, November 10, 2019 6:09 PM
  • User753101303 posted

    You are using Visual Studio ? In addition to the "mypage.aspx" file, you should have a "mypage.aspx.designer.cs" file which is created automatically and contains C# declaration for controls found in your page...

    Another option might be to not use Web Forms for new applications. (this is part of ASP.NET 4.x) ASP.NET Core is a cross platform rewrite and uses rather https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-3.0&tabs=visual-studio or MVC.

    Sunday, November 10, 2019 8:05 PM
  • User283571144 posted

    Hi Bjarked,

    Could you please post the details aspx codes and the code-behind make up? Do you add the TwoFactorPopUp dynamically in the page load event?

    Besides, I suggest you could try to use Page. Find Control(String) method to find the div by id to achieve your requirement. 

    Best Regards,

    Brando

    Monday, November 11, 2019 9:20 AM