需要ASPX页面引用用户控件(ASCX)在多个地方有不同的结果 [英] Need to reference Usercontrol(ascx) in ASPX page in more than one place with different results

查看:176
本文介绍了需要ASPX页面引用用户控件(ASCX)在多个地方有不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:
Default.aspx的是如以下

Scenario : Default.aspx is as below.

<%@ Page MasterPageFile="~/StandardLayout.master" Language="C#" CodeFile="Default.aspx.cs"     Inherits="_Default" %>
<%@ Register src=  "~/Controls/Us.ascx" tagname="AboutUs" tagprefix="site" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
    <div id="large">
       <site:AboutUs  ID="AboutUsControl" runat="server" />
    </div>
   <div id="small">
      <site:AboutUs  ID="AboutUsControl" runat="server" />
    </div>
</asp:Content>

AboutUs.ascx.cs分配一定的价值,以一个标签控件。在上述情况下,我想DIV ID =小范围内重复使用关于我们的逻辑是一样的,但只有值的变化。

AboutUs.ascx.cs assigns some value to a label control. In the above scenario, I want to re-use AboutUs within "div id=small" as the logic is same but only value change.

我的问题是AboutUs.ascx.cs内,我需要一些方法来找出它是否属于在,指定的Label1 =我在这里。否则标签1 =我无处不在

My question is within AboutUs.ascx.cs, I need some way to find out if it belongs within "", assign Label1 = "I am here". Otherwise Label1 = "I am everywhere"

我试图传递参数,但我需要的任何东西在code-背后default.aspx.cs?或任何其他建议。

I am trying to pass parameters but do I need to anything in the code-behind in default.aspx.cs? or any other suggestions.

请建议。

推荐答案

请确保两个用户控件具有唯一的ID。我将使用AboutUsControl1和AboutUsControl2。声明一个名称属性用户控件:

Make sure both user controls have unique ID's. I'll use AboutUsControl1 and AboutUsControl2. Declare a name property for your user control:

private string _doWhat;
    public string doWhat
    {
        get { return _doWhat; }
        set { _doWhat = value; }
    }

    //Execute the check somewhere in your code to set the text you want.    
    private void Do_Something()
    {
        if (_doWhat == "Large")
        {
            //display "I am here"
        }
        else
        {
            //display "I am everywhere"
        }
    }

而在后面的code。使用用户控件的页面上,只需通过调用公共变量传递值:

And in the code behind on the page using the user controls, just pass the value by calling the public variable:

AboutUsControl1.doWhat = "Large";
        AboutUsControl2.doWhat = "Small";

或控制自己刚刚设置doWhat:

or just set doWhat in the control itself:

<site:AboutUs ID="AboutUsControl1" runat="server" doWhat="Large" />
<site:AboutUs ID="AboutUsControl2" runat="server" doWhat="Small" />

这篇关于需要ASPX页面引用用户控件(ASCX)在多个地方有不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆