Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

PlaceHolder misbehavior under GridView?

 
 
F
Guest
Posts: n/a
 
      09-22-2007
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>
<asplaceHolder 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";
}
}

 
Reply With Quote
 
 
 
Reply

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



Advertisments