Calling a method from a dropdown control

D

Doogie

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>
 
M

Mark Rae [MVP]

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">
 
T

tomisarobot

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.
 
T

tomisarobot

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.
 
D

Doogie

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?
 
D

Doogie

Easiest way is to use an <asp:DropDownList> control and then wire up the
OnSelectedIndexChanged event:http://msdn2.microsoft.com/en-us/library/system.web.ui.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.
 
D

Doogie


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.
 
M

Mark Rae [MVP]

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
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...
 
D

Doogie

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?
 
M

Mark Rae [MVP]

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...
 
D

Doogie

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.
 
M

Mark Rae [MVP]

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
 
G

Guest

Mark i for example tried to do:
<script type="text/javascript">
and got :eek:bject expected - JS error
and when i did
<script type="javascript">
everything was ok!
any idea why?


thnaks
peleg
 
M

Mark Rae [MVP]

Mark i for example tried to do:
<script type="text/javascript">
and got :eek:bject 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 <script> tag are
"text/ecmascript", "text/javascript", "application/ecmascript",
"application/javascript" & "text/vbscript"
http://www.w3schools.com/tags/tag_script.asp
 
G

Guest

i am using IE6.02900 XP_SP2

Mark Rae said:
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 <script> tag are
"text/ecmascript", "text/javascript", "application/ecmascript",
"application/javascript" & "text/vbscript"
http://www.w3schools.com/tags/tag_script.asp
 

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
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top