Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 排序列表视图_C#_Asp.net_Listview_Data Binding - Fatal编程技术网

C# 排序列表视图

C# 排序列表视图,c#,asp.net,listview,data-binding,C#,Asp.net,Listview,Data Binding,我有一个要排序的列表视图。 我的问题是,当我单击排序列时,Onsorting事件仅在我再次绑定pageload上的数据时才会触发。 这意味着在每次页面加载时,我首先必须绑定数据,然后才能捕获OnBinding事件,然后才能重新绑定数据。 有没有更好的办法。基本上,我想要的是仅在onsorting事件中绑定数据 <asp:ListView ID="TempList" runat="server" OnSorting="TempList_sorting"> <LayoutT

我有一个要排序的列表视图。 我的问题是,当我单击排序列时,Onsorting事件仅在我再次绑定pageload上的数据时才会触发。 这意味着在每次页面加载时,我首先必须绑定数据,然后才能捕获OnBinding事件,然后才能重新绑定数据。 有没有更好的办法。基本上,我想要的是仅在onsorting事件中绑定数据

<asp:ListView ID="TempList" runat="server" OnSorting="TempList_sorting">
    <LayoutTemplate>
        <table >
            <tr>
                <th >
                    <asp:LinkButton runat="server" ID="btnSortVoorletters2" CommandName="Sort" Text="Voorletters"
                        CommandArgument="Voorletters" OnClick="btnSortVoorletters_Click" />
                </th>
            </tr>
              <tr runat="server" id="itemPlaceholder">
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("Naam") %>'/>
            </td>
        </tr>
    </ItemTemplate>
    <EmptyDataTemplate> 
    <p>Empty text that will be displayed.</p> 
</EmptyDataTemplate>

将显示的空文本。


您总是在
页面加载中绑定,这样以前提供的订单就会丢失。仅当
IsPostback
为false时绑定。(需要将ListView
EnableViewState
设置为true,这是默认值)

当用户单击“排序”按钮时,将触发事件,您的事件将相应地对数据进行排序和绑定

如果页面被重新加载,并且您没有在
PageLoad
中重新绑定它,这就足够了

但是,如果出于某种原因确实需要在
PageLoad
中重新绑定,那么您需要做的是将排序存储在
ViewState
、一个
HiddenField
Session
中,以便在将数据绑定到
PageLoad
中之前,从中获取排序数据的值


您应该获取排序并绑定到新方法的代码,并从排序和
PageLoad
事件调用它。

您好,谢谢,当我将viewstate设置为启用状态时,它可以工作。唯一的问题是,该客户机不希望启用viewstate,因为它可以拥有很大的容量。因为这个原因禁用viewstate不是很常见吗/我一直认为您不应该使用它,谢谢您的回复
 if(!IsPostback)
 {
   // do the binding
 }