简体   繁体   English

将参数传递给用户控件 - asp.net

[英]Pass parameters to a user control - asp.net

I have this user control: 我有这个用户控件:

<user:RatingStars runat="server" product="<%= getProductId() %>" category="<%= getCategoryId() %>"></user:RatingStars>

You can see that I fill in the product and category by calling two methods: 您可以通过调用两种方法来查看我填写产品和类别:

public string getProductId()
{
    return productId.ToString();
}

public string getCategoryId()
{
    return categoryId.ToString();
}

I do not understand why, in the user control, when I take the data received (product and category) it gives me "<%= getProductId() %>" instead of giving the id received from that method... 我不明白为什么,在用户控件中,当我接收到的数据(产品和类别)时,它给了我“<%= getProductId()%>”而不是给出从该方法收到的id ...

Any help would be kindly appreciated... 任何帮助将不胜感激......

Edit : Solved with: product='<%# getProductId() %>' 编辑 :解决方法:product ='<%#getProductId()%>'

Last problem : in the user control I have this: 最后一个问题 :在用户控件中我有这个:

 public string productId;
 public string product
{
    get
    {
        return productId;
    }
    set
    {
        productId = value;
    }
}

So, I expect that the productId is set up ok in the user control. 所以,我希望在用户控件中将productId设置为ok。 Unfortunately it is null when I try to use it... 不幸的是,当我尝试使用它时它是空的...

Is there anything I wrote that's incorrect? 有什么我写的不正确吗?

So that you get compile-time checking, you can give your user control an ID and then set its Product and Category properties in C# like this: 因此,您可以获得编译时检查,您可以为您的用户控制一个ID ,然后在C#中设置其ProductCategory属性,如下所示:

ASPX: ASPX:

<user:RatingStars id="myUserControlID" runat="server" Product="<%= getProductId() %>" Category="<%= getCategoryId() %>"></user:RatingStars>

CS: CS:

myUserControlID.Product = GetProductId();
myUserControlID.Category = GetCategoryId();

Also, as 5arx mentions, once you've populated that then refreshing your page will reload your control and you'll lose the Product and Category IDs. 此外,正如5arx所提到的,一旦你填充了那么刷新你的页面将重新加载你的控件,你将丢失ProductCategory ID。 You can handle that by using ViewState on the properties in your user control, like this: 您可以通过在用户控件中的属性上使用ViewState来处理它,如下所示:

private const string ProductKey = "ProductViewStateKey";
public string Product
{
    get
    {
        if (ViewState[ProductKey] == null)
        {
              // do whatever you want here in case it's null 
              // throw an error, return string.empty or whatever
        }
        return ViewState[ProductKey].ToString();
    }

    set
    {
        ViewState[ProductKey] = value;
    }
}

NOTE: I've updated the property name casing to follow convention, as it just makes more sense to me that way! 注意:我已经更新了属性名称大小以遵循惯例,因为它对我来说更有意义! Personally, I'd always suffix IDs with ID (eg: ProductID ) to distinguish it from a property that contains a Product object. 就个人而言,我总是使用ID (例如: ProductID )对ID进行后缀,以将其与包含Product对象的属性区分开来。 Read more about coding standards here: Are there any suggestions for developing a C# coding standards / best practices document? 在此处阅读有关编码标准的更多信息: 是否有任何关于开发C#编码标准/最佳实践文档的建议?

Please post some more of your code? 请发布更多代码?

A couple of things: 有几件事:

  • productId is an object reference (to a string object) which means: productId是一个对象引用(对于一个string对象),这意味着:

  • unless you write some intialisation code to 'fill' it with a string reference at the start, it will be null . 除非你在开始时用一个字符串引用写一些初始化代码来填充它,否则它将为null

  • if you create a string eg string x = new String() x will be an empty string , which you can think of as "" without the quotes . 如果你创建一个字符串,例如string x = new String() x将是一个空字符串 ,你可以将其视为没有引号的 “”。 (It is a string, but its empty because it has no characters in it). (它一个字符串,但它是空的,因为它没有字符)。

  • you can just write return productId; 你可以写return productId; - no need to call ToString() on a string. - 不需要在字符串上调用ToString()

  • Your page does not have an out-of-the-box mechanism to store variables across postbacks. 您的页面没有开箱即用的机制来在回发中存储变量。 You need to use ViewState or hidden form fields to do this. 您需要使用ViewState或隐藏的表单字段来执行此操作。

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

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