Worgon
Worgon

Reputation: 1657

Web User Control postback Issues

I am new to ASP.net thing, and i have some question regarding postback.

I have a Senario like this:

1) I have a grid on web with a panel inside.

2) I "Insert" the panel with a Web User Control by calling this

 Control ctlControl;
 ctlControl = LoadControl("~/UserControls/ChequeCreation.ascx");
 pnlTransaction.Controls.Add(ctlControl);

3)The Web User Control providing two button. One is "update" and one is "reset".

Problem is like here:

What i wanted to achieve is when press the "update" button, it will update something back to my DB? But seem after i press the button "Update" or "Reset". The web user control is gone or missing. For my guest is because of the postback issues? Is that correct?

I tried if(!postback) still its doesn't work.

How am i going to overcome this? I already scratching my head about a day?

Thanks you so much.

Regards

LiangCk:

PS:Sorry for my english level, and please dont hesitate to voice out my error or mistake.

Upvotes: 1

Views: 2590

Answers (4)

Ashfaq Shaikh
Ashfaq Shaikh

Reputation: 1668

You have to Every Time Reload the user control on Page_Init or Page_Load. Then you can get the Button Click Event and After tha User Control will not lost.

private void LoadUserControl(){

   string controlPath = LastLoadedControl;

    if (!string.IsNullOrEmpty(controlPath)) {
        PlaceHolder1.Controls.Clear();
        UserControl uc = (UserControl)LoadControl(controlPath);
        PlaceHolder1.Controls.Add(uc);
    }
}

protected void Page_Load(object sender, EventArgs e) {  
   LoadUserControl();
}

Upvotes: 1

Alan Stephens
Alan Stephens

Reputation: 579

ASP.NET will not preserve a dynamically added user control between postbacks. This is why it is dissapearing. You will need to add the control each time the page is created. However you will need to add it when the control tree is being initialized and restore the original control ID if you want your events to fire. These links provide a full explanation https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx and http://avinashsing.sunkur.com/2011/02/24/dynamic-controls-viewstate-and-postback/

Upvotes: 1

Rashmi Kant Shrivastwa
Rashmi Kant Shrivastwa

Reputation: 1167

if you are using AJAX, try add updatepanel on your UCT design page

Upvotes: 1

Ali
Ali

Reputation: 595

well you can convert any of your data columns to template column and then drag and drop your web user control to it

this will result in something like the following code check where "uc1:webUserControle1" is located in the code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDB">
            <Columns>
                <asp:TemplateField HeaderText="ID" SortExpression="ID">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        <uc1:webUserControle1 ID="WebUserControle1_1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            </Columns>
        </asp:GridView>

Upvotes: 1

Related Questions