Retrieve htmlbutton ID from placeholder

J

John Mason

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.
 
E

Eliyahu Goldin

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
 
J

John Mason

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.
 
G

Guest

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
 
J

John Mason

System.Web.UI.HtmlControls.HtmlButton

Of course! How silly of me...


Thank-you Shaw.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top