By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,013 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

PlaceHolder misbehavior under GridView?

P: n/a
F
Hi,

The funniest part of the problem described here is that I am not new to
ASP.NET, but honestly here I have no idea about what's going on.

I have a GridView with a data source that just returns 1 row of only 1
column.
The GridView has only one field, a TemplateField.

In GridView.RowCreated, I create a TextBox under a PlaceHolder statically
declared in the ItemTemplate.

In GridView.RowDataBound, I assign a Text value to this TextBox.
I've added a Button to my page to be able to initiate a simple postback.
The idea is to make sure the text value of the TextBox is properly restored
upon postback.

As simple as it might seem, it just doesn't work.

If I add the TextBox *to the Cell* where the PlaceHolder is declared,
everything works as expected

Now if I do want to create this TextBox under the PlaceHolder, FindControl
doesn't work in RowDataBound upon postback (returns null).
Even if I don't use FindControl and manually add then retrieve the TextBox
through phData.Controls[0], the ViewState is not applied upon postback.
This looks like a problem with the PlaceHolder to me.

Can somebody explain me why I get this behavior?

Thanks
-F

--
Here is the markup:

<asp:GridView ID="gv1" runat="server" DataSourceID="ds1"
AutoGenerateColumns="False" DataKeyNames="Name"
OnRowDataBound="gv1_RowDataBound"
OnRowCreated="gv1_RowCreated">
<Columns>
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
<asp:PlaceHolder ID="phData" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

And here is the code:

protected void gv1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// This works
// Control whereToAdd = e.Row.Cells[0];

// This DOESN'T WORK
Control whereToAdd = (PlaceHolder)e.Row.FindControl("phData");

TextBox tb = new TextBox();
tb.ID = "heho";
whereToAdd.Controls.Add(tb);
}
}

protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// This works
// Control whereToAdd = e.Row.Cells[0];

// This DOESN'T WORK
// FindControl returns NULL here. Why?
Control whereToAdd = (PlaceHolder)e.Row.FindControl("phData");

TextBox tb = (TextBox)whereToAdd.FindControl("heho");
if (tb == null)
{
tb = new TextBox();
tb.ID = "heho";
whereToAdd.Controls.Add(tb);
}
tb.Text = "OKAY";
}
}

Sep 22 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.