参数传递到用户控件 - asp.net [英] Pass parameters to a user control - asp.net

查看:117
本文介绍了参数传递到用户控件 - asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的用户控件:

<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();
}

我不明白,为什么在用户控件,当我把收到的(产品和类别),它给了我&LT;%= getProductId()%>中的数据,而没有给的ID从该方法获得..

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...

任何帮助将是好心AP preciated ...

Any help would be kindly appreciated...

修改:解决和:产品='&LT;%#getProductId()%>

Edit: Solved with: product='<%# getProductId() %>'

最后一个问题:在用户控件我有这样的:

Last problem: in the user control I have this:

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

所以,我希望ProductID等于设置确定在用户的控制。
不幸的是空当我尝试使用它...

So, I expect that the productId is set up ok in the user control. Unfortunately it is null when I try to use it...

这有什么,我写这是不正确的?

Is there anything I wrote that's incorrect?

推荐答案

那么,你得到编译时检查,你可以给你的用户控制 ID ,然后设置其产品类别在C#这样的特性:

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:

ASPX:

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

CS:

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

另外,作为<一href=\"http://stackoverflow.com/questions/2722561/pass-parameters-to-a-user-control-asp-net/5518517#5518517\">5arx提到,一旦你填充的再刷新页面会重新载入你的控制,你就会失去产品类别标识。您可以通过使用ViewState的在你的用户控件的属性,这样的处理是:

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. You can handle that by using ViewState on the properties in your user control, like this:

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;
    }
}

注意:我已经更新了属性名称外壳遵循惯例,因为它只是更有意义,我认为这样!就个人而言,我总是后缀标识与 ID (例如:的ProductID )从包含的属性区别产品对象。了解更多关于编码标准在这里: C#编码标准/最佳做法

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. Read more about coding standards here: C# Coding standard / Best practices

这篇关于参数传递到用户控件 - asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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