Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > PlaceHolder misbehavior under GridView?

Thread Tools

PlaceHolder misbehavior under GridView?

Posts: n/a

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
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?


Here is the markup:

<asp:GridView ID="gv1" runat="server" DataSourceID="ds1"
AutoGenerateColumns="False" DataKeyNames="Name"
<asp:TemplateField HeaderText="Column1">
<asplaceHolder ID="phData" runat="server" />

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];

Control whereToAdd = (PlaceHolder)e.Row.FindControl("phData");

TextBox tb = new TextBox();
tb.ID = "heho";

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

// 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";
tb.Text = "OKAY";

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Win7 Pro keyboard misbehavior GS Windows 64bit 5 03-15-2013 05:55 AM
Build control in a placeholder or like a placeholder nail ASP .Net Building Controls 0 09-10-2004 07:57 PM
The times() function and child cpu time misbehavior A. Lewenberg Perl Misc 2 02-22-2004 06:42 AM
2003 webserving misbehavior!? pat ASP General 1 09-09-2003 02:00 PM
slight csv misbehavior under Windows Thomas Womack Python 1 08-18-2003 06:57 AM