Client Script for Server control?

M

Mike Labosh

I have a WebForm that has, among other things, the following:

ASP:TEXTBOX txtCorrectedName
ASP:LISTBOX lstCorrectedNames

The list box gets loaded with all the "correct" names of cities, and the
textbox is blank. The idea is that the user can pull up a record with bogus
address info, and correct the name of the city by either typing a new name
in the textbox, or choosing one from the list.

When the user clicks the list box, I have a click event that says:

private void lstCorrectedNames_SelectedIndexChanged(object sender,
System.EventArgs e) {
txtCorrectedName.Text = lstCorrectedNames.SelectedItem.Text;
}

But I think it's silly to make a round-trip to do that. So I want to do it
with client script. Client JavaScript is easy for this, but I'm not sure
how to get to it since both are server controls. I can add the client
<script...> block, but I would still have to change the HTML generated by
the server-side framework so that the controls register their client-side
events:

<select id="lstCorrectedNames" ... onclick="return thisScriptFunction();">

--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane
 
E

Eliyahu Goldin

Mike,

You don't need to change any html. In your browser server controls become
client ones with the same id. < ASP:LISTBOX id = lstCorrectedNames will
become <select id="lstCorrectedNames" automatically. You just need to setup
a client-side event handler:
<ASP:LISTBOX id=lstCorrectedNames ... onclick="return
thisScriptFunction();"...

Eliyahu
 
A

Atul

Hi,

to do it at clientside you can
function selectcity()
{
ob1=document.getElementById('lstCorrectedNames');
ob2=document.getElementById('txtCorrectedName');
ob2.value=ob1.value;
return false;
}

and

<select id="lstCorrectedNames" ... onclick="return selectcity();">
 
M

Mike Labosh

Still not working. Here is the relevant code:

<html>
<head>
<script language=javascript>
<!--
function correctName() {
lst = document.getElementById("lstCorrectedNames");
txt = document.getElementById("txtCorrectedName");
txt.value = lst.value;
return false;
}
-->
</script>
</head>
<body>
<form runat=server... >

<ASP:LISTBOX id="lstCorrectedNames" runat="server"
onselectedindexchanged="return correctName();">
<!--
THIS TAG CAUSES COMPILE ERROR:
"Identifier expected, 'return' is a keyword"

If I remove the return keyword, it complains about the ')'
-->
</ASP:LISTBOX>

<ASP:TEXTBOX id="txtCorrectedName" runat="server">
</ASP:TEXTBOX>
</form>
</body>
</html>


--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane
 
B

Bruce Barker

ASP:LISTBOX controls(at least in version 1), does not have clientside event
support. the onclick, and onselectedindexchanged are server side events. to
add client side events you do it in the code behind with
Attributes.Add("onclick","dosomething();").

note: many asp.net server controls render a span around their content, and
the client events are added to the span.

-- bruce (sqlwork.com)
 
A

Atul Parmar via DotNetMonster.com

Hi, this is another way without modifying your codebehind file.
<ASP:LISTBOX id="lstCorrectedNames" runat="server" onclick="return
correctName();">

I know this is not supported but it will work fine.
 

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

Top