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 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
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
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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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'
|
by: Praveen |
last post by:
How to get rid of this error
|
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)
|
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...
|
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;
| |
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,
|
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?
|
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?
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |