473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Calling a method from a dropdown control

If I run this code below in ASP.NET 2.0 it fails with an "Object
Required" error and I have no idea why.

Could someone tell me why this is failing? Then if I can get an
answer to that, I would actually prefer to run a C# method instead of
a javascript one and would like to figure out how to set that up (but
I get the same error doing that too).
<select id="cbCarriers" onchange="javascript: test();">
<option value='241'>3GP</option>
<option value='441'>AAC</option>
<option value='1115'>AB1</option>
</select>
<script type="javascript">
function test()
{
alert("this");
}
</script>

Jul 13 '07 #1
22 1799
"Doogie" <dn******@dtgnet.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
If I run this code below in ASP.NET 2.0 it fails with an "Object
Required" error and I have no idea why.
What happens if you change

<select id="cbCarriers" onchange="javascript: test();">

to

<select id="cbCarriers" onchange="test();">

and change

<script type="javascript">

to

<script type="text/javascript">
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 13 '07 #2
couple things to try.

theres a space after the javascript:

alert('this');

i dont remember what double quotes do in javascript, but singles are
generally what you want. doubles might be trying to convert your
object reference to a string. functions are objects in javascript,
and this is a special variable which references that object.

to do it in your code behind you'll want to set the autopostback to
true and then make a selected Index changed event. you won't be
having an javascript alert popup in your code behind though.

Jul 13 '07 #3
thought i posted, but its not showing up.

try these:
seems to be a space after javascript: and before test();
alert('blah');

dont know what double quotes do in javascript, but it could be trying
to call the tostring method of your function object. functions are
objects in javascript, this is a reference to that functions object.
I don't remember double quote behavior though.

try it with single quotes, and with a non reserved word.

Jul 13 '07 #4
and change
>
<script type="javascript">

to

<script type="text/javascript">
This solved my problem. But my next issue is that I really would
prefer to call a .NET method instead of a javascript method from this
control. Is that possible?

Jul 13 '07 #5
"Doogie" <dn******@dtgnet.comwrote in message
news:11**********************@m3g2000hsh.googlegro ups.com...
This solved my problem.
Excellent.
But my next issue is that I really would prefer to call a .NET method
instead of a javascript method from this control. Is that possible?
Easiest way is to use an <asp:DropDownListcontrol and then wire up the
OnSelectedIndexChanged event:
http://msdn2.microsoft.com/en-us/lib...st(vs.80).aspx

Don't forget to set AutoPostBack to "True" as well...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 13 '07 #6
>
Easiest way is to use an <asp:DropDownListcontrol and then wire up the
OnSelectedIndexChanged event:http://msdn2.microsoft.com/en-us/lib...webcontrols.dr...

Don't forget to set AutoPostBack to "True" as well...
I may have to do that, but the reason I was using this control was
because our company has a common method that allows you to query a DB,
get the results back and it builds a HTML string with the results
nicely put into a select tag. I was going to use that, but I need to
get back into .net though for another data call that uses the value
the users selects from this control.

Jul 13 '07 #7
"Doogie" <dn******@dtgnet.comwrote in message
news:11********************@q75g2000hsh.googlegrou ps.com...

Easiest way is to use an <asp:DropDownListcontrol and then wire up the
OnSelectedIndexChanged
event:http://msdn2.microsoft.com/en-us/lib...webcontrols.dr...

Don't forget to set AutoPostBack to "True" as well...

I may have to do that, but the reason I was using this control was
because our company has a common method that allows you to query a DB,
get the results back and it builds a HTML string with the results
nicely put into a select tag.
ASP.NET does that natively for you:
http://www.google.co.uk/search?sourc...st+Databinding
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 13 '07 #8
ASP.NET does that natively for you:http://www.google.co.uk/search?sourc...hl=en-GB&ie=UT...
Right, but here's what this common method is doing...you pass in the
connection string, and your stored proc name (and parameters, etc) and
it returns a string that would look something like this:

"<select id="cbCarriers" onchange="javascript: test();"><option
value='241'>3GP</option><option value='441'>AAC</option><option
value='1115'>AB1</option></select>"

I then assigned this string to a protected variable (named
_carrierDropDown) inside .NET and can reference it in my HTML like so:

<%=_carrierDropDown%>

and when I run my app, my control is built with not a lot of code on
my part. The problem is that I now need the selected value from this
control inside .NET when I go to do another DB call (That's why I was
placing that onchange method in the tag). Since this control is A)
built dynamically and B) is a client side control, I'm thinking it
will be very difficult to get the value in the way I need to.

FYI...a lot of what is done here is with javascript/client side
controls vs. server side as the belief is that it performs better for
the user who uses the app outside of our network. Thus why the
control they built with this method is a client one and not a server
one (like the dropdown list you mentioned). Not sure if that is an
accurate belief, but I'm fairly new to web development so don't have
much to go on.

Jul 13 '07 #9
"Doogie" <dn******@dtgnet.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
and when I run my app, my control is built with not a lot of code on
my part.
ASP.NET, with a DAL, does it in two lines:

MyDDL.DataSource = MyDAL.GetDataSet(strConnection, spName, lstParameters);
MyDDL.DataBind();
The problem is that I now need the selected value from this
control inside .NET when I go to do another DB call (That's why I was
placing that onchange method in the tag). Since this control is A)
built dynamically and B) is a client side control, I'm thinking it
will be very difficult to get the value in the way I need to.
You're right! However, if you don't go through all that rigmarole and just
use the ASP.NET webcontrols in the way they were designed to be used, none
of the above is necessary at all... And the selected value of the
<asp:DropDownListwill be as easy as MyDDL.SelectedValue...
FYI...a lot of what is done here is with javascript/client side
controls vs. server side as the belief is that it performs better for
the user who uses the app outside of our network. Thus why the
control they built with this method is a client one and not a server
one (like the dropdown list you mentioned). Not sure if that is an
accurate belief, but I'm fairly new to web development so don't have
much to go on.
Obviously it's impossible to tell without seeing your code...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 13 '07 #10
I went ahead and decided to go with the server side dropdownlist, but
now I'm running into a whole other problem that isn't related to this
anymore, but was wondering if I could get your help too (I may post on
another message too).

This dropdownlist sits inside a user control that also has a command
button to execute a db call. So I'm trying to figure out a way inside
the page that uses this control, how I get access to the selected
value within this dropdownlist. So I created a method like this in my
user control:

public partial class SearchCriteria : System.Web.UI.UserControl
{
public event EventHandler SearchButtonClicked;
public int _tcoSeqId = int.MinValue;

protected void btnSearch_Click(object sender, EventArgs e)
{
SearchButtonClicked(this, new EventArgs());
}

protected void cbCarriers_SelectedIndexChanged(object sender,
EventArgs e)
{
_tcoSeqId = Convert.ToInt32(cbCarriers.SelectedValue);
}
}

Then inside my page that uses this control I have the following:

protected void Page_Load(object sender, EventArgs e)
{
SearchCriteria1.SearchButtonClicked += new
EventHandler(btnRunSearch_Click);
}

protected void btnRunSearch_Click(object sender, EventArgs e)
{
DataManager.GetRates(SearchCriteria1._tcoSeqID);
}

Everything in this code works fine, with the exception of the
"SearchCriteria1._tcoSeqID" value I'm trying to grab inside the Click
event above. I get the following compiler error:

ASP.controls_searchcriteria_ascx' does not contain a definition for
'_tcoSeqID'.

What is the correct way to do this?

Jul 13 '07 #11
"Doogie" <dn******@dtgnet.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
I went ahead
:-)
public int _tcoSeqId = int.MinValue;
ASP.controls_searchcriteria_ascx' does not contain a definition for
'_tcoSeqID'.

What is the correct way to do this?
C# is case-sensitive...

_tcoSeqId isn't the same as tcoSeqID...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 13 '07 #12
C# is case-sensitive...
>
I know that...that's just a typo on my part for the message. It's not
recognizing that the variable exists at all.

I tried something else...I created a method over in my page that uses
this control, in the same way I did for the search button and pass the
selected value over that way. Works great, with one exception, the
"selectedValue" is always the first value in the combo box for some
reason regardless of what I switch it to. I have enable AutoPostBack
set to true for this so not sure why that is happening.

Jul 13 '07 #13
"Doogie" <dn******@dtgnet.comwrote in message
news:11**********************@m3g2000hsh.googlegro ups.com...
I tried something else...I created a method over in my page that uses
this control, in the same way I did for the search button and pass the
selected value over that way. Works great, with one exception, the
"selectedValue" is always the first value in the combo box for some
reason regardless of what I switch it to. I have enable AutoPostBack
set to true for this so not sure why that is happening.
Are you binding the combo box every time the page loads even if it being
posted back...?
http://www.thescripts.com/forum/thread474561.html
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 13 '07 #14
Mark i for example tried to do:
<script type="text/javascript">
and got :object expected - JS error
and when i did
<script type="javascript">
everything was ok!
any idea why?
thnaks
peleg
"Mark Rae [MVP]" wrote:
"Doogie" <dn******@dtgnet.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
If I run this code below in ASP.NET 2.0 it fails with an "Object
Required" error and I have no idea why.

What happens if you change

<select id="cbCarriers" onchange="javascript: test();">

to

<select id="cbCarriers" onchange="test();">

and change

<script type="javascript">

to

<script type="text/javascript">
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 14 '07 #15
"pelegk1" <pe*****@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
Mark i for example tried to do:
<script type="text/javascript">
and got :object expected - JS error
and when i did
<script type="javascript">
everything was ok!
any idea why?
Did you forget to specify your DOCTYPE?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Or are you maybe using a *really* old browser...?

In XHTML, the only valid values of the type property of the <scripttag are
"text/ecmascript", "text/javascript", "application/ecmascript",
"application/javascript" & "text/vbscript"
http://www.w3schools.com/tags/tag_script.asp
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 15 '07 #16
i am using IE6.02900 XP_SP2

"Mark Rae [MVP]" wrote:
"pelegk1" <pe*****@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
Mark i for example tried to do:
<script type="text/javascript">
and got :object expected - JS error
and when i did
<script type="javascript">
everything was ok!
any idea why?

Did you forget to specify your DOCTYPE?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Or are you maybe using a *really* old browser...?

In XHTML, the only valid values of the type property of the <scripttag are
"text/ecmascript", "text/javascript", "application/ecmascript",
"application/javascript" & "text/vbscript"
http://www.w3schools.com/tags/tag_script.asp
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 15 '07 #17
"pelegk1" <pe*****@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...

Please don't top-post...
i am using IE6.02900 XP_SP2
OK - please provide your full page markup where this occurs...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 15 '07 #18


"Mark Rae [MVP]" wrote:
"pelegk1" <pe*****@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...

Please don't top-post...
i am using IE6.02900 XP_SP2

OK - please provide your full page markup where this occurs...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

what do u nmean by " page markup "
Jul 15 '07 #19
"pelegk1" <pe*****@discussions.microsoft.comwrote in message
news:8E**********************************@microsof t.com...
what do u nmean by " page markup "
Your aspx page...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 15 '07 #20
Are you binding the combo box every time the page loads even if it being
posted back...?http://www.thescripts.com/forum/thread474561.html
That was my problem, kind of. My code to fill the dropdown list is in
my page load of my user control and was happening regardless of it was
a post back or not so was going to the db and getting the data to fill
the dropdown everytime. Once I modified that to only occur when not
doing a post back, everything is working fine.

Jul 16 '07 #21


rwerwerwererwere

*** Sent via Developersdex http://www.developersdex.com ***
Dec 20 '07 #22
hi!

<select id="cbCarriers" onchange="return test();">
<option value='241'>3gp</option>
<option value='441'>aac</option>
<option value='1115'>ab1</option>
</select>

<script type="text/javascript" language="javascript">
function test()
{
alert("this");
}
</script>

Note: i have made slight changes in script tag
try with this code

*** Sent via Developersdex http://www.developersdex.com ***
Dec 22 '07 #23

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

Similar topics

1
by: Mad Scientist Jr | last post by:
I don't know how this is happening, but a dropdown control I have is resetting to the 2nd value on the list anytime a postback occurs. I have no initiation code outside of If Not (IsPostBack) ...
1
by: Neil | last post by:
Hi, I have an aspx page page with some existing controls code etc. This page has an update button which updates a SQL Db based on any user changes. I need to add a new section to this page and a...
13
by: monomaniac21 | last post by:
hi i want to be able to trigger a javascript style popup alert in php (i want a message displayed on the actual page) is this possible?
0
by: Kay | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
0
by: Kay O'Keeffe | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
7
by: =?Utf-8?B?Qw==?= | last post by:
I have a dropdown list as below. I add an onchnage attribute in my codebehind to call some Javascript. I want to get the selected text from my dropdown. What am I doing wrong below? ...
9
by: unlikeablePorpoise | last post by:
I would like to have an HTML dropdown list where each selection calls a method. The following code doesn't work, but it I hope it gives the idea of what I'm trying to do: <FORM NAME="frm">...
3
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I need to show a custom control in the DropDown of a Windows.Forms.ToolStripMenuItem (e.g., similar to the Font Color menu item in Word except that the control is specific to my application). I...
5
by: =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ== | last post by:
I have a VS 2008 ASP.NET webform that has a reportview tag on it, accessing an .RLDC report in local report. The columns for the report are essentially: Month Item #1 Item#2 Item#3 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.