Convert an object to a control, or what?

M

Magnus Blomberg

Hello!

An common issue for me is that I have a web control, double-click it to
reach the event in the code-behind. Then I for example get the code:
protected void CheckBox1_CheckedChanged(Object sender , EventArgs e)

This is good, but if I then want to use the sender as a control, for
instance the methods are not for the Checkbox, but for the Object.
This is quite obvious, but how to solve it?
I can't (or it's not recommended to) write if (sender.Checked == true)...
Should I convert it, and if so, how?

Regards Magnus
 
M

Mr Newbie

Easy

Dim myControlName as ControlTypeLikeTexBoxOrWhaterver

myControlName = DirectCast( sender, ControlTypeLikeTexBoxOrWhaterver )

myControlName.WhateverPropertyYouWantToSetIsNow = "OK"
 
M

Mark Monster

Hello Magnus,

I think it is preferred to first check the type before casting the instance
to another Class.

Regards,

Mark Monster
 
M

Magnus Blomberg

Thanks!
The problem is that this function is VB, and I'm using C#. Do you now
the opposit for C#?

Regards Magnus
 
S

S. Justin Gengo

Magnus,

In C# you use an "Explicit Cast"" so working from the previous example:

ControlTypeLikeTextBoxOrWhatever myControlName;

myControlName = (ControlTypeLikeTextBoxOrWhatever) sender;

myControlName.WhateverPropertyYouWantToSetIsNow = "OK";

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top