Reference "Programmatic" Gridview Control on Postback

Discussion in 'ASP .Net Web Controls' started by John Walker, Jun 9, 2008.

  1. John Walker

    John Walker Guest

    Hi, my gridview has a label field which I'm replacing with a dropdownlist at
    the time of databind with this code:

    Dim lblType As Label = CType(e.Row.FindControl("lbltype"), Label)
    e.Row.Cells(Cnst.Cells.AccountType).Controls.Remove(lblType)

    Dim ddlType As New DropDownList
    ddlType.ID = "ddlType"
    ddlType.Items.Add(New ListItem("test", "test"))
    ddlType.Items.Add(New ListItem("Bill To", "BILLTO"))
    ddlType.Items.Add(New ListItem("Acct Of", "ACCTOF"))
    e.Row.Cells(Cnst.Cells.AccountType).Controls.Add(ddlType)


    So, the dropdown displays in the browser, but on subsequent postbacks i
    cannot reference that control:

    Dim ddlType As DropDownList =
    CType(gvr.Cells(Cnst.Cells.AccountType).FindControl("ddlType"), DropDownList)

    gvr is the gridviewrow and the value of the dropdownlist is returning
    "Nothing".... but when i'm not creating the dropdown programatically i am
    able to reference it successfully.

    What am i doing wrong??

    Thanks,
    John
    John Walker, Jun 9, 2008
    #1
    1. Advertising

  2. Hello John

    You add the DDL in the RowDataBound Event, right?
    And the following Code is in your Page_Load Method:
    > Dim ddlType As DropDownList =
    > CType(gvr.Cells(Cnst.Cells.AccountType).FindControl("ddlType"),
    > DropDownList)


    right?

    If so, move the above Code in Page_PreRender, cause at that time
    the RowDataBound Event should be done and you`ve access to your
    created Control at RowDataBound Time.

    --
    Gruss, Peter Bucher
    Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
    Peter Bucher [MVP], Jun 11, 2008
    #2
    1. Advertising

  3. John Walker

    John Walker Guest

    Peter,
    Yes i add the DDL on the RowDataBound event, but i do not attempt the
    "FindControl" until the page has already been displayed to the screen and the
    user clicks "Save" (after changing the selected value in the dropdownlist.)
    So, i am doing the FindControl in the button click event. Please take
    another look and let me know if i'm doing anything wrong when/where creating
    the dropdown.
    As i said, when defining the ddl in aspx (instead of creating it
    programmatically) the FindControl works successfully.
    Thanks!
    John

    "Peter Bucher [MVP]" wrote:

    > Hello John
    >
    > You add the DDL in the RowDataBound Event, right?
    > And the following Code is in your Page_Load Method:
    > > Dim ddlType As DropDownList =
    > > CType(gvr.Cells(Cnst.Cells.AccountType).FindControl("ddlType"),
    > > DropDownList)

    >
    > right?
    >
    > If so, move the above Code in Page_PreRender, cause at that time
    > the RowDataBound Event should be done and you`ve access to your
    > created Control at RowDataBound Time.
    >
    > --
    > Gruss, Peter Bucher
    > Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    > http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    > http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
    >
    >
    John Walker, Jun 11, 2008
    #3
  4. Hi John

    > Yes i add the DDL on the RowDataBound event, but i do not attempt the
    > "FindControl" until the page has already been displayed to the screen and
    > the
    > user clicks "Save" (after changing the selected value in the
    > dropdownlist.)

    Primarily you should add the Controls at least at Page_Load, better OnInit
    of the Lifecycle.
    That means in your case, that you should define your DropDownList in a
    TemplateField for example.

    This way, you`ve the possibility to switch the "Visible" Property of your
    Control at any time in the Cycle (at least OnPreRender of the Page).

    HTH

    --
    Gruss, Peter Bucher
    Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
    Peter Bucher [MVP], Jun 12, 2008
    #4
    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. Michael Porcelli
    Replies:
    1
    Views:
    360
    »OÆW·L³n§Þ³N¤ä´©³¡
    Jul 28, 2003
  2. DV
    Replies:
    10
    Views:
    2,526
  3. Hai Nguyen

    Datagrid Programmatic

    Hai Nguyen, Jan 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    906
    Norman Yuan
    Jan 17, 2004
  4. =?Utf-8?B?RGVtZXRyaQ==?=

    Programmatic GridView

    =?Utf-8?B?RGVtZXRyaQ==?=, May 30, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    588
  5. Jimmy M
    Replies:
    4
    Views:
    483
    Jimmy M
    Sep 19, 2006
Loading...

Share This Page