473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

PlaceHolder misbehavior under GridView?

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
0 1960

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't...
2
by: Mike Speak | last post by:
I have a user control that I want to use to render 4 menu items (retrieved from db) on the top of each of my asp.net pages. The user control defines a table, with one TR and one TD. Within the...
1
by: Dan | last post by:
I have an asp.net page default.aspx with a user control and a placeholder control. <html> <body> <form id="myform" method="post" runat="server" /> <PageHeader:Header id="header1"...
2
by: Chad Crowder | last post by:
Is it possible to create a placeholder inside a template column of a datagrid? My goal is to use a placeholder, and during runtime add user controls to the placeholder as the items in the datagrid...
0
by: shark | last post by:
I have a placeholder in a control that is in turn, used as a control in a placeholder in a form. I am getting a viewstate error so I believe I have to clear my placeholder before I load the...
1
by: Angel | last post by:
I have added controls to the placeholder control. All the controls that were added have EnableViewState = true including the placeholder. One of the controls has a button that performs a postback. My...
4
by: Dica | last post by:
i need to dynamically add a web control to a page without using placeHolder. i'm looping through all the files in a folder and creating a table row as string for each file. the contol is to be...
6
by: David Colliver | last post by:
Hi, using vb.net 1.1 I am trying to add a control to a placeholder but am having problems with it. I do it practically the same way as i do in C# (I have more C# skill than VB.NET)and I...
7
by: Brad Baker | last post by:
I am trying to programmatically set a placeholder control in csharp which is nested inside a repeater control between <ItemTemplateand </ItemTemplate> tags, however I am running into problems. I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.