Referencing controls in a DataGrid

Discussion in 'ASP .Net' started by mystical_potato, Sep 7, 2004.

  1. From VS.NET 03 AND VB.NET

    I added a dropdown list to a data grid control in response to the
    EditCommand event to force users to pick values from a list and not
    type them in. I used the following code:

    'Hide the existing txt box
    Dim txt As TextBox
    txt = CType(e.Item.Cells(0).Controls(0), TextBox)
    txt.Visible = False

    'display a ddl instead
    Dim ds As New DataSet
    ds = dbCalls.GetData
    Dim ddl As New DropDownList
    ddl.ID = "myID"
    ddl.DataSource = ds
    ddl.DataTextField = "myTxt"
    ddl.DataValueField = "myID"
    ddl.DataBind()
    ddl.Attributes.Add("class", "myCSS")
    e.Item.Cells(0).Controls.Add(ddl)

    This works great (everything displays as intended in the UI).
    However, when I code the UpdateCommand I cannot find the DropDownList
    control I created above. When I debug and add a watch for
    e.Item.Cells(0) it only shows a single control
    (e.Item.Cells(0).controls(0) which is the text box). How can I
    reference the DropDownList control? I am using the following code:

    Dim ddlMaintCatID As DropDownList
    ddlMaintCatID = CType(e.Item.Cells(0).Controls(X), DropDownList)

    For X i tried the following vales (0 and 1). For 0 I get and invalid
    cast error becuase controls(0) is a text box and for 1 I get an index
    out of range error.

    What am I missing???

    thanks in advance for your time and assistance.
    Scott
     
    mystical_potato, Sep 7, 2004
    #1
    1. Advertising

  2. mystical_potato

    Thomas Dodds Guest

    ddlMaintCatID = CType(e.Item.Cells(0).FindControl("myID"), DropDownList)

    the above will work if you used

    ddl.ID = "myID" and "myID" is a string, if it is the unique DB id then you
    will have to substitue something to determine the currect ID (use a hidden
    column and grab the text property or something)


    "mystical_potato" <> wrote in message
    news:...
    > From VS.NET 03 AND VB.NET
    >
    > I added a dropdown list to a data grid control in response to the
    > EditCommand event to force users to pick values from a list and not
    > type them in. I used the following code:
    >
    > 'Hide the existing txt box
    > Dim txt As TextBox
    > txt = CType(e.Item.Cells(0).Controls(0), TextBox)
    > txt.Visible = False
    >
    > 'display a ddl instead
    > Dim ds As New DataSet
    > ds = dbCalls.GetData
    > Dim ddl As New DropDownList
    > ddl.ID = "myID"
    > ddl.DataSource = ds
    > ddl.DataTextField = "myTxt"
    > ddl.DataValueField = "myID"
    > ddl.DataBind()
    > ddl.Attributes.Add("class", "myCSS")
    > e.Item.Cells(0).Controls.Add(ddl)
    >
    > This works great (everything displays as intended in the UI).
    > However, when I code the UpdateCommand I cannot find the DropDownList
    > control I created above. When I debug and add a watch for
    > e.Item.Cells(0) it only shows a single control
    > (e.Item.Cells(0).controls(0) which is the text box). How can I
    > reference the DropDownList control? I am using the following code:
    >
    > Dim ddlMaintCatID As DropDownList
    > ddlMaintCatID = CType(e.Item.Cells(0).Controls(X), DropDownList)
    >
    > For X i tried the following vales (0 and 1). For 0 I get and invalid
    > cast error becuase controls(0) is a text box and for 1 I get an index
    > out of range error.
    >
    > What am I missing???
    >
    > thanks in advance for your time and assistance.
    > Scott
     
    Thomas Dodds, Sep 7, 2004
    #2
    1. Advertising

  3. mystical_potato

    Scott Allen Guest

    Hi Potato:

    I have some articles with C# source that might help. They talk about
    the basics of using FindControl and using DropDownLists in a grid.

    In Search Of ASP.NET controls
    http://odetocode.com/116.aspx

    Embedding DropDownList Controls In A Grid
    http://odetocode.com/Articles/231.aspx


    HTH,
    --
    Scott
    http://www.OdeToCode.com

    On Tue, 07 Sep 2004 14:37:16 GMT, mystical_potato
    <> wrote:

    >From VS.NET 03 AND VB.NET
    >
    >I added a dropdown list to a data grid control in response to the
    >EditCommand event to force users to pick values from a list and not
    >type them in. I used the following code:
    >
    > 'Hide the existing txt box
    > Dim txt As TextBox
    > txt = CType(e.Item.Cells(0).Controls(0), TextBox)
    > txt.Visible = False
    >
    > 'display a ddl instead
    > Dim ds As New DataSet
    > ds = dbCalls.GetData
    > Dim ddl As New DropDownList
    > ddl.ID = "myID"
    > ddl.DataSource = ds
    > ddl.DataTextField = "myTxt"
    > ddl.DataValueField = "myID"
    > ddl.DataBind()
    > ddl.Attributes.Add("class", "myCSS")
    > e.Item.Cells(0).Controls.Add(ddl)
    >
    >This works great (everything displays as intended in the UI).
    >However, when I code the UpdateCommand I cannot find the DropDownList
    >control I created above. When I debug and add a watch for
    >e.Item.Cells(0) it only shows a single control
    >(e.Item.Cells(0).controls(0) which is the text box). How can I
    >reference the DropDownList control? I am using the following code:
    >
    >Dim ddlMaintCatID As DropDownList
    >ddlMaintCatID = CType(e.Item.Cells(0).Controls(X), DropDownList)
    >
    >For X i tried the following vales (0 and 1). For 0 I get and invalid
    >cast error becuase controls(0) is a text box and for 1 I get an index
    >out of range error.
    >
    >What am I missing???
    >
    >thanks in advance for your time and assistance.
    >Scott
     
    Scott Allen, Sep 7, 2004
    #3
    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. Jim Corey

    Referencing controls on user controls

    Jim Corey, Jan 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    361
    Hermit Dave
    Jan 26, 2004
  2. 2obvious
    Replies:
    0
    Views:
    328
    2obvious
    Sep 13, 2004
  3. 2obvious
    Replies:
    2
    Views:
    383
    2obvious
    Jan 11, 2005
  4. DC Gringo

    referencing controls in controls

    DC Gringo, Sep 15, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    139
    DC Gringo
    Sep 16, 2004
  5. DC Gringo

    referencing controls in controls

    DC Gringo, Sep 15, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    137
    DC Gringo
    Sep 16, 2004
Loading...

Share This Page