PlaceHolder misbehavior under GridView?

Discussion in 'ASP .Net' started by F, Sep 22, 2007.

  1. F

    F Guest

    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";
    }
    }
    F, Sep 22, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,946
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  2. Thomas Womack

    slight csv misbehavior under Windows

    Thomas Womack, Aug 18, 2003, in forum: Python
    Replies:
    1
    Views:
    381
  3. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    324
  4. pat

    2003 webserving misbehavior!?

    pat, Sep 9, 2003, in forum: ASP General
    Replies:
    1
    Views:
    156
    Ray at
    Sep 9, 2003
  5. A. Lewenberg
    Replies:
    2
    Views:
    220
    A. Lewenberg
    Feb 22, 2004
Loading...

Share This Page