FindControl

Discussion in 'ASP .Net' started by tma, Aug 9, 2004.

  1. tma

    tma Guest

    My code below returns the .text property of the chkEqmtRental check box but
    not it's checked state. I'm creating the control in a placeholder and need
    to access the check box in the codebehind. Also, is it possible to access
    events for that control and if so, how?
    chkEqmtRental = plh.FindControl("chkEqmtRental")
    tma, Aug 9, 2004
    #1
    1. Advertising

  2. You code below assigns the checkbox (assuming it is found) to a variable. As
    Checked is a property of the checkbox, you should have no trouble accessing
    it. So, you might want to explain what you're trying to say.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "tma" <> wrote in message
    news:...
    > My code below returns the .text property of the chkEqmtRental check box

    but
    > not it's checked state. I'm creating the control in a placeholder and need
    > to access the check box in the codebehind. Also, is it possible to access
    > events for that control and if so, how?
    > chkEqmtRental = plh.FindControl("chkEqmtRental")
    >
    >
    >
    >
    >
    >
    Kevin Spencer, Aug 9, 2004
    #2
    1. Advertising

  3. tma

    tma Guest

    This what I'm trying to do:


    Dim chkEqmtRental As WebControls.CheckBox

    chkEqmtRental = plh.FindControl("chkEqmtRental")

    If chkEqmtRental.Checked Then

    ....some code...

    End If

    Presently, the chkEqmtRental.Checked always evaluates to FALSE.




    "Kevin Spencer" <> wrote in message
    news:...
    > You code below assigns the checkbox (assuming it is found) to a variable.

    As
    > Checked is a property of the checkbox, you should have no trouble

    accessing
    > it. So, you might want to explain what you're trying to say.
    >
    > --
    > HTH,
    > Kevin Spencer
    > .Net Developer
    > Microsoft MVP
    > Big things are made up
    > of lots of little things.
    >
    > "tma" <> wrote in message
    > news:...
    > > My code below returns the .text property of the chkEqmtRental check box

    > but
    > > not it's checked state. I'm creating the control in a placeholder and

    need
    > > to access the check box in the codebehind. Also, is it possible to

    access
    > > events for that control and if so, how?
    > > chkEqmtRental = plh.FindControl("chkEqmtRental")
    > >
    > >
    > >
    > >
    > >
    > >

    >
    >
    tma, Aug 9, 2004
    #3
  4. hi tma,

    I think that the problem is there because you don't cast the finded control
    to a checkbox
    so the code should be:

    Dim chkEqmtRental as CheckBox = CType(plh.FindControl("chkEqmtRental"),
    CheckBox)
    chkEqmtRental.Text 'for text property
    chkEqmtRental.Checked 'for checked state

    what do you mean by acces the event ?

    Regards
    Martin
    "tma" <> wrote in message
    news:...
    > My code below returns the .text property of the chkEqmtRental check box

    but
    > not it's checked state. I'm creating the control in a placeholder and need
    > to access the check box in the codebehind. Also, is it possible to access
    > events for that control and if so, how?
    > chkEqmtRental = plh.FindControl("chkEqmtRental")
    >
    >
    >
    >
    >
    >
    Martin Marinov, Aug 9, 2004
    #4
  5. tma

    tma Guest

    With regard to events, I'm asking if it's possible to gain access to events
    in the codebehind for controls added via placeholder.


    "Martin Marinov" <> wrote in message
    news:...
    > hi tma,
    >
    > I think that the problem is there because you don't cast the finded

    control
    > to a checkbox
    > so the code should be:
    >
    > Dim chkEqmtRental as CheckBox = CType(plh.FindControl("chkEqmtRental"),
    > CheckBox)
    > chkEqmtRental.Text 'for text property
    > chkEqmtRental.Checked 'for checked state
    >
    > what do you mean by acces the event ?
    >
    > Regards
    > Martin
    > "tma" <> wrote in message
    > news:...
    > > My code below returns the .text property of the chkEqmtRental check box

    > but
    > > not it's checked state. I'm creating the control in a placeholder and

    need
    > > to access the check box in the codebehind. Also, is it possible to

    access
    > > events for that control and if so, how?
    > > chkEqmtRental = plh.FindControl("chkEqmtRental")
    > >
    > >
    > >
    > >
    > >
    > >

    >
    >
    tma, Aug 9, 2004
    #5
  6. First, I would advise you to turn Option Strict ON. It will prevent data
    type errors like this. Every Checkbox has the same properties. However, the
    FindControl() method returns a type of System.Web.UI.Control, not
    System.Web.UI.WebControls.CheckBox. You have to cast it as a CheckBox in
    order to access the CheckBox properties.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "tma" <> wrote in message
    news:...
    > This what I'm trying to do:
    >
    >
    > Dim chkEqmtRental As WebControls.CheckBox
    >
    > chkEqmtRental = plh.FindControl("chkEqmtRental")
    >
    > If chkEqmtRental.Checked Then
    >
    > ...some code...
    >
    > End If
    >
    > Presently, the chkEqmtRental.Checked always evaluates to FALSE.
    >
    >
    >
    >
    > "Kevin Spencer" <> wrote in message
    > news:...
    > > You code below assigns the checkbox (assuming it is found) to a

    variable.
    > As
    > > Checked is a property of the checkbox, you should have no trouble

    > accessing
    > > it. So, you might want to explain what you're trying to say.
    > >
    > > --
    > > HTH,
    > > Kevin Spencer
    > > .Net Developer
    > > Microsoft MVP
    > > Big things are made up
    > > of lots of little things.
    > >
    > > "tma" <> wrote in message
    > > news:...
    > > > My code below returns the .text property of the chkEqmtRental check

    box
    > > but
    > > > not it's checked state. I'm creating the control in a placeholder and

    > need
    > > > to access the check box in the codebehind. Also, is it possible to

    > access
    > > > events for that control and if so, how?
    > > > chkEqmtRental = plh.FindControl("chkEqmtRental")
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Kevin Spencer, Aug 9, 2004
    #6
  7. tma

    Scott Allen Guest

    Hi tma:

    Since you mentioned using a PlaceHolder control earlier I'm thinking
    you might be running into problems creating controls dynamically and
    adding them to the page.

    The following KB article shows how to dynamically add a control and
    add an event handler:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;317515

    Notice it also adds the control during Page_Init, which is before
    Page_Load, as this can impact how the control behaves and remembers
    it's state.

    HTH,

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

    On Mon, 9 Aug 2004 07:17:26 -0500, "tma" <>
    wrote:

    >My code below returns the .text property of the chkEqmtRental check box but
    >not it's checked state. I'm creating the control in a placeholder and need
    >to access the check box in the codebehind. Also, is it possible to access
    >events for that control and if so, how?
    >chkEqmtRental = plh.FindControl("chkEqmtRental")
    >
    >
    >
    >
    >
    Scott Allen, Aug 9, 2004
    #7
  8. tma

    tma Guest

    With Option Strict On and the following lines, I still get no value for
    ..checked. I can define the chkEqmtRental as webcontrols.checkbox or just
    checkbox and same result. I'm at a loss. Is anyone able to make this work?
    Could you post a code sample?

    table = new htmltable

    cell = new htmltablecell

    row = new htmltablerow

    chk = New WebControls.CheckBox

    chk.AutoPostBack = True

    chk.Attributes.Add("runat", "server")

    chk.Text = "Equipment rental"

    chk.ToolTip = "Player requested rental equipment."

    chk.ID = "chkEqmtRental"

    cell.Controls.Add(chk)

    row.Controls.Add(cell)

    table.Controls.Add(row)

    plh.controls.add(table)



    chkEqmtRental = CType(plh.FindControl("chkEqmtRental"), CheckBox)

    Cast does not return a true for .checked when box is checked.





    "Kevin Spencer" <> wrote in message
    news:...
    > First, I would advise you to turn Option Strict ON. It will prevent data
    > type errors like this. Every Checkbox has the same properties. However,

    the
    > FindControl() method returns a type of System.Web.UI.Control, not
    > System.Web.UI.WebControls.CheckBox. You have to cast it as a CheckBox in
    > order to access the CheckBox properties.
    >
    > --
    > HTH,
    > Kevin Spencer
    > .Net Developer
    > Microsoft MVP
    > Big things are made up
    > of lots of little things.
    >
    > "tma" <> wrote in message
    > news:...
    > > This what I'm trying to do:
    > >
    > >
    > > Dim chkEqmtRental As WebControls.CheckBox
    > >
    > > chkEqmtRental = plh.FindControl("chkEqmtRental")
    > >
    > > If chkEqmtRental.Checked Then
    > >
    > > ...some code...
    > >
    > > End If
    > >
    > > Presently, the chkEqmtRental.Checked always evaluates to FALSE.
    > >
    > >
    > >
    > >
    > > "Kevin Spencer" <> wrote in message
    > > news:...
    > > > You code below assigns the checkbox (assuming it is found) to a

    > variable.
    > > As
    > > > Checked is a property of the checkbox, you should have no trouble

    > > accessing
    > > > it. So, you might want to explain what you're trying to say.
    > > >
    > > > --
    > > > HTH,
    > > > Kevin Spencer
    > > > .Net Developer
    > > > Microsoft MVP
    > > > Big things are made up
    > > > of lots of little things.
    > > >
    > > > "tma" <> wrote in message
    > > > news:...
    > > > > My code below returns the .text property of the chkEqmtRental check

    > box
    > > > but
    > > > > not it's checked state. I'm creating the control in a placeholder

    and
    > > need
    > > > > to access the check box in the codebehind. Also, is it possible to

    > > access
    > > > > events for that control and if so, how?
    > > > > chkEqmtRental = plh.FindControl("chkEqmtRental")
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    tma, Aug 9, 2004
    #8
    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. George Durzi

    OnEditCommand - .FindControl Returning Null

    George Durzi, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    480
    George Durzi
    Jul 14, 2003
  2. Ravikanth[MVP]

    FindControl

    Ravikanth[MVP], Jul 18, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    443
    Yan-Hong Huang[MSFT]
    Jul 22, 2003
  3. James G. Beldock
    Replies:
    1
    Views:
    9,304
    Kevin Spencer
    Aug 7, 2003
  4. Ivan Demkovitch
    Replies:
    1
    Views:
    813
    Ivan Demkovitch
    Oct 7, 2003
  5. ani

    FindControl method doesn't work

    ani, Nov 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    362
Loading...

Share This Page