简体   繁体   English

如何在用户控件中传递值

[英]How to pass value in user control

Hi I have set a value in <%= MutlipleB %> and below is my Usercontrol calling in .aspx page. 嗨,我已经在<%= MutlipleB%>中设置了一个值,下面是在.aspx页中调用的Usercontrol。 Let say <%= MutlipleB %> is in string format and i have set a value as "1" or "2" ie set dynamically from code side. 假设<%= MutlipleB%>是字符串格式,并且我已将值设置为“ 1”或“ 2”,即从代码端动态设置。 I want to pass this value in user control. 我想在用户控件中传递此值。 But it is passing value as <%= MutlipleB %> not "1" or "2". 但是它传递的值是<%= MutlipleB%>而不是“ 1”或“ 2”。 How could I pass my proper value? 我该如何传递我的正确价值?

<eu:EASIUp ID="myeasiup" runat="server" Quantity="-1" CanBeZero="false" NameID='<%= MutlipleB %>'></eu:EASIUp>

or Tried passing value as 或尝试将传递值作为

<%# MutlipleB %> / <%= this.MutlipleB %> / <%# this.MutlipleB %>//Still not working

User control code 用户控制码

private string _nameID="0";
public string NameID
{
    get
    {
        return _nameID.ToString();
    }
    set { _nameID = value; }
}

Set value in <%= MutlipleB %> as below in aspx.cs code 在aspx.cs代码中如下设置<%= MutlipleB%>中的值

private string _multipleB = "0";
public string MutlipleB
{
    get { return _multipleB; }
    set { _multipleB = value; }
}

how can I retrive value on user control side 我该如何在用户控制端获取价值

The main reason to pass this value is on UserControl we are generating dynamic control and the id of this control should be generated related to the value im passing 传递此值的主要原因是在UserControl上,我们正在生成动态控件,并且应生成与该值有关的控件id

Give your User Control a public property. 给您的用户控件一个公共属性。

public partial class myCustomControl : System.Web.UI.UserControl
{
    public string NameID { get; set; }
}

And you can then set or get the value from the aspx page code behind. 然后,您可以从后面的aspx页面代码中设置或获取值。

protected void Page_Load(object sender, EventArgs e)
{
    myeasiup.NameID = "myString";
}

Open your user control's code behind page ( FILENAME.ascx.cs ). 在页面后面( FILENAME.ascx.cs )打开用户控件的代码。 There you add below property. 在此处添加以下属性。

public string Value { get; set; }

So, overall your code page be like: 因此,总体而言,您的代码页如下:

namespace YOUR.NAMESPACE
{
    public partial class CLASS_NAME : UserControl
    {
        public string Value { get; set; }

        ... // REST OF CODE

    }
}

You can simply pass data to your code behind like below in where you are using user control: 您可以像下面在使用用户控件的地方那样简单地将数据传递到您的代码后面:

<TAG_PREFIX:CLASS_NAME runat="server" Value="YOUR_DATA" ID="SomeId"/>

Or from the code behind like below: 或从如下所示的代码中:

SomeId.Value = "YOUR_DATA";

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM