put control in a repeater cell from code behind

Discussion in 'ASP .Net Web Controls' started by David Thielen, Mar 21, 2006.

  1. Hi;

    I have a repeater block where the number of rows and the value of the first
    column come from the data bound to the repeater control. But I need a second
    column in the table where I place a control from my code behind.

    I need to do this because the type of control depends on the data for that
    row.

    How can I do this?

    --
    thanks - dave
    david_at_windward_dot_net
    http://www.windwardreports.com
     
    David Thielen, Mar 21, 2006
    #1
    1. Advertising

  2. Hi Dave,

    As for the adding dynamic created controls into repeater rows depending on
    the databound values, I think the "ItemDataBound" event is the one you're
    looking for. we can dynamically create controls there and add them into
    repeater row. Also, the event's parameter can let us check the current
    databinding datarow's value do do some conditional customization. Here is a
    simple example:

    ==========repeater template===
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"
    OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate >
    <hr style="width:100%" /><br />

    </ItemTemplate>
    </asp:Repeater>
    ======================

    ==========code behind=========
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs
    e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {

    string name = DataBinder.Eval(e.Item.DataItem,
    "CategoryName","{0}");

    if (e.Item.ItemIndex % 2 == 1)
    {
    TextBox txt = new TextBox();
    txt.Text = name;
    e.Item.Controls.Add(txt);
    }else{
    Label lbl = new Label();
    lbl.Text = name;
    e.Item.Controls.Add(lbl);
    }
    }
    }
    ================================

    Hope this helps.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================
     
    Steven Cheng[MSFT], Mar 22, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    620
    Alphonse Giambrone
    Jun 28, 2003
  2. Ric
    Replies:
    9
    Views:
    5,498
    ric carrasquilla
    Mar 3, 2004
  3. Tee
    Replies:
    1
    Views:
    4,150
    Raterus
    Jun 24, 2004
  4. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: help with repeater, how to code it in code-behind page

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    479
    Ireney Berezniak
    Jun 25, 2004
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    509
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...

Share This Page