Test.aspx
<%@ Register Src="~/WebUserControl.ascx" TagName="GridView" TagPrefix="uc" %>
<uc:GridView ID="TestGridView" runat="server"/>
Test.aspx.cs
1. Replaced Page_LoadComplete with Page_LoadCompleted or any other names. Buttom line is that the databinding must not be inside LoadComplete event.
e.g: protected void MyDataBindHandler(object sender, EventArgs e)
{
}
2. Create a Page_Init handler and attach the MyDataBindHandler to the usercontrol load event. this is to make sure that the binding happens right after adding of columns in gridview.
e.g: protected void Page_Init(object sender, EventArgs e)
{
this.TestGridView.Load += this.MyDataBindHandler;
}
WebUserControl.ascx
<asp:Button ID="Button1" runat="server" Text="ClickMe" OnClick="Button1_Click"/>
<asp:GridView ID="CommonGridView" runat="server"></asp:GridView>
WebUserControl.ascx.cs
1. In Page_Load handler call the method to add the columns in the gridview
e.g: protected void Page_Load(object sender, EventArgs e)
{
this.ConfigureGridColumn();
}
2. Get handle the click event
Sequence is that
* One first load
Default8.aspx Page_Init -> WebUserControl.ascx Page_Load -> Default8.aspx MyDataBindHandler
* On Button click
Default8.aspx Page_Init -> WebUserControl.ascx Page_Load -> Default8.aspx MyDataBindHandler -> Button1_Click
thanks to Vivek Thakuri got this info:
If you create a dynamic control in the Page_Load event, and add it to a PlaceHolder or Panel (with view state turned on), then this dynamic control will maintain its state even though it was not created in the Page_Init().
i have updated the question and added a solution section with codes. note that the code is dirty just to address the issue but can be improved.