【问题标题】:listview, datapager, objectdatasource not working in updatepanellistview、datapager、objectdatasource 在更新面板中不起作用
【发布时间】:2012-07-31 09:36:30
【问题描述】:

当我在 datapager 中更改页面时,它会导致完全回发,即使 datapager 工作非常异常,也不知道是什么原因。通过异常,我的意思是有时数据有时不显示,
以下是我在 c# 中的代码

     protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindListView();
        }           
    }

    protected void StudentListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        BindListView();
    }
    private void BindListView()
    {
        StudentListView.DataSource = StudentDataSource;
        StudentListView.DataBind();
    }

    protected void StudentDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.Arguments.MaximumRows = StudentListDataPager.MaximumRows;
        e.Arguments.StartRowIndex = StudentListDataPager.StartRowIndex;
    }

以下是我的标记

    <asp:UpdatePanel runat="server" UpdateMode="Conditional">
    <ContentTemplate>        <
    <asp:DropDownList runat="server" ID="BatchDropDownList" AutoPostBack="True">
        <asp:ListItem Text="Batch 1" Value="1" Selected="True" />
        <asp:ListItem Text="Batch 2" Value="2" />
    </asp:DropDownList>

    <asp:ListView ID="StudentListView" runat="server"       
        ItemPlaceholderID="ListViewContent" 
            EnableViewState="false" 
            onpagepropertieschanging="StudentListView_PagePropertiesChanging"> 
        <LayoutTemplate> 
            <table style="width:100%">
                <thead>
                    <tr>
                        <th class="align-left"><strong>Name</strong></th>
                        <th class="align-left"><strong>MobNo</strong></th>
                    </tr>
                </thead>
                <tbody runat="server" id="ListViewContent"> 
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3">

                        </td>
                    </tr>
                </tfoot>
            </table>

        </LayoutTemplate>  
        <ItemTemplate>  

            <tr>
                <td style="width:70%;"><%# Eval("FirstName") %>&nbsp<%# Eval("LastName") %></td>
                <td style="width:30%;"><%# Eval("MobNo") %></td>                    
            </tr>

        </ItemTemplate>             
    </asp:ListView>
    <asp:DataPager ID="StudentListDataPager" runat="server" PageSize="5" PagedControlID="StudentListView">
        <Fields>
            <asp:NumericPagerField />
        </Fields>
    </asp:DataPager>


    <asp:ObjectDataSource ID="StudentDataSource" runat="server" 
        SelectMethod="GetStudentListBatchWise" SelectCountMethod="StudentCount" 
            TypeName="SCERPCommonUtil.Student" EnablePaging="True" 
            onselecting="StudentDataSource_Selecting">
        <SelectParameters>
            <asp:ControlParameter Name="batchId" ControlID="BatchDropDownList" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:ObjectDataSource>
    </ContentTemplate>
</asp:UpdatePanel>

如果我犯了任何错误,请告诉我。

附: :请不要指出任何 stackoverflow 问题,因为我已经阅读了所有这些问题,并且没有一个特定于我的要求,

我面临的最重要的问题是,即使将datapager 放入updatepanel 内,也会发生同样的事情。

【问题讨论】:

  • 亲爱的,您已将 updatemode 指定为有条件的。您是否在更新面板中提到了回发触发器????
  • 好的 - 在StudentListView_PagePropertiesChanging() 事件中使用StudentListDataPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);,数据分页器现在可以正常工作,但更新面板的问题仍然是守护进程,这会导致完全回发,并且我丢失了所有我的 javascript 本地变量.

标签: c# asp.net updatepanel objectdatasource datapager


【解决方案1】:

亲爱的,您已将 updatemode 指定为有条件的。但是您已在更新面板中提到了任何触发器。 将 AsyncPostBackTrigger 添加到您的更新面板

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate> 
<Triggers>
            <asp:AsyncPostBackTrigger ControlID="StudentListView" EventName="PagePropertiesChanging" />
</Triggers>
<ContentTemplate>
</asp:UpdatePanel>

来自 MSDN: If the UpdateMode property is set to Conditional, and one of the following conditions occurs:

  1. 您显式调用 UpdatePanel 控件的 Update 方法。
  2. 回发是由使用 UpdatePanel 控件的 Triggers 属性定义为触发器的控件引起的。在这种情况下,控件显式触发面板内容的更新。该控件可以位于定义触发器的 UpdatePanel 控件内部或外部。
  3. ChildrenAsTriggers 属性设置为 true,UpdatePanel 控件的子控件会导致回发。嵌套的 UpdatePanel 控件的子控件不会导致对外部 UpdatePanel 控件的更新,除非它被明确定义为触发器。

更新答案: 为什么您没有在列表视图中提及 DataSourceID 属性???

 <asp:ListView ID="StudentListView" runat="server"  DataSourceID  ="StudentDataSource"

并且您正在将您的列表视图绑定在您的代码中作为

private void BindListView()
{
    StudentListView.DataSource = StudentDataSource; //also it should be DataSourceID 
    StudentListView.DataBind();
}

您正在使用对象数据源,并且在您的代码中将其绑定为错误的数据源。将其绑定为数据源 ID 或简单提及 listview 的 datasourceID 属性,然后您不必在后面的代码中绑定它。

【讨论】:

  • PagePropertiesChanging 事件与 ListView 相关联,而不与 DataPager 相关联,我也删除了 UpdateMode 并尝试过,但它仍然无法正常工作..
  • 对不起,我给了错误的 ID 来触发。您的代码在没有更新面板的情况下正常工作????检查一下
  • 抱歉,但您的建议不起作用,它仍在提供完整的回发。
  • 当然不用updatepanel也能正常工作,但我想用更新面板来实现ajax功能。
  • 两个方法都是正确的 - 在标记中设置 DataSourceId 或在后面的代码中设置 DataSource,但是 datapager 导致完全回发,这就是为什么我尝试了后面的代码方法(如在一些文章中所读到的那样)
【解决方案2】:

我想通了——万一有人来看——

我使用的是&lt;form runat="server"&gt;...&lt;form&gt; 标签,其中包含所有列表视图、数据分页器代码。当我将唯一的 id 属性分配给表单标签时,一切都开始正常工作。
虽然我不明白这种行为的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2017-01-31
    • 2010-10-13
    相关资源
    最近更新 更多