Convert an object to a control, or what?

  • Thread starter Thread starter Magnus Blomberg
  • Start date Start date
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
 
Easy

Dim myControlName as ControlTypeLikeTexBoxOrWhaterver

myControlName = DirectCast( sender, ControlTypeLikeTexBoxOrWhaterver )

myControlName.WhateverPropertyYouWantToSetIsNow = "OK"
 
Hello Magnus,

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

Regards,

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

Regards Magnus
 
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

Back
Top