0

I'm using inline C# to write a dynamic drop down. Depending on if the user is logged in or not. I'd like to be able to write in some asp textbox's (see below). Is there a way to do this, or a better way of accomplishing the task.

Thanks.

<%
    if (checkLogin() == true)
    {
        Response.Write("<ul id=\"userDropdown\">");
        Response.Write("<li class=\"userDropdown_right\"><a href=\"#\" class=\"drop\">Test User</a>");
        Response.Write("<div class=\"dropdown align_right\" style=\"width:175px;\">");
        Response.Write("<div class=\"col_2\">");
        Response.Write("<h2>Test User</h2>");
        Response.Write("<h3>Administrator</h3>");
        Response.Write("<a href=\"#\">Settings </a>");
        Response.Write("<a href=\"#\">Logout </a>");
        Response.Write("</div></div></li></ul>");
    }
    else
    {
        Response.Write("<ul id=\"userDropdown\">"); 
        Response.Write("<li class=\"userDropdown_right\"><a href=\"#\" class=\"drop\">Login</a>");
        Response.Write("<div class=\"dropdown align_right\" style=\"width:175px;\">");
        Response.Write("<div class=\"col_2\">");

        **<asp:TextBox ID="test" runat="server" />**


        Response.Write("</div></div></li></ul>");
    }
%>
3
  • You can use a PlaceHolder control to direct the output to a specific place on the page rather than Response.Write. Also, no, writing a server-side control to the page in this manner will not work (at least, it shouldn't, and if it does you shouldn't trust it). I'll defer to the other suggestions already given for a better approach... Commented Dec 5, 2011 at 21:08
  • Create a custom control, and use LoginView control to display it if the user is logged in. Commented Dec 5, 2011 at 21:08
  • @Henk I'm using WebForms Commented Dec 6, 2011 at 17:02

3 Answers 3

4

You could use a conditional like this:

<%
if (checkLogin()==true){
%> Logged In content here <%
} else { 
%>
Not Logged In content here, including server controls.
<% 
} 
%> 

A templated control would also work.

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

Comments

2

Did you try this? The results from Response.Write() will be placed above the opening <html> tag.
So that's not a good idea.

You can use a Templated control, the LoginStatus control seems perfect for the job.

Comments

2

@Tetsujin no Oni answer solves the problem, but...

Why don't you create your own user control (customDropDown.ascx) ???

By doing that you would be able to:

  1. Reuse it elsewhere
  2. Encapsulate the behavior, so you'll only need to make changes to one place
  3. Don't work with strings and have intellisense support
  4. Write HTML tags with support to visual studio preview

2 Comments

I agree, this html should be in a user control with its associated business logic in the controls code behind.
I can't see myself using this more than just in this one instance, or else creating a user control would be a good solution.

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.