简体   繁体   English

用户控件内部的可排序DataGrid

[英]Sortable DataGrid inside User Control

I need to implement sortable DataGrid inside User Control. 我需要在用户控件内实现可排序的DataGrid。

My main page code behind looks like this: 我的主页代码如下:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    myPlaceHolder.Controls.Add(page.LoadControl("~/myControl.ascx") as MyCtrl);
}

User Control looks like this: 用户控件如下所示:

<asp:DataGrid ID="myGrid" runat="server" Width="100%" AutoGenerateColumns="False"
    AllowSorting="True" OnSortCommand="Grid_Sort" EnableViewState="true" >
        <Columns>
            <asp:BoundColumn DataField="Clmn1" SortExpression="" HeaderText="" />
            <asp:BoundColumn DataField="Clmn2" SortExpression="Clmn2" HeaderText="Clmn2header" />
        </Columns>
    </asp:DataGrid>

So I faced following problem: when I click on automatic generated link button Clmn2header to sort data, it cause the main page to reload (not post back) and hence to create new user control. 所以我遇到了以下问题:当我单击自动生成的链接按钮Clmn2header对数据进行排序时,它将导致重新加载主页(而不是回发),从而创建了新的用户控件。 So post back never occure and sorting method Grid_Sort is never fired. 因此,永远不会发回邮件,并且永远不会触发Grid_Sort排序方法。

I sure that ViewState is enabled. 我确定ViewState已启用。

Help me, please. 请帮帮我。 What have I done wrong? 我做错了什么? Thank you. 谢谢。

EDIT 编辑

I just simplify, actually, user control initializing looks like this: 实际上,我只是简化了用户控件的初始化,如下所示:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    myPlaceHolder.Controls.Add(MyCtrl.createInstance(/*some params*/));
}

And crete instance method: 和克里特实例方法:

public static MyCtrl createInstance(/*some params*/)
{
    MyCtrl ctrl = page.LoadControl("~/myControl.ascx") as MyCtrl;
    ctrl._init(/*some params*/);
    return ctrl;
}

private void _init(/*some params*/)
{
    /*setting controls properties with params*/
}

What is MyCtrl ? 什么是MyCtrl? why casting as you are using loadcontrol method ? 为什么在使用loadcontrol方法时进行铸造? below line should be suffice to instantiate your control, 下面的行足以实例化您的控件,

myPlaceHolder.Controls.Add(page.LoadControl("~/myControl.ascx"));

Did you put IsPostBack() checkpoint in your page_load event of the page & control, i am quite sure that when you click on column headertext link, there should not be full page postback. 您是否将IsPostBack()检查点放在页面和控件的page_load事件中,我很确定当您单击列标题文本链接时,应该没有完整的页面回发。

if (!IsPostBack) { }

let me know if your sort command event is not hit still. 让我知道您的排序命令事件是否仍未击中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM