开发者

Access controls inside a repeater inside a user control

I have a property in my code that sends back the information in a usercontrol that is a repeater. rptOwners is the repeater I am using. The information inside the repeater has to be parsed manually to xml and sent back through a property in the code. The problem is that I can't seem to get access to the values of the controls inside the repeater.

When debugging, I am able to see that there are the 2 items I expected in rptOwners.DataSource and that the rptOwners.Items.Count = 2. I can then see in the watch window that the information I want is there but I can't get access to it.

I have been trying rptOwners.DataSource[0].Name etc but it says that "cannot apply indexing to an expression of type object.

public string xmlString
{
    get
    {
        var _builder = new StringBuilder();

        var rpt = rptOwners.DataSource;
        IList<string> ownersRepeater = new List<string>();

        foreach (var item in rpt )
        {
            _builder.Append("<Owners>");
            _builder.Append("<Owner>");
            _builder.Append(String.Format("<item>{0}</item>", name));
            _builder.Append(String.Format("<item>{0}</item>", address));
            _builder.Append(String.Format("<item>{0}</item>", age));
            _builder.Append("</Owner>");
            _builder.Append("</Owners>");
        }
        return _builder.ToString();
    }

Thanks for the help. Let me know if this was not clear enough.

Here is more of what I mean...

Access controls inside a repeater inside a user control

Here is the answer:

foreach (RepeaterItem item in rptOwners.Items)
        {
            var lblOwnerName = (Label)item.FindControl("lblOwnerName");


            _builder.Append("<Owners>");
            _builder.Append("<Owner>");
            _builder.Append(String.Format("<item>{0}</item>", lblO开发者_StackOverflow中文版wnerName));
            _builder.Append(String.Format("<item>{0}</item>", item));
            _builder.Append(String.Format("<item>{0}</item>", item));
            _builder.Append(String.Format("<item>{0}</item>", item));
            _builder.Append(String.Format("<item>{0}</item>", item));
            _builder.Append(String.Format("<item>{0}</item>", item));
            _builder.Append(String.Format("<item>{0}</item>", item));
            _builder.Append("</Owner>");
            _builder.Append("</Owners>");


        }
        return _builder.ToString();


Try this (i only added a cast):

public string xmlString
{
get
{
    var _builder = new StringBuilder();

    var rpt = (IList<Owner>) rptOwners.DataSource; //ADDED A CAST
    IList<string> ownersRepeater = new List<string>();

    foreach (var item in rpt )
    {
        _builder.Append("<Owners>");
        _builder.Append("<Owner>");
        _builder.Append(String.Format("<item>{0}</item>", name));
        _builder.Append(String.Format("<item>{0}</item>", address));
        _builder.Append(String.Format("<item>{0}</item>", age));
        _builder.Append("</Owner>");
        _builder.Append("</Owners>");
    }
    return _builder.ToString();
}


I dont know how your DataSource property implemented but from your description seems that changing

var rpt = rptOwners.DataSource;

line to

var rpt = rptOwners.Items;

should work.


I suggest you not to use 'var' wherever you want. It is better to show the exact type if it's not obvious what is the type looking into the right part of expression.

You cannot apply indexer just because Repeater.DataSource property is of type System.Object. You have to cast it to appropriate one to get access to indexer (List?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜