Retrieve htmlbutton ID from placeholder

Discussion in 'ASP .Net' started by John Mason, Jan 21, 2005.

  1. John Mason

    John Mason Guest

    Hi!

    I hope someone can assist... this has been driving me crazy!!

    I have dynamically added several htmlbuttons to a placeholder control
    using...
    AddHandler btnReports.ServerClick, AddressOf Get_Date_Range

    In the Get_Date_Range sub, I am trying to find out which button was
    clicked. Each button I dynamically created has a unique ID and text
    value.

    I need to get the ID of the clicked button to pass to another
    sub-routine. The number of htmlbutton's within this placeholder does
    vary depending on the current user's role (role-based forms
    authentication).

    Any advice would be very much appreciated!!!

    Thanks,

    John.


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    John Mason, Jan 21, 2005
    #1
    1. Advertising

  2. John,

    You don't need to retrieve the clicked button id from the placeholder. It
    comes to Get_Date_Range as the first parameter in the function call. It's
    always the event sender. Typecast to your button type.

    Eliyahu

    "John Mason" <> wrote in message
    news:%23TrQpd6$...
    > Hi!
    >
    > I hope someone can assist... this has been driving me crazy!!
    >
    > I have dynamically added several htmlbuttons to a placeholder control
    > using...
    > AddHandler btnReports.ServerClick, AddressOf Get_Date_Range
    >
    > In the Get_Date_Range sub, I am trying to find out which button was
    > clicked. Each button I dynamically created has a unique ID and text
    > value.
    >
    > I need to get the ID of the clicked button to pass to another
    > sub-routine. The number of htmlbutton's within this placeholder does
    > vary depending on the current user's role (role-based forms
    > authentication).
    >
    > Any advice would be very much appreciated!!!
    >
    > Thanks,
    >
    > John.
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Eliyahu Goldin, Jan 21, 2005
    #2
    1. Advertising

  3. John Mason

    John Mason Guest

    Hi Eliyahu

    Thank-you very much for your response.

    Here is the placeholder code snipet:

    Do While objDataReader.Read()
    btnReports = New HtmlButton()
    btnReports.Attributes.Add("class","btnClass")
    btnReports.InnerHtml = objDataReader("DistrictName")
    btnReports.ID = objDataReader("DistrictID")
    AddHandler btnReports.ServerClick, AddressOf Get_District_Report
    DistrictButtons.Controls.Add(btnReports)
    Loop

    Here is the Get_District_Report sub:

    Sub Get_District_Report(sender As Object, e As System.EventArgs)
    Dim b As System.Web.UI.WebControls.Button
    Dim DistrictID as String
    b = CType(sender, System.Web.UI.WebControls.Button)
    DistrictID = b.ID

    When I run this and click on the placeholder button, I get an error...

    System.InvalidCastException: Specified cast is not valid.

    This error refers to the line that begins with:

    b = Ctype...

    Any ideas?

    Thanks,

    John.




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    John Mason, Jan 21, 2005
    #3
  4. Certainly, HtmlButton and WebControls.Button are not same classes, and do not
    have sub-base class relationship. The type cast will be failed. I guess,
    you’ve already figured out the problem.

    Shaw


    "John Mason" wrote:

    > Hi Eliyahu
    >
    > Thank-you very much for your response.
    >
    > Here is the placeholder code snipet:
    >
    > Do While objDataReader.Read()
    > btnReports = New HtmlButton()
    > btnReports.Attributes.Add("class","btnClass")
    > btnReports.InnerHtml = objDataReader("DistrictName")
    > btnReports.ID = objDataReader("DistrictID")
    > AddHandler btnReports.ServerClick, AddressOf Get_District_Report
    > DistrictButtons.Controls.Add(btnReports)
    > Loop
    >
    > Here is the Get_District_Report sub:
    >
    > Sub Get_District_Report(sender As Object, e As System.EventArgs)
    > Dim b As System.Web.UI.WebControls.Button
    > Dim DistrictID as String
    > b = CType(sender, System.Web.UI.WebControls.Button)
    > DistrictID = b.ID
    >
    > When I run this and click on the placeholder button, I get an error...
    >
    > System.InvalidCastException: Specified cast is not valid.
    >
    > This error refers to the line that begins with:
    >
    > b = Ctype...
    >
    > Any ideas?
    >
    > Thanks,
    >
    > John.
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    >
    =?Utf-8?B?U2hhdw==?=, Jan 21, 2005
    #4
  5. John Mason

    John Mason Guest

    System.Web.UI.HtmlControls.HtmlButton

    Of course! How silly of me...


    Thank-you Shaw.



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    John Mason, Jan 22, 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. Anita C

    HtmlButton

    Anita C, Aug 23, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    473
    Anita C
    Aug 23, 2003
  2. Michael
    Replies:
    1
    Views:
    497
    Eliyahu Goldin
    Dec 7, 2004
  3. Bob
    Replies:
    3
    Views:
    4,840
  4. Kees de Winter
    Replies:
    3
    Views:
    967
    Karl Seguin [MVP]
    May 12, 2006
  5. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    332
Loading...

Share This Page