Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

repeater, web user control, it's property and a databinded field

Status
Not open for further replies.

Greybear

Technical User
Aug 27, 2008
5
Hello all
I"ve just run into this:
There's a simple page, what contains a repeater, that only has an itemtemplate with a web user control. like this:
Code:
    <asp:Repeater runat="server" ID="rptrChecklist">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
        <asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "id" )%>'></asp:Label>
        <FN:Checklist ID="Checklist1" runat="server" Server='<%#DataBinder.Eval(Container.DataItem, "id" )%>'></FN:Checklist>
    </ItemTemplate>
    </asp:Repeater>
The web control is simple:
Code:
Parameter passed: <asp:Label runat="server" ID="Parameter"></asp:Label>
Code behind the ascx:
Code:
public partial class Serverchecklist : System.Web.UI.UserControl
{
    public string ServerID;
    public string Server
    {
        get
        {
            return ServerID;
        }
        set
        {
            ServerID = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Parameter.Text = ServerID;
    }
}
My problem is the following:
on the page, the label shows the correct value. If i hardcode a value (like: server="1") into the user control, that's fine too. But if it's like above, the ServerID variable doesn't get any value.

What am i doing wrong? I've been googling my head off and i didn't get any closer.
I'm running .NET 3.5 under IIS7(Vista), working with VS2008

I'd appreciate any help

Thanks
Greybear
 
on the page, in the repester's itemtemplate when adding the control:
<FN:Checklist ID="Checklist1" runat="server" Server='<%#DataBinder.Eval(Container.DataItem, "id" )%>'></FN:Checklist>
 
That line of code is not setting the property's value... It is setting an attribute of Checklist1 called "Server." If you want to set the property to match the value you are going to need to retrieve it from the attribute.. For example, if you wanted to get the "Server" attribute of the checklist in the first item of the repeater you can do this (in this example I am using an asp.net checkboxlist, but you can use the same methodology):

Code:
CheckBoxList list = (CheckBoxList)rptrChecklist.Items[0].FindControl("CheckBoxList1");
Server = list.Attributes["Server"];

I don't know how useful this is actually going to be though, as I am guessing the value of Server is likely to change within each item. You may want to consider using a collection to store the value of Server for each item.

Hope this helps.

 
OK, i get it.

Is it possible to retrieve the Server attribute from the web control's code?

Something like this (in control.ascx.cs and Page_Load)
ServerID = this.Attributes["Server"];
 
The problem you are going to face is that your property, ServerID, is relative to the control itself. In other words, there will only ever be 1 ServerId property in the control regardless of how many items are in the repeater. If you want to capture the ServerId for each item in the repeater you can do this using the ItemDataBound event, but you will need to store the values in some type of collection. An easy way to do this would be to use an Hashtable.... The following example illustrates this...

Code:
private Hashtable serverIdList = new Hashtable();
public Hashtable ServerIdList
{
   get
   {
      return serverIdList;
   }
}
protected void rptrChecklist_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
      CheckBoxList cbl = (CheckBoxList)e.Item.FindControl("CheckBoxList1");
      string serverId = cbl.Attributes["Server"];
      ServerIdList.Add(e.Item.ItemIndex, serverId);
   }
}



 
I think we're talking about two separate things.
In my example i have many controls, because my user control is inside the repeater's Itemtemplate, so one is created for every datarow.

And i try to access the Server attribute from INSIDE the user control.

And that works as mentioned in my first post - if the control's alone and i call it like this:
<FN:Checklist runat="Server" Server="1"/>

That's cool, i can handle the Server property's value from inside the control's logic.

But if i have it inside a repeater and i try to pass a databinded value, like:
<FN:Checklist runat="Server" Server='<%# DataBinder.Eval(Container.DataItem, "id" )%>' />

- i get nothing.

Inside a repeater, and passing a direct value (Server="1"), i get the value.

I think the problem is around databindig, and something i don't understand with, or maybe i just think wrong, i don't know.

Thanks
Greybear
 
If the property is inside of the user control (CheckList) the you are correct about not needing a collection, however the same rule applies as far as setting the property goes. You may not actually need to set the property though, as you will have access to that attribute throughout the life of the control. The problem is not with databinding, the problem is that you are only setting the attribute value.

 
does DataBinder.Eval(Container.DataItem, "id" ) have a non-empty value?

it could also be timing of events. your setting the parameter value on page load in the user control. when you hard code the value it's set before the page load event. when you use databinding the value is not set until the row is databound. this may be happening after the page load event.

this should fix your timing issue.
Code:
public partial class Serverchecklist : System.Web.UI.UserControl
{
    public string Server
    {
        get { return Parameter.Text; }
        set { Parameter.Text= value; }
    }
    //remove Page_Load member
}


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
KDavie: i get what you write, but if i set it to 1 instead of the databinded value, it works. I can get the property value. that's why assume it's a databinding oriented problem.

jmeckley:
Yes, if i put <asp:label runat="server" text='DataBinder.Eval(Container.DataItem, "id" )' /> into the itemtemplate, it displays the data correctly.

I tried the code you gave, but i didn't get any value either.
 
Interesting, I didn't think you could set the property through the attribute unless you compiled the control... Is the control compiled into a dll?

Regardless, I still don't think it is a databinding issue... as you said, the value is being set... Are you able to get the value from the attribute?

Something to keep in mind is that you are creating the attribute on the page that is using the control, not in the control itself. This being said, I agree with jmeckley that it is a matter of timing, the control's load event is firing before the attribute is getting its value. Can you post the markup for your control please? It will be easier to provide a solution if I understand what exactly is in the control.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top