【问题标题】:Event triggers method but doesn't update the page事件触发方法但不更新页面
【发布时间】:2013-06-28 19:27:29
【问题描述】:

所以我在更新面板中有很多列表框。第一个的 OnselectedIndexChanged 事件触发更新下一个的方法,依此类推。现在只有第一个列表框正确更新了第二个。第二个不会像它应该的那样更新第三个。

在调试时。我单击第二个 ListBox (ModelList),它会根据调试器正确触发事件。 C# 代码隐藏中的值正在更新,但更改不会像第一次那样显示在网页上。

例如有一个标签,我可以在其中看到它的文本属性在调试中的变化。但它不会改变网站。 另一个有趣的事情是,一旦单击第二个列表框,第一个列表框就会停止更新,但代码隐藏方法仍然会毫无例外地触发:-/

好了,说够了。这是我的代码。 (相关部分)。

ASP.NET 代码

    <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:Panel runat="server" CssClass="column1">

         <asp:Panel ID="Panel1" runat="server" CssClass="row1">

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


         </asp:Panel>

         <%--Model Panel --%>
          <asp:Panel ID="Panel2" CssClass="row2" runat="server">
              <span runat="server" style="padding:8px; position:relative;">
                    <asp:Label ID="label8" runat="server" Text="Model" Font-Size="Large" ></asp:Label>    
                    <asp:Listbox  ID="ModelList" runat="server" Width="166px" SelectionMode="Multiple" DataSourceID="ModelSource" OnSelectedIndexChanged="UpdateYear" AutoPostBack="true">
                    </asp:Listbox>
                    <asp:SqlDataSource ID="ModelSource" runat="server" ConnectionString="<%$ ConnectionStrings:VCDBConnectionString %>" > </asp:SqlDataSource>
             </span>
         </asp:Panel>

    </asp:Panel> <%--End of Column1 --%>


            <%-- column 3--%>
    <asp:Panel CssClass="column3" runat="server">

        <%--Year Panel --%>
        <asp:Panel ID="Panel7" CssClass="row1" runat="server">
           <span runat="server" style="padding:8px; position:relative;">
                <asp:Label ID="label13" runat="server" Text="Year" Font-Size="Large" ></asp:Label>    
                <asp:Listbox AutoPostback="true" ID="YearList" runat="server" Width="166px" SelectionMode="Multiple" DataSourceID="YearSource">
                </asp:Listbox>
                <asp:SqlDataSource ID="YearSource" runat="server" ConnectionString="<%$ ConnectionStrings:VCDBConnectionString %>"></asp:SqlDataSource>
           </span>
        </asp:Panel>

        <%--End of Content Template! --%>
        <%--Don't put any Dynmaic content past here! --%>

    </ContentTemplate>

 </asp:UpdatePanel>

还有 C# 代码隐藏。

  public partial class MassUpdate : System.Web.UI.Page
{


    //setup connection strings
  //  string VCDBconnect = ConfigurationManager.ConnectionStrings["VCDBConnectionString"].ConnectionString;
   // string ACESconnect = ConfigurationManager.ConnectionStrings["ACESConnectionString"].ConnectionString;


    //utility lists for building complex queries
    List<string> selectedMakes= new List<string>();
    List<string> selectedModels = new List<string>();
    List<string> selectedYears = new List<string>();
    List<string> selectedSubmodels = new List<string>();
    List<string> selectedEngines = new List<string>();
    List<string> selectedLocations = new List<string>();


    protected void Page_Load(object sender, EventArgs e)
    {

        if (this.IsPostBack == false) {


                  MakeSource.SelectCommand = "SELECT [MakeID], [MakeName] FROM [Make] ORDER BY [MakeName]";


            //setup the EVENTS
          //  MakeList.SelectedIndexChanged += UpdateModels;



        }//end of postback==false


    }//end of Page Load



  // called by selected index changed on Make
    public void UpdateModels(object sender, EventArgs e)
    {

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

        selectedMakes.Clear();
        //build a query into a list which will be compiled later into a single string
        List<string> queryBuilder = new List<string>();

        //add the base query
        queryBuilder.Add(baseQuery);

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

                }
                catch (ArgumentNullException) { TESTER.Text = "Argument Null"; }
                catch (IndexOutOfRangeException) { TESTER.Text = "Index out of range"; }
                catch (UpdateException) { TESTER.Text = "Update Problems"; }
                catch (Exception) { TESTER.Text = "Other Problems"; }


                ModelSource.SelectCommand = newQuery;
                ModelList.DataTextField="ModelName";
                ModelList.DataValueField = "ModelID";
            //    GroupList.Enabled = false;
            //    YearList.Enabled = false;



        }

    //called by onSelectedIndexchanged event from Model
    public void UpdateYear(object sender, EventArgs e)
    {

        TESTER.Visible = false;
        UpdatePanel1.Update();
        try
        {


                //empty it so it doesn't reuse old selections in future queries.
                selectedModels.Clear();

                //build a string for a SQL query for the Models
                //basic idea = SELECT [YearID] FROM [BaseVehicle] Where [YearID] >='1950' AND ([MakeID] = ''  ) AND ([ModelID] = '') ORDER BY [YearID]
                string baseQuery = "SELECT [YearID] FROM [BaseVehicle] Where [YearID] >='1950' AND (";
                string addOn = " ORDER BY [YearID]";
                string newQuery = "";


                //build a query into a list which will be compiled later into a single string
                List<string> queryBuilder = new List<string>();

                //add the base query
                queryBuilder.Add(baseQuery);

                //will need a for each loop for each  where clause group
                //will need one for each loop for buiilding the selected list for model
                //will need a final foreach loop to build the query


                //add the seleted items from the make list
                queryBuilder.Add("[MakeID] ='");
                foreach (string li in selectedMakes)
                {



                    queryBuilder.Add(li);
                    queryBuilder.Add("' OR [MakeID] = '");


                }


                //<----    cleanup area  ---->

                //remove the last  ' OR MakeID= '
                queryBuilder.RemoveAt(queryBuilder.Count - 1);

                //add the ' to close the last ID value
                queryBuilder.Add("'");


                //close the where clause group with a ) 
                queryBuilder.Add(") ");

                //<---- END   cleanup area  ---->

                //start the new where clause group
                queryBuilder.Add("AND ([ModelID ='");

                foreach (ListItem li in ModelList.Items)
                {


                    if (li.Selected == true)
                    {


                        //add the selected item's ID to the queryBuilder as a string
                        queryBuilder.Add(li.Value);
                        queryBuilder.Add("' OR [ModelID] = '");


                        //build selected model list for later use
                        selectedModels.Add(li.Value);

                    }


                }

                //<----    cleanup area  ---->

                //remove the last  ' OR ModelID = '
                queryBuilder.RemoveAt(queryBuilder.Count - 1);

                //add the ' to close the last ID value
                queryBuilder.Add("'");


                //close the where clause group with a ) 
                queryBuilder.Add(") ");

                //<---- END   cleanup area  ---->


                //add the addons(ending clauses such as ORDER BY)
                queryBuilder.Add(addOn);


                //Build the query!!!




                //build the string
                foreach (string s in queryBuilder)
                {
                    /*somehow there are 2 select statements that are built right here */
                    newQuery += s;

                }

                try
                {
                    //debug for visibilty 
                    TESTER.Text = newQuery;



                    YearSource.SelectCommand = newQuery;
                    YearList.DataTextField = "YearID";
                    YearList.DataValueField = "YearID";
                }

                catch(Exception k)
                {
                    TESTER.Text = "SQL small block fail " +k.ToString();

                }
            }
            catch (Exception j) {

                TESTER.Text = j.ToString();
        }
             //UpdatePanel1.Update();
    }//end of method

同样,这两种方法都根据 Debug 工作,但实际上只有 UpdateModels 会影响网页。我也不知道为什么。

如果有帮助,我首先在 Visual Studio Express for Web 2010 中制作了这个项目,然后切换到 Visual Studio 2012 Express for Web。我的项目有一个更新/转换过程。如果这与它有关,请告诉我。

此外,我愿意应要求发布任何其他源代码。例如,如果您认为 web.config 的内容是相关的。我很乐意发布它。

【问题讨论】:

  • 当我明天(星期一)回去工作时,我会尝试将 ChildrenAsTriggers 属性设置为 true。
  • 好吧,那没用。这可能是一个框架错误吗?更新面板可以拥有的孩子的最大数量是多少?

标签: c# asp.net web-applications updatepanel postback


【解决方案1】:

你的 UpdatePanel1.Update() 在你的 UpdateYear 函数的底部被注释掉了——你试过取消注释吗?

【讨论】:

    【解决方案2】:

    您在YearList 上缺少OnSelectedIndexChanged

    【讨论】:

    • Yearlist 由 ModelList 的 OnSelectedIndexChanged="UpdateYear" 调用的 UpdateYear() 填充。不过你是对的,YearList 缺少一个 onSelectedIndexChanged。但是将其设置为方法并不能解决问题,我已经测试过 =\
    【解决方案3】:

    不确定,但UpdatePanel1.Update(); 应该是UpdateYear 函数的最后一行。你知道为什么,不是吗?

    【讨论】:

    • 您可能会发现在回复之前阅读之前的答案是值得的。我已经和 Cortright 谈过了。
    • 如果您滚动到 UpdateYear 函数的顶部,您会看到我将 TESTER 的可见属性设置为 false 的部分,然后执行 UpdatePanel1.Update(); .问题是即使在调用 UpdatePanel 的 Update() 方法后,网页上仍然可以看到 TESTER。这就是为什么我很困惑并来到 StackOverFlow 寻求帮助的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多