473,396 Members | 1,996 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,396 software developers and data experts.

Client Script for Server control?

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
Dec 20 '05 #1
5 1958
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

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl...
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

Dec 20 '05 #2
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();">
Mike Labosh wrote:
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();">

Dec 20 '05 #3
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
Dec 20 '05 #4
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)

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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

Dec 20 '05 #5
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.
Mike Labosh wrote:
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>


--
Atul Parmar

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...p-net/200512/1
Dec 21 '05 #6

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

Similar topics

1
by: Cathryn Lindner | last post by:
I have a web form that has a checkbox on it and a textbox associated with it. I want the textbox to be displayed when the checkbox is checked and I want this to happen on the client-side instead of...
1
by: Jim Hammond | last post by:
I can get data from a client-side assembly to the server in two manual steps, but I need to be able to do it in one step. Step 1: The user presses the manually coded "Step 1" button, which calls...
3
by: Earl Teigrob | last post by:
I wanted my "Terms and Conditions" Checkbox control to participate in my ASP.NET validation just like all the the other controls on the page. After some time of searching the web for an example of...
8
by: Mike Fellows | last post by:
Ok, im not sure if this is at all possible and if it is how i go about it is beyond me i have a piece of client side code that requires a piece of data from the server side (an ID number in this...
3
by: SimonZ | last post by:
In ASP.NET 1.0 all client ID's of controls run at server were the same as the rendered ones on the client. In ASP.NET 2.0 the client id of server control is different on server than on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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,...

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.