473,664 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot convert type 'System.Web.UI. Control' to ...

Hi,

I am trying to write some server side validator code in C#. I want to
have a piece of code that can be used by all DropDownLists on a
webform, giving a false value if the SelectedIndex is 0.

Here's the code:

protected void serverValidateC boBx(object source,
ServerValidateE ventArgs args)
{
DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate);

if(dl.SelectedI ndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

But i get the build error

Cannot implicitly convert type 'System.Web.UI. Control' to
'System.Web.UI. WebControls.Dro pDownList'

and the FindControl syntax is underlined.

Any suggestions on ways to solve this?

Thanks

Nov 17 '05 #1
7 13971
Hi,

FindControl returns a Control, you need a cast into a DropDownList (if you
know it's gonna be a DroDownList):

DropDownList dl = (DropDownList)
FindControl(((C ustomValiator)( source)).Contro lToValidate);

Regards - Octavio

"Assimalyst " <c_******@hotma il.com> escribió en el mensaje
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi,

I am trying to write some server side validator code in C#. I want to
have a piece of code that can be used by all DropDownLists on a
webform, giving a false value if the SelectedIndex is 0.

Here's the code:

protected void serverValidateC boBx(object source,
ServerValidateE ventArgs args)
{
DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate);

if(dl.SelectedI ndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

But i get the build error

Cannot implicitly convert type 'System.Web.UI. Control' to
'System.Web.UI. WebControls.Dro pDownList'

and the FindControl syntax is underlined.

Any suggestions on ways to solve this?

Thanks

Nov 17 '05 #2
Hi,

FindControl returns a Control, not a DropDownList , do this:

DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate) as DropDownList ;
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Assimalyst " <c_******@hotma il.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi,

I am trying to write some server side validator code in C#. I want to
have a piece of code that can be used by all DropDownLists on a
webform, giving a false value if the SelectedIndex is 0.

Here's the code:

protected void serverValidateC boBx(object source,
ServerValidateE ventArgs args)
{
DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate);

if(dl.SelectedI ndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

But i get the build error

Cannot implicitly convert type 'System.Web.UI. Control' to
'System.Web.UI. WebControls.Dro pDownList'

and the FindControl syntax is underlined.

Any suggestions on ways to solve this?

Thanks

Nov 17 '05 #3
Hi,
The problem with this , is that if the control returned is not a
DropDownList you will get an exception, if you use "as" you will get a null
value , which you can check for after.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Octavio Hernandez" <do****@danysof t.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

FindControl returns a Control, you need a cast into a DropDownList (if you
know it's gonna be a DroDownList):

DropDownList dl = (DropDownList)
FindControl(((C ustomValiator)( source)).Contro lToValidate);

Regards - Octavio

"Assimalyst " <c_******@hotma il.com> escribió en el mensaje
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi,

I am trying to write some server side validator code in C#. I want to
have a piece of code that can be used by all DropDownLists on a
webform, giving a false value if the SelectedIndex is 0.

Here's the code:

protected void serverValidateC boBx(object source,
ServerValidateE ventArgs args)
{
DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate);

if(dl.SelectedI ndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

But i get the build error

Cannot implicitly convert type 'System.Web.UI. Control' to
'System.Web.UI. WebControls.Dro pDownList'

and the FindControl syntax is underlined.

Any suggestions on ways to solve this?

Thanks


Nov 17 '05 #4
Can you not pass in the type and use that to cast it?

Get its parent from the control and then cast it wil that.
Lewis
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:Ov******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
The problem with this , is that if the control returned is not a
DropDownList you will get an exception, if you use "as" you will get a
null value , which you can check for after.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Octavio Hernandez" <do****@danysof t.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

FindControl returns a Control, you need a cast into a DropDownList (if
you know it's gonna be a DroDownList):

DropDownList dl = (DropDownList)
FindControl(((C ustomValiator)( source)).Contro lToValidate);

Regards - Octavio

"Assimalyst " <c_******@hotma il.com> escribió en el mensaje
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi,

I am trying to write some server side validator code in C#. I want to
have a piece of code that can be used by all DropDownLists on a
webform, giving a false value if the SelectedIndex is 0.

Here's the code:

protected void serverValidateC boBx(object source,
ServerValidateE ventArgs args)
{
DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate);

if(dl.SelectedI ndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

But i get the build error

Cannot implicitly convert type 'System.Web.UI. Control' to
'System.Web.UI. WebControls.Dro pDownList'

and the FindControl syntax is underlined.

Any suggestions on ways to solve this?

Thanks



Nov 17 '05 #5
Thank you both.

Nov 17 '05 #6
Hi,

You can do so, it's just that a casting does fail with an exception if the
casting can not be done, the "is" operator returns null, which you should
always check in this case anyway.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Lewis Harvey" <as***@asdas.co m> wrote in message
news:O6******** ******@TK2MSFTN GP12.phx.gbl...
Can you not pass in the type and use that to cast it?

Get its parent from the control and then cast it wil that.
Lewis
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote in message news:Ov******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
The problem with this , is that if the control returned is not a
DropDownList you will get an exception, if you use "as" you will get a
null value , which you can check for after.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Octavio Hernandez" <do****@danysof t.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

FindControl returns a Control, you need a cast into a DropDownList (if
you know it's gonna be a DroDownList):

DropDownList dl = (DropDownList)
FindControl(((C ustomValiator)( source)).Contro lToValidate);

Regards - Octavio

"Assimalyst " <c_******@hotma il.com> escribió en el mensaje
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi,

I am trying to write some server side validator code in C#. I want to
have a piece of code that can be used by all DropDownLists on a
webform, giving a false value if the SelectedIndex is 0.

Here's the code:

protected void serverValidateC boBx(object source,
ServerValidateE ventArgs args)
{
DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate);

if(dl.SelectedI ndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

But i get the build error

Cannot implicitly convert type 'System.Web.UI. Control' to
'System.Web.UI. WebControls.Dro pDownList'

and the FindControl syntax is underlined.

Any suggestions on ways to solve this?

Thanks



Nov 17 '05 #7
Ignacio,

If the CustomValidator is gonna be attached only to DropDownList controls,
you can tell for sure FindControl will return a DropDownList. In that case
the typecast is 100% safe.

Regards - Octavio

*************** *************** *************** *************** **+

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
escribió en el mensaje news:Ov******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
The problem with this , is that if the control returned is not a
DropDownList you will get an exception, if you use "as" you will get a
null value , which you can check for after.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Octavio Hernandez" <do****@danysof t.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

FindControl returns a Control, you need a cast into a DropDownList (if
you know it's gonna be a DroDownList):

DropDownList dl = (DropDownList)
FindControl(((C ustomValiator)( source)).Contro lToValidate);

Regards - Octavio

"Assimalyst " <c_******@hotma il.com> escribió en el mensaje
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi,

I am trying to write some server side validator code in C#. I want to
have a piece of code that can be used by all DropDownLists on a
webform, giving a false value if the SelectedIndex is 0.

Here's the code:

protected void serverValidateC boBx(object source,
ServerValidateE ventArgs args)
{
DropDownList dl =
FindControl(((C ustomValiator)( source)).Contro lToValidate);

if(dl.SelectedI ndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

But i get the build error

Cannot implicitly convert type 'System.Web.UI. Control' to
'System.Web.UI. WebControls.Dro pDownList'

and the FindControl syntax is underlined.

Any suggestions on ways to solve this?

Thanks



Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14951
by: Anita C | last post by:
I have the foll. code to update the value of an attribute: xmlDocument.Load("abc.xml"); XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value = ddDes.SelectedItem.ToString(); xmlDocument.Save("abc.xml"); However, I get the foll. error: Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlAttribute'
3
7488
by: Praveen | last post by:
How to get rid of this error
2
10899
by: Simon Harris | last post by:
I have created a web service, which when I call in my browser presents the text form etc. When I click the button, I get this error: System.ArgumentException: Cannot convert to System.Int32. Parameter name: type ---> System.FormatException: Input string was not in a correct format. at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
6
14555
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web server. I am able to log in. However after publishing the page to my local IIS, it results in the below error. This error is occurring on the Visual Studio.Net 2k5 release version, but did not occur on the beta 2, same code. A point of interest, is...
2
14798
by: Paul Hemans | last post by:
I am very new at .Net. I have a small project where I need to manipulate the contents of a web page. I have a form with a web browser control (webBrowser1) on it. Within the webBrowser1_DocumentCompleted method I have the following code. mshtml.HTMLDocument oDoc = new HTMLDocumentClass(); oDoc = (mshtml.HTMLDocument)webBrowser1.Document;
2
5005
by: keithb | last post by:
A web site published from its development environment (VS Web Server) to an IIS 6.0 site displays this message: Compiler Error Message: CS0030: Cannot convert type 'ASP.login_aspx' to 'System.Web.UI.WebControls.Login' Anyone have an idea about the cause? Thanks,
6
30171
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
2
10260
by: Paulo | last post by:
DataRow dr = (DataRow)ds.Tables.Rows.ItemArray; Error 1 Cannot convert type 'object' to 'System.Data.DataRow' C:\Documents and Settings\Fabio\Meus documentos\Visual Studio 2005\Projects\CodBarraPalm\CodBarraPalm\Default.aspx.cs 35 26 CodBarraPalm ? why this error trying to get the contents of a row on a DataSet?
4
25369
by: clflyer | last post by:
I have a small problem. I'm pretty much a newbie to C# coming out of VB.Net. I have a windows form that has a GroupBox named grpSoldTo. I want to be able to clear all the textboxes and comboxes that are in the GroupBox. I know how to do it in VB.Net but when I try something similar in C# I get the following error: Cannot implicitly convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ComboBox'. An explicit conversion...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.