Listview面板未在更新面板

时间:2016-05-09 16:48:45

标签: c# html asp.net asp.net-ajax updatepanel

您好我在更新面板内的listview元素上显示数据时遇到问题。没有更新面板,信息显示完全正常。我正在使用更新面板根据DropDown元素的选定值对数据进行排序。

代码:

<asp:DropDownList ID="sortdropdown" AutoPostBack="true" runat="server" Enabled="true" OnSelectedIndexChanged="SelectedIndexChanged">
  <asp:ListItem>Most Recent</asp:ListItem>
  <asp:ListItem>Price: Rs Low to High</asp:ListItem>
  <asp:ListItem>Price: Rs High to Low</asp:ListItem>
</asp:DropDownList>

<asp:ScriptManager ID="MyScriptManager" runat="server" />

<asp:SqlDataSource ID="posts" runat="server" ConnectionString="<%$ ConnectionStrings:ElmTreeConnect %>">
</asp:SqlDataSource>



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


    <asp:ListView ID="userpostslistview" runat="server" DataKeyNames="ID" DataSourceID="posts">


      <EmptyDataTemplate>
        <span>No data was returned.</span>
      </EmptyDataTemplate>

      <ItemTemplate>



        <div class="col-md-11 pad2 lead">

          <ul class="list">
            <a>                      
                                  <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "single.aspx?postID="+Eval("ID") %>'>
                                  <li>
    									<asp:Image ID="adimage" runat="server" ImageUrl='<%#"../files/" + Eval("adphoto") %>' />
    									<section class="list-left">
    									<h5 class="title"><asp:Label Text='<%# Eval("adtitle") %>' runat="server" ID="adtitlelabel" /></h5>
    									<span class="adprice"><asp:Label Text='<%# "Price: £" + Eval("adprice", "{0:n2}") %>' runat="server" ID="adpricelabel" /></span>
    									<p class="catpath"><asp:Label Text='<%# "Category: " + Eval("category") %>' runat="server" ID="adcategorylabel" /></p>
    									</section>
    									<section class="list-right">
    									<span class="date"><asp:Label Text='<%# "Date uploaded: " + Eval("uploadeddate", "{0:d}") %>' runat="server" ID="uploadeddate" /></span>
    									<span class="cityname"></span>
    									</section>
    									<div class="clearfix"></div>
                                 </li>
                                  </asp:HyperLink></a>
          </ul>
        </div>
      </ItemTemplate>
      <LayoutTemplate>

        <div runat="server" id="itemPlaceholderContainer" class="container">
          <span runat="server" id="itemPlaceholder" />
        </div>
        <div class="pad">
          <asp:DataPager runat="server" ID="DataPager1" PageSize="3">
            <Fields>
              <asp:NextPreviousPagerField ButtonType="Button" ButtonCssClass="btn btn-default" ShowFirstPageButton="True" ShowLastPageButton="True"></asp:NextPreviousPagerField>
            </Fields>
          </asp:DataPager>

        </div>
      </LayoutTemplate>

    </asp:ListView>
  </asp:ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="sortdropdown" EventName="SelectedIndexChanged" />
  </Triggers>
</asp:UpdatePanel>

在我的代码后面我正在使用if来检查下拉列表的值,然后根据需要更改我的Select语句(我有另外一个原因是我已将其设置为显示所有类别,如果查询字符串id相等到0):

    protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString = WebConfigurationManager.ConnectionStrings["ElmTreeConnect"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(connectionString);

        //myConnection.ConnectionString is now set to connectionString.
        myConnection.Open();

        string urlquery = Request.QueryString["categorytype"];
        int queryid = Convert.ToInt32(urlquery);

        string query = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID WHERE userpost.categoryID =" + queryid;

        string query1 = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID";

        string mostrecent = " ORDER BY userpost.uploadeddate DESC";

        if (queryid != 0)
        {

                posts.SelectCommand = query += mostrecent;
                posts.DataBind();
                updatepanel.Update();

        }
        else
        {

            posts.SelectCommand = query1;
                posts.DataBind();
                updatepanel.Update();

        }

        myConnection.Close();
    }
    protected void SelectedIndexChanged(object sender, EventArgs e)
    {

        string urlquery = Request.QueryString["categorytype"];
        int queryid = Convert.ToInt32(urlquery);

        string query = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID WHERE userpost.categoryID =" + queryid;

        string query1 = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID";

        string mostrecent = " ORDER BY userpost.uploadeddate DESC";
        string lowtohigh = " ORDER BY userpost.adprice DESC";
        string hightolow = " ORDER BY userpost.adprice ASC";


        if (queryid != 0)
        {
            if (sortdropdown.SelectedValue == "0")
            {
                posts.SelectCommand = query += mostrecent;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "1")
            {
                posts.SelectCommand = query += lowtohigh;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "2")
            {
                posts.SelectCommand = query += hightolow;
                posts.DataBind();
                updatepanel.Update();
            }
        }
        else
        {
            if (sortdropdown.SelectedValue == "0")
            {
                posts.SelectCommand = query1 += mostrecent;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "1")
            {
                posts.SelectCommand = query1 += lowtohigh;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "2")
            {
                posts.SelectCommand = query1 += hightolow;
                posts.DataBind();
                updatepanel.Update();
            }
        }


    }

}

select语句从后面的代码绑定到sqldatasource。

任何人都可以看到我的错误吗?我无法理解为什么列表视图在更新面板中包含时不会显示信息。

非常感谢任何帮助!

0 个答案:

没有答案