views:

358

answers:

6

I am trying to learn asp.net (vb.net) and I'm having some trouble. I want to change a pages content based on the querystring.

In classic asp I would do:

<% If request.querystring("page") = 1 THEN %>

-entire page-

<% Else   %>

-different page-

<% End If %>

The closest I could get in .net is

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)  
        If Request.QueryString("page") = 1 Then  
            lblMessage1.Text = "message"  
        Else  
            lblMessage1.Text = "message2"  
        End If  
End Sub

That only seems good for small things. What would be the best method to change an entire page?

+6  A: 

You could do the following (simple redirect):

If Request.QueryString("page") = 1 Then
   Response.Redirect("MyPage1.aspx")
Else
   Response.Redirect("MyPage2.aspx")
End If

You could also do this (read more here):

If Request.QueryString("page") = 1 Then
   Server.Transfer("MyPage1.aspx")
Else
   Server.Transfer("MyPage2.aspx")
End If

And finally one more option (show/hide different panels on the page):

If Request.QueryString("page") = 1 Then
   MyPanel1.Visible = true
   MyPanel2.Visible = false
Else
   MyPanel1.Visible = false
   MyPanel2.Visible = true
End If
RSolberg
@womp: I just got hit with -4 within 20 seconds... Your post does not actually fit the bill. See my note.
RSolberg
Maybe it wasn't you... every post lost a vote, and I noticed you dropped 6 rep... apologies.
womp
@womp: no worries, I do hate the gaming the system that goes on here... I know folks go back and fourth on it on META, but when you lose 4 points in the matter of 20 seconds with no explanation as to why, it is very fishy.
RSolberg
It's always interesting to see which threads generate this kind of activity... invariably it's the "easier" questions
womp
No kidding... I really see it take off on the jQuery ones. Even seen some where answer #2 was copy paste from #1 (or typed the same) and then answer 1 is voted down at -1...
RSolberg
+1  A: 

You really have a few options, you could:

  • Response.Redirect(url) to a different page based on the input.

  • You could have an ASP:Panel with the "visible" property set to false and toggle that value based on the input.

JMP
Or a MultiView instead of the Panel.
Forgotten Semicolon
MultiView is probably more suited to this, yes..
Juri
+4  A: 

I would suggest using the MultiView control.

In a nutshell, you would create two multiview "Views", each with the html that you would want to show. Then you could look at the querystring parameter and switch the active view of the multiview accordingly.

This has a lot of advantages to Response.Redirect() like others suggested. First off, that would always generate at least two browser requests. Also, Response.Redirect() throws a ThreadAborted exception behind the scenes, which can confuse people diagnosing the application.

Example MultiView control:

ASPX:

<form id="form1" runat="server">
    <div>
    <asp:MultiView ID="MultiView1" runat="server">
        <asp:View runat="server">
        Hi, this is Page 1
        </asp:View>
        <asp:View runat="server">
        Hi, this is Page 2
        </asp:View>
    </asp:MultiView>    
    </div>
    </form>

Code:

 Protected  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Request.QueryString("page") = "1" Then
                MultiView1.ActiveViewIndex = 0
            Else 
                MultiView1.ActiveViewIndex = 1
            End If
 End Sub
womp
Looks alot like C# while the person was asking about VB
RSolberg
Changed to VB. Hardly worth a downvote.
womp
And so the up-vote shall reflect such a change ;)
RSolberg
much appreciated :)
womp
It took me a while, but I changed<asp:MultiView ID="MultiView1" runat="server">to<asp:MultiView ActiveViewIndex="0" ID="MultiView1" runat="server">and it worked perfectly!Thank you so much for your help!
+1  A: 

Why not use different files instead? redirect to different pages. That would avoid having to have if statements everywhere.

OR

put your data in panels and just hide one or the other panel1.visible = (true/false). That's the best thing to do if you have to have it all in the same aspx page.

Eric
A: 

Hi there.

For future reference, you can still use the classic ASP way to control content. Here's an ASPX page I wrote just now:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<%
    if (3 == 9)
    {%>
    <span>Hello</span>
    <%
    }
    else
    {
    %> <span>What?</span > <%
    }

     %>   

    </div>
    </form>
</body>
</html>

When the page renders, it displays 'What?' on the page.

However, I would say that this is bad practise and poor design! Use either womp's suggestion of a multiview, or a page redirect.

Cheers. Jas.

Jason Evans
A: 

I prefer doing it on the ASPX page using DataBinding:

<asp:PlaceHolder runat="server" ID="Messages">
    <asp:Label runat="server" Visible=<%# Request.QueryString("page") = 1 %> Text="Message 1" />    
    <asp:Label runat="server" Visible=<%# Request.QueryString("page") <> 1 %> Text="Message 2"/>
</asp:PlaceHolder>

Then on the server side:

Protected  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)            
    Messages.DataBind()
End Sub
Sébastien Ros