Search code examples
ascx

Call function from ascx to .cs of a user control and draw html


I have a user control, I want to make if conditions in the ascx, and upon that call a function in the user control and return data from the method to be displayed as html in the ascx. Any ideas? I am still newbie to asp.


Solution

  • Just drop a literal onto your user control. Then in your code behind, it'll look something like this:

    private void Page_Load(object sender, System.EventArgs e)
    {
        // Make your IF conditions here
    
        MyFunction();
    }
    
    private void MyFunction()
    {
        string myHtml = "<div>My Html Code!</div>";
    
        literal1.Text = myHtml;
    }
    

    That should render your HTML.