2013-06-26 40 views
0

好吧我有一些我正在构建的网站的代码。我有一个数据绑定到SqlDataSource的列表框。选中时,它应该生成一个SQL查询,用于更新\筛选页面中其他列表框的其他列表框。我试过只是为了让它改变一个标签的文本,甚至没有工作。下面是我的一些代码:虽然autopostback为真,但Asp.net OnSelectedIndexChanged事件不能触发

<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> 

现在在我的C#代码隐藏我有这样的:

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; 

     } 

但还是测试仪没有被更新。即使AutoPostBack = true,即使整个事情被包装在更新面板中。 =(我需要一些帮助搞清楚这一个。 我也希望任何其他建议。 我将很高兴为您提供可能需要的任何额外的信息,只是让我知道了意见。

回答

0

String.remove做不工作的方式,我想它是这样的,这导致运行时异常,直到我没有显示出来,直到我调试,这就是为什么TESTER的文本没有得到更新。方式。

而不是立即建立串并试图棘手的字符串操作,而不是我只是把每个字符串转换成列表,然后除去最后一个项目最后的结尾。 然后,一旦我有完善的名单,然后我把它添加到字符串。

在代码中,它看起来是这样的:

 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; 
相关问题