English 中文(简体)
ASP.NET • 如何通过海关用户控制页数?
原标题:ASP.NET How to change Page controls via custom UserControl?

我的网页上有一个“Label”控制和习俗用户目录。 我想,当用户议会出现一些情况时,它会带来一些变化,例如拉贝尔文本财产(如我所提到的,拉贝尔人不属于用户委员会)。 如何做到这一点?

问题回答

最好的做法是利用某种活动通知载有用户目录的网页。

public class MyControl : UserControl {
    public event EventHandler SomethingHappened;

    private void SomeFunc() {
        if(x == y) {
            //....

            if(SomethingHappened != null)
                SomethingHappened(this, EventArgs.Empty);
        }
    }
}

public class MyPage : Page {

    protected void Page_Init(object sender, EventArgs e) {
        myUserControl.SomethingHappened += myUserControl_SomethingHappened;
    }

    private void myUserControl_SomethingHappened(object sender, EventArgs e) {
        // it s Business Time
    }
}

这只是一个基本例子,但我个人建议使用设计人接口,具体指明用户控制活动手,使任务在设计人代码背后处理,而不是由你重新工作。

用户目录应当重新使用,以便正确使用,因此,你应当利用用户目录的一项活动,即:

public NewTextEventArgs : EventArgs
{
    public NewTextEventArgs(string newText)
    {
        _newText = newText;
    }

    public NewText 
    { 
        get { return _newText; }
    }
}

接着,在“用户”上添加以下活动:

public event OnNewText NewText;
public delegate void OnNewText(object sender, NewTextEventArgs e);

接着从用户控制处向活动开火:

private void NotifyNewText(string newText)
{
    if (NewText != null)
    {
        NewText(this, new NewTextEventArgs(newText));
    }
}

之后,就在你的网页和用户目录和网页上消费这一活动已不再是紧凑的:

随后处理此事,并将案文提交给你的Label:

protected void YourControl1_NewText(object sender, NewTextEventArgs e)
{
    Label1.Text = e.NewText;
}

你们可以通过向用户介绍活动来这样做。 该网页随后可以拦截该活动,并相应修改标签文本财产:

http://asp.net-tutorials.com/user-controls/events/"rel=“nofollow noreferer”>http://asp.net-tutorials.com/user-controls/events/

您可使用页码,进入用户目录。 Try:

(Page1)this.Page.Label1.Text = “Label1 Text”





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签