Hi , I using popupcontrols with gridviews.
I want to bind the grids only when the popup is shown.
I have tried popupcontrol.load .init and .prerender but they all bind early.
Is there a way to only databind when the popup is actually shown?
thanks
ian
ASPxPopupControl - How to bind ASPxGridView inside popup on demand on first shown
Answers
Hello Ian,
You can set the ASPxPopupControl.LoadContentViaCallback property to "OnFirstShow". In this case, ASPxPopupControl will create its client-side content on demand.
>>Is there a way to only databind when the popup is actually shown?
No, there is not. Since ASPxGridView is a part of ASPxPopupControl and the entire ASP.NET page, all these controls go through the entire page life cycle during each round trip to the server.
Please refer to the ASP.NET Page Life Cycle MSDN article for more information.
Thanks,
Mike
Hello Ian,
I have prepared samples that illustrate possible ways of binding ASPxGridView on demand when the popup is shown for the first time:
- ContentCollection:
- Define ASPxGridView so that it does not have a datasource assigned;
- Show ASPxPopupControl on the client side;
- If ASPxPopupControl is shown for the first time, perform a custom popup callback and bind the embedded ASPxGridView control. Do not forget to bind the grid during each next round trip to the server, because the grid has been bound only once.
JavaScript<script type="text/javascript">
var gridIsBound = false;
function OnClick(s, e) {
pc.Show();
if(!gridIsBound) {
pc.PerformCallback('BindGrid');
gridIsBound = true;
}
}
</script>
ASPx<dx:ASPxButton ID="btn" runat="server" AutoPostBack="false" Text="Show Popup">
<ClientSideEvents Click="OnClick" />
</dx:ASPxButton>
<div>
<dx:ASPxPopupControl ID="popup" runat="server" AutoUpdatePosition="true" CloseAction="CloseButton"
ClientInstanceName="pc" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
OnWindowCallback="popup_WindowCallback">
<ContentCollection>
<dx:PopupControlContentControl runat="server" SupportsDisabledAttribute="True">
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="CategoryID"
OnDataBinding="grid_DataBinding">
<Columns>
<dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" ShowInCustomizationForm="True"
VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CategoryName" ShowInCustomizationForm="True"
VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Description" ShowInCustomizationForm="True"
VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
</dx:PopupControlContentControl>
</ContentCollection>
</dx:ASPxPopupControl>
</div>
<asp:AccessDataSource ID="ds" runat="server" DataFile="~/App_Data/nwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:AccessDataSource>
C#protected void Page_Load(object sender, EventArgs e) {
if(Session["BindGrid"] != null) {
grid.DataBind();
}
}
protected void popup_WindowCallback(object source, PopupWindowCallbackArgs e) {
ASPxPopupControl popupControl = (ASPxPopupControl)source;
switch(e.Parameter) {
case "BindGrid":
Session["BindGrid"] = true;
ASPxGridView gridView = (ASPxGridView)popupControl.FindControl("grid");
gridView.DataBind();
break;
default:
break;
}
}
protected void grid_DataBinding(object sender, EventArgs e) {
ASPxGridView gridView = (ASPxGridView)sender;
if(Session["BindGrid"] != null) {
gridView.DataSource = ds;
}
}
Visual BasicInherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Session("BindGrid") IsNot Nothing Then
grid.DataBind()
End If
End Sub
Protected Sub popup_WindowCallback(ByVal source As Object, ByVal e As PopupWindowCallbackArgs)
Dim popupControl As ASPxPopupControl = CType(source, ASPxPopupControl)
Select Case e.Parameter
Case "BindGrid"
Session("BindGrid") = True
Dim gridView As ASPxGridView = CType(popupControl.FindControl("grid"), ASPxGridView)
gridView.DataBind()
Case Else
End Select
End Sub
Protected Sub grid_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim gridView As ASPxGridView = CType(sender, ASPxGridView)
If Session("BindGrid") IsNot Nothing Then
gridView.DataSource = ds
End If
End Sub
- ContentUrl:
- It is a standalone implementation of the E2270 Code Central example: the ASPxGridView control is part of the external page's hierarchy. The grid's callbacks do not initialize the "host" page's life cycle.
Regards,
Mike
P.S. You can downgrade / upgrade the sample project I provided via the Project Converter tool. Take a look at the Upgrade Notes help topic to learn more about downgrade / upgrade procedures.
Hello Ian,
Thank you for your response. Please feel free to contact us at any time.
Thanks,
Mike
--------------------
Check if Search Engine is able to answer questions faster than I do!