dynamically adding controls to a datagrid

Discussion in 'ASP .Net' started by MattB, Jan 17, 2005.

  1. MattB

    MattB Guest

    Hello. I'm creating a page to edit a person's record (name address, etc)
    using a DataGrid. I want the fields that can be edited to be created at
    runtime based on a list of columns in web.config. This is working fine,
    and I can add a control (TextBox or DropDownList) dynamically for the
    user to fill in a value. This works well for just adding controls for
    data entry.

    Now, I want to add validators on the fly as well and it not going quite
    as smoothly. I'm suing the following excerpt in the ItemDataBound event
    for this DataGrid:

    Dim tb As New TextBox, vld As New RequiredFieldValidator

    tb.Text = Convert.ToString(e.Item.DataItem("val"))
    editCell.Controls.Add(tb)
    If e.Item.DataItem("req") Then
    vld.ControlToValidate = FindControl(tb.ID.ToString)
    vld.ErrorMessage = "Required Field!"
    editCell.Controls.Add(vld)
    End If

    IntelliSense is flagging the FindControl part saying:

    "System.Web.UI.Control" cannot be converted to string

    Is this a reasonabler approach that I just need to fix the syntax of, or
    am I taking the wrong approach? Any suggestions? Thanks!

    Matt
     
    MattB, Jan 17, 2005
    #1
    1. Advertising

  2. MattB

    Karl Seguin Guest

    Just to vld.ControlToValidated = tb.ID no need to do a FindControl on it,
    you already found it :p it's tb...you created it...

    You might need to use ClientId or UniqueId instead of just ID...not 100%
    sure...but just a heads up...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "MattB" <> wrote in message
    news:...
    > Hello. I'm creating a page to edit a person's record (name address, etc)
    > using a DataGrid. I want the fields that can be edited to be created at
    > runtime based on a list of columns in web.config. This is working fine,
    > and I can add a control (TextBox or DropDownList) dynamically for the
    > user to fill in a value. This works well for just adding controls for
    > data entry.
    >
    > Now, I want to add validators on the fly as well and it not going quite
    > as smoothly. I'm suing the following excerpt in the ItemDataBound event
    > for this DataGrid:
    >
    > Dim tb As New TextBox, vld As New RequiredFieldValidator
    >
    > tb.Text = Convert.ToString(e.Item.DataItem("val"))
    > editCell.Controls.Add(tb)
    > If e.Item.DataItem("req") Then
    > vld.ControlToValidate = FindControl(tb.ID.ToString)
    > vld.ErrorMessage = "Required Field!"
    > editCell.Controls.Add(vld)
    > End If
    >
    > IntelliSense is flagging the FindControl part saying:
    >
    > "System.Web.UI.Control" cannot be converted to string
    >
    > Is this a reasonabler approach that I just need to fix the syntax of, or
    > am I taking the wrong approach? Any suggestions? Thanks!
    >
    > Matt
     
    Karl Seguin, Jan 17, 2005
    #2
    1. Advertising

  3. MattB

    MattB Guest

    Karl Seguin wrote:
    > Just to vld.ControlToValidated = tb.ID no need to do a FindControl on it,
    > you already found it :p it's tb...you created it...
    >
    > You might need to use ClientId or UniqueId instead of just ID...not 100%
    > sure...but just a heads up...
    >
    > Karl
    >


    Thanks for the reply, but I was trying FindControl because tb.ID didn't
    work (Intellisense said it was ok, but it failed at runtime).

    So here's the runtime errors I get for each of those suggestions:

    tb.ID:
    The ControlToValidate property of '' cannot be blank.

    tb.UniqueID:
    Unable to find control id 'dgGuest:_ctl2:_ctl0' referenced by the
    'ControlToValidate' property of ''.

    tb.ClientID:
    Unable to find control id 'dgGuest__ctl2__ctl0' referenced by the
    'ControlToValidate' property of ''.

    So could it be that in adding a second control to the same cell I'm
    somehow overwriting the tb control? Any other ideas?

    THANKS!

    Matt
     
    MattB, Jan 17, 2005
    #3
  4. MattB

    Karl Seguin Guest

    You can simply use ID if you give the tb an id before adding it to the cell

    dim tb as new TextBox
    tb.ID = "txt"


    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "MattB" <> wrote in message
    news:...
    > Karl Seguin wrote:
    > > Just to vld.ControlToValidated = tb.ID no need to do a FindControl on

    it,
    > > you already found it :p it's tb...you created it...
    > >
    > > You might need to use ClientId or UniqueId instead of just ID...not 100%
    > > sure...but just a heads up...
    > >
    > > Karl
    > >

    >
    > Thanks for the reply, but I was trying FindControl because tb.ID didn't
    > work (Intellisense said it was ok, but it failed at runtime).
    >
    > So here's the runtime errors I get for each of those suggestions:
    >
    > tb.ID:
    > The ControlToValidate property of '' cannot be blank.
    >
    > tb.UniqueID:
    > Unable to find control id 'dgGuest:_ctl2:_ctl0' referenced by the
    > 'ControlToValidate' property of ''.
    >
    > tb.ClientID:
    > Unable to find control id 'dgGuest__ctl2__ctl0' referenced by the
    > 'ControlToValidate' property of ''.
    >
    > So could it be that in adding a second control to the same cell I'm
    > somehow overwriting the tb control? Any other ideas?
    >
    > THANKS!
    >
    > Matt
     
    Karl Seguin, Jan 17, 2005
    #4
  5. MattB

    MattB Guest

    Karl Seguin wrote:
    > You can simply use ID if you give the tb an id before adding it to the cell
    >
    > dim tb as new TextBox
    > tb.ID = "txt"
    >
    >
    > Karl
    >


    Cool. I actually just stumbled on that myself. Thanks again!

    Matt
     
    MattB, Jan 17, 2005
    #5
    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. John Wallace
    Replies:
    0
    Views:
    341
    John Wallace
    Aug 6, 2003
  2. MC D
    Replies:
    4
    Views:
    473
    Big D
    Nov 18, 2003
  3. Uzi Baruch
    Replies:
    0
    Views:
    445
    Uzi Baruch
    Dec 17, 2003
  4. Jonas
    Replies:
    0
    Views:
    169
    Jonas
    Sep 23, 2003
  5. Replies:
    1
    Views:
    341
Loading...

Share This Page