daisy
daisy

Reputation: 23501

Changing inner ASPX code of a PlaceHolder?

I use a Repeater to read from different tables, since tables has different columns, I'll have to use a dynamic template.

 <asp:Repeater ID="tableData" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
                     <asp:PlaceHolder ID="tableBody" runat="server">
                            </asp:PlaceHolder>
           </ItemTemplate>
 </asp:Repeater>

But VS 2010 doesn't seem to recognize the placeholder nested in the repeater.

So I tried to let the placeholder generate the repeater, but how can I change the inner code by hand? There's no "innerHtml" option.

UPDATE

I'm note sure If placeholder is approriate , but what I want is to generate stuff inside <ItemTemplate> dynamically , which is things like <%# DataBinder.Eval(Container.DataItem, "username") %>

Because I'm reading different table column from different tables , this need to be selected base on the table user wanted.

So I need to modify the ASPX code before it's being processed , is there any better way to do this ?

Upvotes: 0

Views: 791

Answers (1)

SurinderBhomra
SurinderBhomra

Reputation: 2199

Visual Studio won't pick up a control that is nested in a Repeater. For this, you will need to use FindControl to access the control in the Repeater.

Here are two examples you can use to access a control within a Repeater.

1) Looping through Repeater items

foreach (RepeaterItem item in tableData.Items)
{
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        var ph = (PlaceHolder)item.FindControl("tableBody");

        //Do something with your placeholder
        ph.Visible = true;
    }
}

2) Use the ItemDataBound event on the Repeater

void tableData_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var ph = (PlaceHolder) e.Item.FindControl("tableBody");

        //Do something with your placeholder
        ph.Visible = true;
    }
}

If you plan on using the ItemDataBound event, you will need to add the following attribute to your Repeater:

OnItemDataBound="tableData_ItemDataBound"

Could you clarify what you exactly want to do with your Placeholder? There is no "innerHTML" property for a Placeholder. I think you are getting mistaken with JavaScript. I presume you want to populate your a control inside your repeater with some Html. You could use a Literal instead.

Edit

Using a Literal to populate the Repeater dynamically based on data from database.

void tableData_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Get column from datasource
        DataRowView drv = (DataRowView)e.Item.DataItem;

        var lit = (Literal) e.Item.FindControl("MyLiteral");

        //Populate Literal with data
        StringBuilder htmlMarkup = new StringBuilder();

        htmlMarkup.AppendFormat("{0}<br />", drv["username"].ToString());
        htmlMarkup.AppendFormat("{0}<br />", drv["Name"].ToString());
        htmlMarkup.AppendFormat("{0}<br />", drv["lastname"].ToString());

        lit.Text = htmlMarkup.ToString();
    }
}

As you can see I am retrieving the data and outputting what I want in the format I want.

Upvotes: 1

Related Questions