0
votes

Okay I have some code for a website I am building. I have a listbox that is databound to an SqlDataSource. When selected, it is supposed to generate an SQL query that updates\filters a different listbox elsswhere in the page. I have tried testing it just to get it to change the text of a Label and that isn't even working. Here is some of my code:

    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> 

        <%--register triggers for Partial postback --%>
          <Triggers>
            <asp:AsyncPostBackTrigger ControlID="showbutton" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="viewapps" EventName="Click" />
          </Triggers>

          <ContentTemplate>
      <%--- the controls in their rows and columns --%>

            <%--column 1 --%>
     <asp:Label runat="server" ID="TESTER">Text</asp:Label>
     <asp:Panel runat="server" CssClass="column1">
         <asp:Panel ID="Panel1" runat="server" CssClass="row1">

             <%-- Make Panel --%>

                     <span style="padding:8px; position:relative;">
                         <asp:Label ID="label1" runat="server" Text="Make" Font-Size="Large" ></asp:Label>    
                         <asp:Listbox ID="MakeList" runat="server" Width="166px" SelectionMode ="Multiple" DataSourceID="MakeSource" DataTextField="MakeName" AutoPostBack="true"  DataValueField="MakeID" OnSelectedIndexChanged="UpdateModels">
                         </asp:Listbox>
                      </span>
                      <asp:SqlDataSource ID="MakeSource" runat="server" ConnectionString="<%$ ConnectionStrings:VCDBConnectionString %>" ></asp:SqlDataSource>

         </asp:Panel>

Now in my C# Codebehind I have this:

   public void UpdateModels(object sender, EventArgs e)
    {

        //build a string for a SQL query for the Models
        string newQuery = "SELECT M.[ModelID], M.[ModelName] FROM Model M INNER JOIN BaseVehicle BV ON BV.ModelID = M.ModelID Where BV.MakeID= '";
        string test = "";

        foreach (ListItem li in MakeList.Items)
        {
            //add each piece of the selected text to the string
            if (li.Selected)
            {

                test += li.Value;
                test += "' AND BV.MakeID= '";


            }

            int cleanup = test.LastIndexOf("' AND BV.MakeID= '");
            test.Remove(cleanup-19,cleanup);
            //ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", "alert('" + test + "');", true);
            TESTER.Text = test;

        }

But still tester is not being updated. Even though AutoPostBack=true, even though the whole thing is wrapped in an update panel. =( I need some help figuring this one out. I would also appreciate any other advice. I would be glad to provide any additional information you may need, just let me know in the comments.

1

1 Answers

0
votes

String.remove does not work the way I was thinking it did. This was causing a runtime exception that did not show up until I did debugging, which is why TESTER's text wasn't getting updated.

I was able to fix my problem a different way.

Instead of building the string immediately and trying tricky string operations, instead I just put each string into a list and then removed the last item of the last at the end. Then once I had the perfected list, then I added it to the string.

In code it looks like this:

      List<string> queryBuilder = new List<string>();

     //add the seleted items to items in the list
            foreach (ListItem li in MakeList.Items)
            {

                if (li.Selected)
                {
                    queryBuilder.Add(li.Value);                 
                    queryBuilder.Add("' OR BV.MakeID = '");


                    //build the list of selected makes for later use
                    selectedMakes.Add(li.Value);
                }
            }
                try
                {
                    //remove the last  ' AND BV.MakeID= '
                    queryBuilder.RemoveAt(queryBuilder.Count-1);

                   //add back the ' and the orderby
                    queryBuilder.Add("'");
                    queryBuilder.Add(" ORDER BY [ModelName]");

                    //build the string
                    foreach(string s in queryBuilder){

                        newQuery+= s;

                    }


                    //debug for visibilty 
                    TESTER.Text =newQuery;