我将用户控件添加到占位符。当fire按钮任何用户控件放置持有人而不是消防代码

时间:2015-08-20 01:06:34

标签: c# asp.net

我将uc添加到占位符时使用了用户控件和占位符。计数占位符为0.然后单击uc上的按钮不会触发任何代码。所有观点都没问题,我也不知道发生了什么。

我用这个类来添加用户控件:

 public abstract class MasterControl : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;

        public void Initial(bool postback =true)
        {

            this.CurrenetContorl = (BaseUserControl)this.LoadControl(this.CurrentPath);
            this.CurrenetContorl.PostBack = this.FristLoad ? !FristLoad : postback;
            this.CurrenetContorl.PreviousControl = this.PreviousControl;
            this.CurrenetContorl.CurrentControl = this.CurrentPath;
            if (!this.PlaceHolder1.Controls.OfType<BaseUserControl>().Any( x=> x.ID.Equals(this.CurrenetContorl.ID)))
            {
                this.PlaceHolder1.Controls.Add(this.CurrenetContorl);
                this.FristLoad = false;

            }
        }

        private BaseUserControl _currentcontrol;

        public BaseUserControl CurrenetContorl
        {
            get { return _currentcontrol; }
            set { 
                _currentcontrol = value;
                if (_currentcontrol == null) return;
                _currentcontrol.Redirecting += CurrentControlRedirecting;
                _currentcontrol.ID = this.GetId;

            }
        }
        private bool FristLoad = false;
        void CurrentControlRedirecting(string url)
        {
           var ctrl= this.PlaceHolder1.Controls.OfType<BaseUserControl>().FirstOrDefault();
           if (ctrl != null)
           {
               this.PlaceHolder1.Controls.Remove(ctrl);
               this.PreviousControl = ctrl.GetType().Name;

           }
           this.CurrnetUrl = url;
           this.Initial(false);
        }

        public string CurrnetUrl
        {
            get
            {
                return this.ViewState["currentUrl"].ToString();
            }
            set
            {
                var temp = "";
                if (value.StartsWith("/"))
                {
                    temp = "http://"+Request.Url.Authority +value;
                }
                this.ViewState["currentUrl"] = temp;
                this.CurrentPath = this.GetPath();
            }
        }

        private string GetPath()
        {
            var uri = new  Uri(this.CurrnetUrl);
            return uri.AbsolutePath;
        }
        public string PreviousControl
        {
            get
            {
                if (this.ViewState["_previousControl"] == null)
                    this.ViewState["_previousControl"] = string.Empty;
                return this.ViewState["_previousControl"].ToString();
            }
            set
            {
                this.ViewState["_previousControl"] = value;
            }
        }

        protected string CurrentPath
        {
            get
            {
                if (this.ViewState["_currentpath"] == null)

                    this.ViewState["_currentpath"] = string.Empty;

                return this.ViewState["_currentpath"].ToString();
            }
            set
            {
                this.ViewState["_currentpath"] = value;
            }
        }
        private string GetId
        {
            get
            {
                if (this.ViewState["ControlId"] == null)
                    this.ViewState["ControlId"] = Guid.NewGuid();
                return this.ViewState["ControlId"].ToString();
            }
        }

        public void Redirect(string url)

        {
            this.CurrentControlRedirecting(url);
        }

        public void LoadUrl(string url)
        {
            this.CurrnetUrl = url;
            this.CurrenetContorl = (BaseUserControl)this.LoadControl(this.CurrentPath);
            this.FristLoad = true;
            this.PreviousControl = "";
        }
    }

    public abstract class BaseUserControl : System.Web.UI.UserControl
    {
        public string PreviousControl
        {
            get
            {
                if (this.ViewState["precontrol"] == null)
                    this.ViewState["precontrol"] = string.Empty;
                return this.ViewState["precontrol"].ToString();
            }
            set
            {
                this.ViewState["precontrol"] = value;
            }
        }

        public string CurrentControl
        {
            get
            {
                if (this.ViewState["ccontrol"] == null)
                    this.ViewState["ccontrol"] = string.Empty;
                return this.ViewState["ccontrol"].ToString();
            }
            set
            {
                this.ViewState["ccontrol"] = value;
            }
        }
        public delegate void RedirectHandler(string url);
        public event RedirectHandler Redirecting;

        public bool PostBack
        {
            get
            {
                if (this.ViewState["_postBack"] == null)
                    this.ViewState["_postBack"] = false;
                return this.ViewState["_postBack"].ToString().ToBol();
            }
            set
            {
                this.ViewState["_postBack"] = value;
            }
        }


        public void Redirect(string url)
        {
            this.Redirecting(url);
        }
    }
    public abstract class BaseUserControl<T> : BaseUserControl
    {
        public virtual void GetDataFromControl(T obj)
        {
            this.TryUpdateModel(obj);
        }
        public virtual void LoadControl(T obj)
        {
            this.TryBindModel(obj);
        }
    }

main.ascx代码

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace WebApplication1
{
    public partial class Main : MasterControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {



            if (!Page.IsPostBack)
            {

            }






        }

        protected void RadMenu1_ItemClick(object sender, Telerik.Web.UI.RadMenuEventArgs e)
        {


            if (e.Item.Selected != null)
            {
                this.Redirect(e.Item.Value);
            }


        }
    }
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Main.ascx.cs" Inherits="WebApplication1.Main" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<link href="Main.css" rel="stylesheet" />

<div class="RadMenu_WebBlue">
    <telerik:RadMenu ID="RadMenu1" runat="server" OnItemClick="RadMenu1_ItemClick" EnableEmbeddedSkins="False"  >
        <Items>

            <telerik:RadMenuItem runat="server" Text="مطالب" >
               <Items>
                   <telerik:RadMenuItem runat="server" Text="ایجاد مطلب جدید"  Value="/CreatePost.ascx" />
                    <telerik:RadMenuItem runat="server" Text="ارشیو مطالب"  Value="/ArchivePost.ascx"></telerik:RadMenuItem>
               </Items>
            </telerik:RadMenuItem>


             <telerik:RadMenuItem runat="server" Text="تغییر رمز عبور" Value="/changePassword.ascx"></telerik:RadMenuItem>



        </Items>
    </telerik:RadMenu>
    </div>
<asp:PlaceHolder EnableViewState="True" ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

1 个答案:

答案 0 :(得分:0)

您应该在主页

中初始化usercontrol
 protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

        }
        this.Initialize();

    }
相关问题