[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Keep state of Dropdownlist

I have created three static dropdownlists to filter a dataset. The problem is that after the page reloads the dropdown switches back to its' initial value. What I need to do is when a user selects for example "Niveau = Beginners" in the first dropdown the value stays at "Beginners" in the dropdown after the page reloads.

Here is the code for the dropdowns:

<script language="vb" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not (Page.IsPostBack) Then
                ddlNiveau.Items.Add("niveau...")
                ddlNiveau.Items(0).Value = "overzicht.aspx?"
                ddlNiveau.Items(0).Selected = True
                ddlNiveau.Items.Add("beginners")
                ddlNiveau.Items(1).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=beginners" & "&doelgroep=" & Request("doelgroep") & "&cursus_type=" & Request("cursus_type")
                ddlNiveau.Items.Add("gevorderden")
                ddlNiveau.Items(2).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=gevorderden" & "&doelgroep=" & Request("doelgroep") & "&cursus_type=" & Request("cursus_type")
                        ddlNiveau.Items.Add("alle niveaus")
                ddlNiveau.Items(3).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & "&doelgroep=" & Request("doelgroep") & "&cursus_type=" & Request("cursus_type")
    End If
      
      If Not (Page.IsPostBack) Then
                ddlDoelgroep.Items.Add("doelgroep...")
                ddlDoelgroep.Items(0).Value = "overzicht.aspx?"
                        ddlDoelgroep.Items(0).Selected = True
                ddlDoelgroep.Items.Add("particulieren")
                ddlDoelgroep.Items(1).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & Request("niveau") & "&doelgroep=particulieren" & "&cursus_type=" & Request("cursus_type")
                ddlDoelgroep.Items.Add("bedrijven")
                ddlDoelgroep.Items(2).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & Request("niveau") & "&doelgroep=bedrijven" & "&cursus_type=" & Request("cursus_type")
                        ddlDoelgroep.Items.Add("alle doelgroepen")
                ddlDoelgroep.Items(3).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & Request("niveau") & "&doelgroep=" & "&cursus_type=" & Request("cursus_type")
    End If
      
            If Not (Page.IsPostBack) Then
                ddlCursusType.Items.Add("type cursus...")
                ddlCursusType.Items(0).Value = "overzicht.aspx"
                        ddlCursusType.Items(0).Selected = True
                ddlCursusType.Items.Add("zelfstudie")
                ddlCursusType.Items(1).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & Request("niveau") & "&doelgroep=" & Request("doelgroep") & "&cursus_type=zelfstudie"
                ddlCursusType.Items.Add("groepscursus")
                ddlCursusType.Items(2).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & Request("niveau") & "&doelgroep=" & Request("doelgroep") & "&cursus_type=groepscursus"
                        ddlCursusType.Items.Add("in-company-training")
                ddlCursusType.Items(3).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & Request("niveau") & "&doelgroep=" & Request("doelgroep") & "&cursus_type=in-company-training"
                        ddlCursusType.Items.Add("alle typen")
                ddlCursusType.Items(4).Value = "overzicht.aspx?" & "categorie=" & Request("categorie") & "&subcategorie=" & Request("subcategorie") & "&niveau=" & Request("niveau") & "&doelgroep=" & Request("doelgroep") & "&cursus_type="
    End If
      
End Sub
Sub ddlNiveau_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          Server.Transfer("overzicht.aspx")
End Sub
</script>
<script type="text/javascript">
     function UpdatePage(dropdown)
     {
          window.location=dropdown.options[dropdown.selectedIndex].value;
     }
</script>

....

<form id="filterDataset" method="post" runat="server">
<asp:dropdownlist ID="ddlNiveau" runat="server" Width="100px" OnChange="UpdatePage(this)" CssClass="textbox"></asp:dropdownlist>
<asp:dropdownlist ID="ddlDoelgroep" runat="server" Width="100px" OnChange="UpdatePage(this)" CssClass="textbox"></asp:dropdownlist>
<asp:dropdownlist ID="ddlCursusType" runat="server" Width="100px" OnChange="UpdatePage(this)" CssClass="textbox"></asp:dropdownlist>
</form>

Thanks in advance for the help!
0
Rineau
Asked:
Rineau
  • 8
  • 5
1 Solution
 
AGBrownCommented:
Your problem, I think, is related to the ddlNiveau_SelectedIndexChanged event handler. Inside this you Server.Transfer to overzicht.aspx. I assume that overzicht.aspx is the same page as you have posted the code for above? If that is the case, then on every selectedindexchanged you are reloading the page with Not IsPostback evaluating to True (it's a fresh page load, not a postback). This means that your ddls are getting refilled, and all your lines which say ddlX.Items(0).Selected = True are also being hit.

Instead of doing Server.Transfer, can you simply refill the dataset? This will mean that the page's ViewState is kept, which will keep the drop down selections.

Andy
0
 
RineauAuthor Commented:
Thanks Andy!

I have removed the ddlNiveau_SelectedIndexChanged part from the code and the page is still functioning. It might be a leftover from previous code I tried it with. The dropdowns still get refilled though.
I am a beginner to asp.net, so can you elaborate a bit on how to just refill the dataset and thus maintaining the ViewState?

Rein
0
 
strickddCommented:
Most likely you just need to place:

if(!Page.IsPostBack)
{
    //your code here
}

What this will do is make the DB call ONLY when the page loads the FIRST time. If you don't have this, the DB call will be made each time the page posts back (which happens when server-side code is executed) and the dropdown will be reset.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AGBrownCommented:
strickdd,

That's what he's got already in the Page_Load code; though this is in VB not C# so it's "If Not (Page.IsPostBack) Then" instead.

Rineau,

Looking closer at what you have, your drop down lists call UpdatePage(this). That appears to be a client-side javascript function which is causing the page to redirect to overzicht.aspx?<query string with details>. Again, this will not be a "postback" of the form, instead it will cause an Http GET on overzicht.aspx, which will cause you, in effect, to create a completely new page on the server-side, and fire of all the code inside the Not (Page.IsPostback) blocks again.

So there are two solutions. One would be to use the postback model of ASP.NET. This would associate an event handler with the selectedindexchanged event of the drop down lists - you have obviously already tried this as you have just deleted some code from that model with the ddlNiveau_SelectedIndexChanged part. There may, however be a reason that you want to redirect to a url instead, mainly for accessibility purposes. But by the looks of it you are using javascript to do that instead of another method, so it doesn't look like that matters.

The problem is that either model is going to require a rewrite of your code to a large extent. I'll start you off with the postback model, and you can hopefully fill in the blanks as we go. Make sure you backup your existing files before you start.

First of all you look like you are posting two query string variables to your page when it first loads; categorie and subcategorie. I assume that the page that redirects the user to here is therefore redirecting to something like http://www.foo.com/overzicht.aspx?categorie=x&subcategorie=y. If that is the case, we need to store these values in our page in order to use them in the postbacks. To do this I would create two private properties on the page which store the values in the ViewState. This means we can access them reliably on each postback without having to worry about our query string:

    Private Property Categorie() As String
        Get
            Return Me.ViewState.Item("categorie")
        End Get
        Set(ByVal Value As String)
            Me.ViewState.Item("categorie") = Value
        End Set
    End Property

    Private Property Subcategorie() As String
        Get
            Return Me.ViewState.Item("Subcategorie")
        End Get
        Set(ByVal Value As String)
            Me.ViewState.Item("Subcategorie") = Value
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '   Begin by assigning the categorie if this is a new request to the page
        If Not (Page.IsPostBack) Then
            Me.Categorie = Request("categorie")
            Me.Subcategorie = Request("subcategorie")

            '   Now do the ddl fills
            ...
        End If
    End Sub
   
You do the ddl fills in the gap where I've shown them above. However, they will change so that the value is only equal to the value you want to use, rather than an entire Url:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '   Begin by assigning the categorie if this is a new request to the page
        If Not (Page.IsPostBack) Then
            Me.Categorie = Request("categorie")
            Me.Subcategorie = Request("subcategorie")

            '   Now do the ddl fills
            ddlNiveau.Items.Add("niveau...")
            ddlNiveau.Items(0).Value = "None"
            ddlNiveau.Items(0).Selected = True
            ddlNiveau.Items.Add("beginners")
            ddlNiveau.Items(1).Value = "beginners"
            ddlNiveau.Items.Add("gevorderden")
            ddlNiveau.Items(2).Value = "gevorderden"
            ddlNiveau.Items.Add("alle niveaus")
            ddlNiveau.Items(3).Value = "All"

            '   Do the similar for the other ones
            ...
        End If
    End Sub
   
Now you can use the values in your event handlers for the postback. I will now assume that you have the code to filter the dataset based on the values in the query string that is being passed to you. Put this into a method that can be called from any of the event handlers so it might look like:

    Private Sub FilterDataSet()
        '   Do your dataset stuff here
        SomeAction(Me.Categorie, Me.Subcategorie, Me.ddlNiveau.SelectedValue, Me.ddlDoelgroep.SelectedValue, Me.ddlCursusType.SelectedValue)
    End Sub
As you can see, you can replace the SomeAction method with your code that uses the selected values of the drop down lists to filter your dataset, instead of using the Request("variable") values that you (presumable) currently use. Note that I have assigned the values of "None" and "All" to ddlNiveau in the ddl filling code, and you will have to test for those and use them appropriately to filter your dataset.
   
Now you can put your event handlers in place:
      Sub ddlNiveau_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Me.FilterDataSet()
      End Sub
      Sub ddlDoelgroep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Me.FilterDataSet()
      End Sub
      Sub ddlCursusType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Me.FilterDataSet()
      End Sub

The net workflow will be:
1) User gets redirected to http://www.foo.com/overzicht.aspx?categorie=x&subcategorie=y
2) Page stores the categorie and subcategorie values and fills the DropDownLists (first time only due to the Postback test)
3) Page fills the dataset and displays it (maybe in a grid?)
4) User changes a dropdownlist. Originally this would have redirected the page to another url causing a new page to be created, drop down lists to be filled etc. Now it posts the form back to the original page, and raises the appropriate event handler. This event handler calls FilterDataSet which refilters the dataset and displays it (posting back to the user).
5) User selects a new drop down value, go to step 4 (repeat ad-infinitum)

Does that help?

Andy
0
 
RineauAuthor Commented:
Thanks very much Andy for your extensive reply! (People should get paid hard cash for these quality answers :)

I think I understand what is happening, though I am having some trouble implementing the following part where the dataset is actually filtered.
   
    Private Sub FilterDataSet()
        '   Do your dataset stuff here
        SomeAction(Me.Categorie, Me.Subcategorie, Me.ddlNiveau.SelectedValue, Me.ddlDoelgroep.SelectedValue, Me.ddlCursusType.SelectedValue)
    End Sub

Here is the relevant code which I have now for this page: (btw: It is mainly Dreamweaver generated code, so the ASP.net part may be a bit of a mess.)

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls" Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral" %>
...
<MM:DataSet id="dsCURSUS" runat="Server" IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connCEOMSSQL") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_connCEOMSSQL") %>'
CommandText='<%# "SELECT *  FROM dbo.tblcursus  INNER JOIN dbo.tblcategorie  ON tblcursus.categorie = tblcategorie.categorie  WHERE subcategorie = @subcategorie and niveau LIKE @niveau and doelgroep LIKE @doelgroep and cursus_type LIKE @cursus_type" %>'
CurrentPage='<%# IIf((Request.QueryString("dsCURSUS_CurrentPage") <> Nothing), Request.QueryString("dsCURSUS_CurrentPage"), 0) %>'
PageSize="20"
Debug="true">
<Parameters>
   <Parameter  Name="@subcategorie"  Value='<%# IIf((Request.QueryString("subcategorie") <> Nothing), Request.QueryString("subcategorie"), "") %>'  Type="VarChar"   />
   <Parameter  Name="@niveau"  Value='<%# IIf((Request.QueryString("niveau") <> Nothing), Request.QueryString("niveau"), "%") %>'  Type="VarChar"   />
   <Parameter  Name="@doelgroep"  Value='<%# IIf((Request.QueryString("doelgroep") <> Nothing), Request.QueryString("doelgroep"), "%") %>'  Type="VarChar"   />
   <Parameter  Name="@cursus_type"  Value='<%# IIf((Request.QueryString("cursus_type") <> Nothing), Request.QueryString("cursus_type"), "%") %>'  Type="VarChar"   />
</Parameters>
</MM:DataSet>
...
<MM:PageBind runat="server" PostBackBind="true" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
...
<link href="css/layout.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/text.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/JavaScript" src="scripts/jumpmenu.js"></script>
<script language="vb" runat="server">
Private Property Categorie() As String
        Get
            Return Me.ViewState.Item("categorie")
        End Get
        Set(ByVal Value As String)
            Me.ViewState.Item("categorie") = Value
        End Set
    End Property

    Private Property Subcategorie() As String
        Get
            Return Me.ViewState.Item("subcategorie")
        End Get
        Set(ByVal Value As String)
            Me.ViewState.Item("subcategorie") = Value
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '   Begin by assigning the categorie if this is a new request to the page
        If Not (Page.IsPostBack) Then
            Me.Categorie = Request("categorie")
            Me.Subcategorie = Request("subcategorie")

            '   Now do the ddl fills
            ddlNiveau.Items.Add("niveau...")
            ddlNiveau.Items(0).Value = ""
            ddlNiveau.Items(0).Selected = True
            ddlNiveau.Items.Add("beginners")
            ddlNiveau.Items(1).Value = "beginners"
            ddlNiveau.Items.Add("gevorderden")
            ddlNiveau.Items(2).Value = "gevorderden"
            ddlNiveau.Items.Add("alle niveaus")
            ddlNiveau.Items(3).Value = ""
                  
                  ddlDoelgroep.Items.Add("doelgroep...")
            ddlDoelgroep.Items(0).Value = ""
            ddlDoelgroep.Items(0).Selected = True
            ddlDoelgroep.Items.Add("particulieren")
            ddlDoelgroep.Items(1).Value = "beginners"
            ddlDoelgroep.Items.Add("bedrijven")
            ddlDoelgroep.Items(2).Value = "gevorderden"
            ddlDoelgroep.Items.Add("alle niveaus")
            ddlDoelgroep.Items(3).Value = ""
                  
                  ddlCursusType.Items.Add("doelgroep...")
            ddlCursusType.Items(0).Value = "None"
            ddlCursusType.Items(0).Selected = True
            ddlCursusType.Items.Add("zelfstudie")
            ddlCursusType.Items(1).Value = "zelfstudie"
            ddlCursusType.Items.Add("groepscursus")
            ddlCursusType.Items(2).Value = "groepscursus"
            ddlCursusType.Items.Add("alle niveaus")
            ddlCursusType.Items(3).Value = ""
                  
        End If
    End Sub
      
    Sub ddlNiveau_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          Me.FilterDataSet()
    End Sub
    Sub ddlDoelgroep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          Me.FilterDataSet()
    End Sub
    Sub ddlCursusType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          Me.FilterDataSet()
    End Sub
      
      Private Sub FilterDataSet()
        SomeAction(Me.Categorie, Me.Subcategorie, Me.ddlNiveau.SelectedValue, Me.ddlDoelgroep.SelectedValue, Me.ddlCursusType.SelectedValue)
    End Sub

</script>
</head>
<body>
<div id="wrapper">
<!-- HEADER -->
...
<div id="content_container">
<!-- MAINCONTENT -->
...
<div id="filter">
<form id="filterDataset" runat="server">
<asp:dropdownlist ID="ddlNiveau" runat="server" Width="100px" CssClass="textbox"></asp:dropdownlist>
<asp:dropdownlist ID="ddlDoelgroep" runat="server" Width="100px" CssClass="textbox"></asp:dropdownlist>
<asp:dropdownlist ID="ddlCursusType" runat="server" Width="100px" CssClass="textbox"></asp:dropdownlist>
</form>
</div>

<div id="c_pagercontainer">
<div id="c_pages"><span class="t_pager">
<MM:If runat="server" Expression='<%# (dsCURSUS.CurrentPage <> 0) %>'>
<ContentsTemplate>
<a href="<%# Request.ServerVariables("SCRIPT_NAME") %>?categorie=<%# dsCURSUS.FieldValue("categorie", Container) %>&subcategorie=<%# dsCURSUS.FieldValue("subcategorie", Container) %>&dsCURSUS_currentPage=<%# Math.Max(dsCURSUS.CurrentPage - 1, 0) %>">[ vorige pagina ]</a></ContentsTemplate>
</MM:If>
<MM:If runat="server" Expression='<%# (dsCURSUS.CurrentPage < dsCURSUS.LastPage) %>'>
<ContentsTemplate><a href="<%# Request.ServerVariables("SCRIPT_NAME") %>?categorie=<%# dsCURSUS.FieldValue("categorie", Container) %>&subcategorie=<%# dsCURSUS.FieldValue("subcategorie", Container) %>&dsCURSUS_currentPage=<%# Math.Min(dsCURSUS.CurrentPage + 1, dsCURSUS.LastPage) %>">[ volgende pagina ]</a></ContentsTemplate>
</MM:If>
</span>
</div>
<div id="c_results"><span class="t_pager">Resultaten <strong><%= IIf((dsCURSUS.RecordCount = 0), 0, (dsCURSUS.StartRecord + 1)) %></strong> tot en met <strong><%= dsCURSUS.EndRecord %></strong> van <strong><%= dsCURSUS.RecordCount %></strong></span></div>
</div>

<ASP:Repeater runat="server" DataSource='<%# dsCURSUS.DefaultView %>'><ItemTemplate>
<div class="c_itemtemplate"><span class="c_items">
<div class="c_titel"><img src="images/frame/bullets/c_itemtemplate.gif" /><a href="<%# dsCURSUS.FieldValue("categorie", Container) %>/<%# dsCURSUS.FieldValue("subcategorie", Container) %>/details/<%# dsCURSUS.FieldValue("cid", Container) %>/<%# dsCURSUS.FieldValue("iid", Container) %>/<%# dsCURSUS.FieldValue("cursus_url", Container) %>.aspx"><%# dsCURSUS.FieldValue("cursus", Container) %></a></div>
<div class="c_instituut"><a href="instituut/<%# dsCURSUS.FieldValue("iid", Container) %>/details.aspx"><%# dsCURSUS.FieldValue("instituut", Container) %></a></div>
</span></div>
</ItemTemplate>
<alternatingitemtemplate>
<div class="c_altitemtemplate"><span class="c_items">
<div class="c_titel"><img src="images/frame/bullets/c_altitemtemplate.gif" /><a href="<%# dsCURSUS.FieldValue("categorie", Container) %>/<%# dsCURSUS.FieldValue("subcategorie", Container) %>/details/<%# dsCURSUS.FieldValue("cid", Container) %>/<%# dsCURSUS.FieldValue("iid", Container) %>/<%# dsCURSUS.FieldValue("cursus_url", Container) %>.aspx"><%# dsCURSUS.FieldValue("cursus", Container) %></a></div>
<div class="c_instituut"><a href="instituut/<%# dsCURSUS.FieldValue("iid", Container) %>/details.aspx"><%# dsCURSUS.FieldValue("instituut", Container) %></a></div>
</span></div>
</alternatingitemtemplate>
</ASP:Repeater>
<!-- FOOTER -->
</html>

I have the feeling I am still blending obsolete code with the code you gave me. I feel a bit burdened by asking, but can you point me in the right direction?
Greatly appreciated!
0
 
AGBrownCommented:
I think you are doing fine. I can't put this into Dreamweaver to test it, but I suspect that if you alter your parameters block, then you are pretty much there. First of all, take out the line which says SomeAction(...). Then leave it. Effectively, your postback event handlers for the drop down lists will then do nothing at all, but I'm working on the vague notion that the postback should cause the DataSet to refill (I don't know for sure as this is a dreamweaver control). The next bit is just to alter the parameters block:
<Parameters>
   <Parameter  Name="@subcategorie"  Value='<%# IIf((Request.QueryString("subcategorie") <> Nothing), Request.QueryString("subcategorie"), "") %>'  Type="VarChar"   />
   <Parameter  Name="@niveau"  Value='<%# IIf((Request.QueryString("niveau") <> Nothing), Request.QueryString("niveau"), "%") %>'  Type="VarChar"   />
   <Parameter  Name="@doelgroep"  Value='<%# IIf((Request.QueryString("doelgroep") <> Nothing), Request.QueryString("doelgroep"), "%") %>'  Type="VarChar"   />
   <Parameter  Name="@cursus_type"  Value='<%# IIf((Request.QueryString("cursus_type") <> Nothing), Request.QueryString("cursus_type"), "%") %>'  Type="VarChar"   />
</Parameters>

You want to replace the bit that says Request.QueryString("subcategorie") with Me.Subcategorie, and the Request.QueryString("niveau") etc. bits with the  Me.ddlNiveau.SelectedValue etc. statements.

If that doesn't work, then the FilterDataSet needs to force the dataset to refill itself. If it comes to this (I think it will) then we have a bit more to do, but let me know how you go just by removing the SomeAction line (keep the method) and changing the parameters.

Of course, now I wish we had gone with the other method, which involved letting you do the redirect, refilling the dropdownlists each time, and setting the selection in each drop down list based on the query string...if I had thought properly and realised you were using DreamWeaver maybe that would have been the better option. Let me know how you go with changing the parameter block etc..

A
0
 
AGBrownCommented:
Blimey, its hard work wading through the macromedia documentation ... especially when searching for dataset and pagebind in the dreamweaver 8 help gives me practically nothing :-S

I think that what I suggested will in fact work as you have a tag which says
<MM:PageBind runat="server" PostBackBind="true" />

This, as I understand it, should ensure that the page databinds the data controls on each postback. I would hope that that included refilling the dataset?
0
 
AGBrownCommented:
I'm becoming more confident that this will work, though not necessarily because of the pagebind tag, but because you haven't specified the Expression attribute of the dataset, which means that, as its default value is true, the dataset should fill on every postback. The PageBind tag then ensures that DataBind happens as well on each postback.

Let me know if you have any problems.

A
0
 
RineauAuthor Commented:
Hi Andy,

Thanks again for putting so much effort into this problem. I get the feeling I'm learning a lot from this, though I still can't get it to work.
I've implemented the changes to the code you suggested, though I get the following error:

BC30545: Property access must assign to the property or use its value. (<-- Translated from Dutch)
Line 118:    Sub ddlNiveau_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Line 119:          Me.FilterDataSet()       <---- THIS IS THE HIGHLIGHTED CODE
Line 120:    End Sub
Line 121:    Sub ddlDoelgroep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

It seems I still have to do something with the Me.FilterDataSet().
Besides that I am not sure if I set the parameter values correct. I also set AutoPostBack="true" for the dropdownlists.

I don't know if it's usefull but here is the compiler output and source and the page-code in text format:
http://www.cursusenopleiding.nl/temp/CompilerOutput.txt
http://www.cursusenopleiding.nl/temp/FullCompilationSource.txt
http://www.cursusenopleiding.nl/temp/Overzicht.aspx.txt

Hope you are able to see the problem!

Cheers,

Rein

0
 
AGBrownCommented:
Don't worry its not a problem; I expect it to take some effort to answer a question. I don't agree with just posting a link to an external article ;).

That last problem took a bit of looking at. Basically your form's id is "filterDataset", and you don't have a definition for a method called "FilterDataSet" (how unlucky was it that i chose the same name as your form!), so I think its getting confused about what its trying to do with the Me.FilterDataSet() line. I wouldn't worry too much, you could just set the event handlers to be empty:
    Sub ddlNiveau_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          'Me.FilterDataSet()
    End Sub
    Sub ddlDoelgroep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          'Me.FilterDataSet()
    End Sub
    Sub ddlCursusType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          'Me.FilterDataSet()
    End Sub

I have left the code inside them, but commented out, in case your IDE does something strange like trying to remove the event handlers once there is no code inside them. Does it work now?

Good call on the AutoPostBack, that saved us some time ;)

Andy
0
 
RineauAuthor Commented:
I think the problems lies within the parameters code.

I'm not sure whether the parameters should be written with the <%# %> delimiters, but I tried that option too, without any luck.
The page shows, but the dataset returns zero results. I have also tried it with setting the 'categorie' and 'subcategorie' paramaters to request the querystring like this:
Also here the Dataset returns no results and though I'm requesting querystring parameters here, the values don't show up (e.g. there is no 'categorie' and 'subcategorie' in the page title).

<Parameters>
  <Parameter  Name="@categorie"  Value='<%# IIf((Request.QueryString("categorie") <> Nothing), Request.QueryString("categorie"), "") %>'  Type="VarChar"   />
  <Parameter  Name="@subcategorie"  Value='<%# IIf((Request.QueryString("subcategorie") <> Nothing), Request.QueryString("subcategorie"), "") %>'  Type="VarChar"   />
  <Parameter  Name="@niveau"  Value='<%# Me.ddlNiveau.SelectedValue %>'  Type="VarChar"   />
  <Parameter  Name="@doelgroep"  Value='<%# Me.ddlDoelgroep.SelectedValue %>'  Type="VarChar"   />
  <Parameter  Name="@cursus_type"  Value='<%# Me.ddlCursusType.SelectedValue %>'  Type="VarChar"   />
</Parameters>


The relevant code I have now is:

<MM:DataSet id="dsCURSUS" runat="Server" IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connCEOMSSQL") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_connCEOMSSQL") %>'
CommandText='<%# "SELECT *  FROM dbo.tblcursus  WHERE categorie = @categorie AND subcategorie = @subcategorie and niveau LIKE @niveau and doelgroep LIKE @doelgroep and cursus_type LIKE @cursus_type" %>'
CurrentPage='<%# IIf((Request.QueryString("dsCURSUS_CurrentPage") <> Nothing), Request.QueryString("dsCURSUS_CurrentPage"), 0) %>'
PageSize="20" Debug="true">
<Parameters>
  <Parameter  Name="@categorie"  Value='<%# Me.Categorie  %>'  Type="VarChar"   />
  <Parameter  Name="@subcategorie"  Value='<%# Me.Subcategorie %>'  Type="VarChar"   />
  <Parameter  Name="@niveau"  Value='<%# Me.ddlNiveau.SelectedValue %>'  Type="VarChar"   />
  <Parameter  Name="@doelgroep"  Value='<%# Me.ddlDoelgroep.SelectedValue %>'  Type="VarChar"   />
  <Parameter  Name="@cursus_type"  Value='<%# Me.ddlCursusType.SelectedValue %>'  Type="VarChar"   />
</Parameters>
</MM:DataSet>
...
<script language="vb" runat="server">
Private Property Categorie() As String
        Get
            Return Me.ViewState.Item("categorie")
        End Get
        Set(ByVal Value As String)
            Me.ViewState.Item("categorie") = Value
        End Set
    End Property

    Private Property Subcategorie() As String
        Get
            Return Me.ViewState.Item("subcategorie")
        End Get
        Set(ByVal Value As String)
            Me.ViewState.Item("subcategorie") = Value
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '   Begin by assigning the categorie if this is a new request to the page
        If Not (Page.IsPostBack) Then
            Me.Categorie = Request("categorie")
            Me.Subcategorie = Request("subcategorie")

            '   Now do the ddl fills
            ddlNiveau.Items.Add("niveau...")
            ddlNiveau.Items(0).Value = ""
            ddlNiveau.Items(0).Selected = True
            ddlNiveau.Items.Add("beginners")
            ddlNiveau.Items(1).Value = "beginners"
            ddlNiveau.Items.Add("gevorderden")
            ddlNiveau.Items(2).Value = "gevorderden"
            ddlNiveau.Items.Add("alle niveaus")
            ddlNiveau.Items(3).Value = ""
                  
            ddlDoelgroep.Items.Add("doelgroep...")
            ddlDoelgroep.Items(0).Value = ""
            ddlDoelgroep.Items(0).Selected = True
            ddlDoelgroep.Items.Add("particulieren")
            ddlDoelgroep.Items(1).Value = "beginners"
            ddlDoelgroep.Items.Add("bedrijven")
            ddlDoelgroep.Items(2).Value = "gevorderden"
            ddlDoelgroep.Items.Add("alle niveaus")
            ddlDoelgroep.Items(3).Value = ""
                  
            ddlCursusType.Items.Add("doelgroep...")
            ddlCursusType.Items(0).Value = ""
            ddlCursusType.Items(0).Selected = True
            ddlCursusType.Items.Add("zelfstudie")
            ddlCursusType.Items(1).Value = "zelfstudie"
            ddlCursusType.Items.Add("groepscursus")
            ddlCursusType.Items(2).Value = "groepscursus"
            ddlCursusType.Items.Add("alle niveaus")
            ddlCursusType.Items(3).Value = ""
                  
        End If
    End Sub
      
    Sub ddlNiveau_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          'Me.FilterDataSet()
    End Sub
    Sub ddlDoelgroep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          'Me.FilterDataSet()
    End Sub
    Sub ddlCursusType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          'Me.FilterDataSet()
    End Sub
      
</script>
...
<form id="filterDataset" runat="server">
<asp:dropdownlist ID="ddlNiveau" runat="server" Width="100px" CssClass="textbox" AutoPostBack="true"></asp:dropdownlist>
<asp:dropdownlist ID="ddlDoelgroep" runat="server" Width="100px" CssClass="textbox" AutoPostBack="true"></asp:dropdownlist>
<asp:dropdownlist ID="ddlCursusType" runat="server" Width="100px" CssClass="textbox" AutoPostBack="true"></asp:dropdownlist>
</form>

Does this make any sense?
0
 
AGBrownCommented:
I'm struggling now. If you hard-code the parameters (i.e., put in values instead of the Me.xyz part) do you get anything? Does the dataset remain filled after postback? Are you able to use SQL profiler on your database to capture the web page -> database part? If the dataset is refreshing you should see your query go in and be able to check the values passed as the parameters.

Remember that you have an alternative to this if this is going to be a problem. You can go back to the very original model, only instead of doing ddlNiveau.Items(0).Selected = True (for instance) you can set the selected value based on the values in Request. The reason I didn't suggest it is because it is less efficient. However, it might solve your problem faster than you otherwise will be able to with this.
0
 
RineauAuthor Commented:
Hmm, getting closer. I played a bit with it and I have got it working except for the Dropdown where "doelgroep" is selected. As I leave that one out it works just fine, though I have no idea why that one is not working.
Btw, one last question: do you know how I can make it NOT to filter the records when the user selects no item in the dropdown?

Thanks again very much Andy for the excellent help!
0
 
AGBrownCommented:
No problem. I'm glad you got it working. I can't see why ddlDoelgroep isn't working though.

Given that your select query is using LIKE for niveau, doelgroep and cursos_type, you could assign the value % to the 0-index and 3-index items. "LIKE %" should return all items in that filter, for example for the ddlCursusType:
            ddlCursusType.Items.Add("doelgroep...")
            ddlCursusType.Items(0).Value = "%"
            ddlCursusType.Items(0).Selected = True
            ddlCursusType.Items.Add("zelfstudie")
            ddlCursusType.Items(1).Value = "zelfstudie"
            ddlCursusType.Items.Add("groepscursus")
            ddlCursusType.Items(2).Value = "groepscursus"
            ddlCursusType.Items.Add("alle niveaus")
            ddlCursusType.Items(3).Value = "%"

Andy
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now