现在我想在一个default.aspx页面中设置一个button和 加载多个自定义的usercontrol控件当click这个button的时候来动态的局部刷新,load不同的usercontrol我想问问各位高手啊,这样做可以不可以啊?
应该怎样实现这样的效果呢?主要就是要求不能刷新页面
多谢了。

解决方案 »

  1.   

    应该是可以做到的,
    建两个UpdatePanel:UpdatePanel1;UpdatePanel2,
    在UpdatePanel1里放TextBox1和Button1;//TextBox1的值为1;
    在UpdatePanel2里放TextBox2和Button2;//TextBox2的值为2;分别设置UpdatePanel1,UpdatePanel2的UpdateMode为Conditional
    UpdatePanel1的Triggers为Button1的Click;
    UpdatePanel2的Triggers为Button2的Click;在Page_Load事件中
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                TextBox1.Text = "aaa";
                TextBox2.Text = "bbb";
            }
        }
    点击Button1,你可以看到TextBox1的值改边为aaa,而TextBox2的值还是为2,没改变!!
    同理点击Button2,改变TextBox2的值,而不改变TextBox1的值具体代码:
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                    <asp:TextBox ID="TextBox1" runat="server">1</asp:TextBox>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Button ID="Button2" runat="server" Text="Button" />
                    <asp:TextBox ID="TextBox2" runat="server">2</asp:TextBox>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
      

  2.   

    注意了,正解:default.aspx:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
            <div style="width: 150px; float: left;">
                <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick">
                    <Items>
                        <asp:MenuItem Text="WebUserControl1" Value="WebUserControl1.ascx"></asp:MenuItem>
                        <asp:MenuItem Text="WebUserControl2" Value="WebUserControl2.ascx"></asp:MenuItem>
                        <asp:MenuItem Text="WebUserControl3" Value="WebUserControl3.ascx"></asp:MenuItem>
                    </Items>
                </asp:Menu>
            </div>
            <atlas:UpdatePanel ID="UpdatePanel1" runat="server" Mode="Conditional">
                <Triggers>
                    <atlas:ControlEventTrigger ControlID="Menu1" EventName="MenuItemClick" />
                </Triggers>
                <ContentTemplate>
                    <div style="float: left; margin-left: 10px; width: 300px; height: 300px;">
                        <asp:PlaceHolder ID="PlaceHolder1" runat="server" EnableViewState="false"></asp:PlaceHolder>
                    </div>
                </ContentTemplate>
            </atlas:UpdatePanel>
        </form>
    </body>
    </html>
    default.aspx.cs:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
    {
        protected string ControlType
        {
            get
            {
                if (ViewState["ControlType"] != null)
                {
                    return ViewState["ControlType"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["ControlType"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            if (!e.Item.Value.Equals(ControlType))
            {
                PlaceHolder1.Controls.Clear();
                ControlType = e.Item.Value;
                ITemplate m_template = Page.LoadTemplate(ControlType);
                m_template.InstantiateIn(PlaceHolder1);
                //PlaceHolder1.Controls.Add(Page.LoadControl(ControlType));
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (!string.IsNullOrEmpty(ControlType))
            {
                PlaceHolder1.Controls.Clear();
                ITemplate m_template = Page.LoadTemplate(ControlType);
                m_template.InstantiateIn(PlaceHolder1);
                //PlaceHolder1.Controls.Add(Page.LoadControl(ControlType));
            }
        }
    }WebUserControl1.ascx:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="WebUserControl1" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />WebUserControl1.ascx.cs:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Button1.Text = TextBox1.Text;
        }
    }WebUserControl2.ascx:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl" %>
    <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>WebUserControl2.ascx.cs:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            Label1.Text = Calendar1.SelectedDate.ToString();
        }
    }WebUserControl3.ascx:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl3.ascx.cs" Inherits="WebUserControl3" %>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
    </asp:DropDownList>
    <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>WebUserControl3.ascx.cs:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class WebUserControl3 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox1.Items.Add(DropDownList1.SelectedValue);
        }
    }
      

  3.   


    atlas ????
    必须用啊?
    用AJAX不行么?
      

  4.   

    这是老版本的一个demo,你用新的asp.net ajax版本自然是asp:UpdatePanel这个demo是老早从网上抠下来的,效果不错,缺点是灵活度不高,但貌似完全符合lz的需求
      

  5.   


    多谢了我不是想这样啊按着你的思路,我是想:
    最初页面上有一个总的button,然后只有TextBox1和Button1
    当我点击button的时候,把下面的TextBox1和Button1换成其他的控件来显示
    比如checkbox,dropdownlist等等所以就像我说的,把TextBox1和Button1做成一个usercontrol,把checkbox,dropdownlist等等做成另外一个usercontrol
    点击总的button的时候
    能不能来切换usercontrol的加载呢?
      

  6.   

    不论你控件在什么地方,想触发多个updatapanel更新都是可以的,trrigger,6楼的差不多是正解,同意答案生效,
      

  7.   

    主要参考一下陈黎夫asp.net ajax程序设计 第1卷,前四章就可以,
      

  8.   

    可以倒是可以,只是你要注意ViewState的使用,因为动态加载用户控件如果视图状态弄不好会发生很多莫名其妙的错误,如果需要加载的用户控件不是很多的话,建议你尝试一下MultiView!