G.S Bhangal
G.S Bhangal

Reputation: 3280

post back of Dropdownlist in web user control all other fields become empty

i have a aspx page in which i have placeholder inside the panel

as

<div>
    <div>
        <div>
            <div>
                <div>
                    <div>
                        <asp:UpdatePanel runat="server" ID="UpdatePanelLinks">
                            <ContentTemplate>
                                <center>
                                    <asp:Button Text="ADD" ID="btnAdd" runat="server" Width="100" OnClick="btnAdd_Click" />&nbsp;
                                    <asp:Button Text="Edit" ID="btnEdit" runat="server" Width="100" OnClick="btnEdit_Click" />&nbsp;
                                    <asp:Button Text="Delete" ID="btnDelete" runat="server" Width="100" OnClick="btnDelete_Click" /></center>
                                <asp:LinkButton ID="usercontroldata" runat="server" Text="Branches" 
                                    OnClick="usercontrol_Click"></asp:LinkButton>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </div>
                </div>
                <div >
                    <asp:LinkButton ID="lnkDepartments" runat="server" Text="Departments" OnClick="lnkDepartments_Click"></asp:LinkButton>
                </div>
                <div >
                </div>
                <div >
                    <asp:LinkButton ID="lnkProjects" runat="server" Text="Projects" OnClick="lnkProjects_Click"></asp:LinkButton>
                </div>
                <div >
                </div>
                <div >
                    &nbsp;
                </div>                       
            </div>
            <div>
                <div>
                    <asp:Label Text="" Visible="false" ID="lblmessage" runat="server" />
                    <div>
                        <asp:UpdatePanel ID="UpdatePanel" runat="server">
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="lnkBranches" EventName="Click" />
                                <asp:AsyncPostBackTrigger ControlID="lnkDepartments" />
                                <asp:AsyncPostBackTrigger ControlID="lnkProjects" />
                                <asp:AsyncPostBackTrigger ControlID="btnAdd" />
                                <asp:AsyncPostBackTrigger ControlID="btnEdit" />
                                <asp:AsyncPostBackTrigger ControlID="btnDelete" />
                            </Triggers>
                            <ContentTemplate>
                                <asp:Panel runat="server" ID="MainPanel">
                                    <div class="padding_branch">
                                        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                                    </div>
                                </asp:Panel>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="lnkBranches" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="lnkDepartments" />
        <asp:AsyncPostBackTrigger ControlID="lnkProjects" />
        <asp:AsyncPostBackTrigger ControlID="btnAdd" />
        <asp:AsyncPostBackTrigger ControlID="btnEdit" />
        <asp:AsyncPostBackTrigger ControlID="btnDelete" />
    </Triggers>
    <ContentTemplate>
        <asp:Panel runat="server" ID="MainPanel">
            <div >
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>                                     
            </div>
        </asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>    

and on the aspx.cs page i am dynamically add the web user control on click of links

and on the add button click the new user control is loaded

 protected void usercontrol_Click(object sender, EventArgs e)
 {
        if (ViewState["controlname"] != null)
        {
            PlaceHolder pl = MainPanel.FindControl(ViewState["controlname"].ToString()) as PlaceHolder;
            if (pl.Controls.Count > 0)
            {
                pl.Controls.RemoveAt(0);
            }
        }

        Control uc = (Control)Page.LoadControl("~/usercontrol_Data.ascx");

        ViewState["path"] = "~/usercontrol_Data.ascx";
        ViewState["controlname"] = "PlaceHolder1";
        ViewState["name"] = "usercontrol";
        PlaceHolder1.Controls.Add(uc);
 }

i am also mainaining the web user control on Page_Load

        if (Page.IsPostBack)
        {
            if (ViewState["path"] != null)
            {
                Control uc1 = (Control)Page.LoadControl(ViewState["path"].ToString());

                PlaceHolder pl = MainPanel.FindControl(ViewState["controlname"].ToString()) as PlaceHolder;
                pl.Controls.Add(uc1);
            }
        }

and there is dropdown list on the form user control when iam selecting any of the option from the user control it post back and all the other fields become empty for textboxes etc . this happans for the first time and for next time all the fields retain there values

please help

thanks

Upvotes: 3

Views: 1379

Answers (3)

G.S Bhangal
G.S Bhangal

Reputation: 3280

i have done this by assigning the same ID to the control every time when the control loads dynamically. this is because when the control added dynamically every time it get the new ID that is creating the problem for me

uc1.ID="Web_User_Control_ID";

every time the control load asign this ID to it will resole the problem

Upvotes: 1

Adam Rackis
Adam Rackis

Reputation: 83356

Dynamically added controls are not persisted between post backs; you have to re-add your dynamically added controls on each post back. On each page request, asp.net will parse your aspx file, build the html structure of your page, then fill in any previously filled-in values via ViewState and posted values, then serve it up. That's it. It will never automatically re-add dynamically added content.

So once you run this code

protected void usercontrol_Click(object sender, EventArgs e)
{
    //...
    Control uc = (Control)Page.LoadControl("~/usercontrol_Data.ascx");

    ViewState["path"] = "~/usercontrol_Data.ascx";
    ViewState["controlname"] = "PlaceHolder1";
    ViewState["name"] = "usercontrol";
    PlaceHolder1.Controls.Add(uc);
}

you need to somehow know to re-run it every time the user posts back

Upvotes: 0

Kev Ritchie
Kev Ritchie

Reputation: 1647

Try changing Page.IsPostBack to !Page.IsPostback in your Page_Load event.

Upvotes: 0

Related Questions