在Web用户控件中回发Dropdownlist,所有其他字段都变为空

时间:2011-12-12 15:41:13

标签: c# asp.net updatepanel

我有一个aspx页面,其中我在面板中有占位符

作为

<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>    

在aspx.cs页面上,我点击链接动态添加网页用户控件

并在添加按钮上单击新加载的用户控件

 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);
 }

我还在继续使用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);
            }
        }

当表单用户控件上有下拉列表时,我从用户控件中选择任何一个选项后发回,所有其他字段对于文本框等变为空。这是第一次和下次所有领域都保留了价值

请帮助

感谢

3 个答案:

答案 0 :(得分:1)

我通过每次动态加载控件时为控件分配相同的ID来完成此操作。这是因为当每次获取为我创建问题的新ID时动态添加控件

uc1.ID =&#34; Web_User_Control_ID&#34 ;;

每次控件加载时,将此ID设置为它都会解决问题

答案 1 :(得分:0)

尝试在Page.IsPostBack事件中将!Page.IsPostback更改为Page_Load

答案 2 :(得分:0)

动态添加的控件在帖子后面之间保持;您必须在每个回发后重新添加动态添加的控件。在每个页面请求中,asp.net将解析您的aspx文件,构建页面的html结构,然后通过ViewState和发布的值填写任何以前填入的值,然后提供它。而已。它永远不会自动重新添加动态添加的内容。

所以一旦你运行这段代码

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);
}

你需要以某种方式知道每次用户回发时重新运行它