簡體   English   中英

listview,datapager,objectdatasource在updatepanel中不起作用

[英]listview, datapager, objectdatasource not working in updatepanel

當我在datapager中更改頁面時,它會導致完全回發,即使datapager的工作也非常不正常,idont也知道是什么原因。 通過異常,我的意思是有時顯示數據有時不顯示,
以下是我在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>

如果我有任何錯誤,請告訴我。

PS:請不要將我指向任何stackoverflow問題,因為我已經閱讀了所有問題,但沒有一個是我的特定要求的,

我面臨的最重要的問題是,即使將datapager放在updatepanel內,也會發生完全的回發。

尊敬的您已將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屬性,則不必在后面的代碼中綁定它。

我想通了-如果有人來找-

我使用的是<form runat="server">...<form>標記,其中包含所有此listview,datapager代碼。 當我為表單標簽分配了唯一的id屬性時,一切都開始正常工作。
雖然我不明白這種行為的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM