473,748 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Client-Side JavaScript Problem in runat=server "<SELECT>"

I have some client-side JavaScript that I want to run whenever a pulldown
<SELECT> is changes on th client. I'm trying to do this as follows...

<select id="MyPulldown " onchange="handl eProblemChange( );" runat="server">

If I do this, I get client-side javascript errors because the id
"MyPulldown " doesn't exist. It's been mangled into this crazy-named thing
that I can't use.

How do I fix this so that the ID that I give it ends up being the same on
both the server and client side?

Alex
Apr 26 '06 #1
7 6284
I should add that this code is in a page that's part of a MasterPage. Is that
the reason that the client-side ID is all mangled? How can I fix this?

"Alex Maghen" wrote:
I have some client-side JavaScript that I want to run whenever a pulldown
<SELECT> is changes on th client. I'm trying to do this as follows...

<select id="MyPulldown " onchange="handl eProblemChange( );" runat="server">

If I do this, I get client-side javascript errors because the id
"MyPulldown " doesn't exist. It's been mangled into this crazy-named thing
that I can't use.

How do I fix this so that the ID that I give it ends up being the same on
both the server and client side?

Alex

Apr 26 '06 #2
http://msdn2.microsoft.com/en-US/lib...2a(VS.80).aspx

You can use the UniqueId property of a server side control to get the
generated name ... you can then write out the required javascript using this
name from the code behind.

Cheers.

Greg Young
"Alex Maghen" <Al********@new sgroup.nospam> wrote in message
news:52******** *************** ***********@mic rosoft.com...
I should add that this code is in a page that's part of a MasterPage. Is
that
the reason that the client-side ID is all mangled? How can I fix this?

"Alex Maghen" wrote:
I have some client-side JavaScript that I want to run whenever a pulldown
<SELECT> is changes on th client. I'm trying to do this as follows...

<select id="MyPulldown " onchange="handl eProblemChange( );"
runat="server">

If I do this, I get client-side javascript errors because the id
"MyPulldown " doesn't exist. It's been mangled into this crazy-named thing
that I can't use.

How do I fix this so that the ID that I give it ends up being the same on
both the server and client side?

Alex

Apr 26 '06 #3
when a control is nested in another control, the parents names is added.
this keeps the names unique. think about placing a named control in a
reapter. you can use the UniqueId to get the actual rendered id.

simpler is to pass the control to function, so you do not need the name:

<select id="MyPulldown " onchange="handl eProblemChange( this);"
runat="server">

<script.>
function handleProblemCh ange(e)
{
alert(e.id + ' ' + e.options[e.selectedIndex].value); // display
control id and value
}
</script>
-- brice (sqlwork.com)

"Alex Maghen" <Al********@new sgroup.nospam> wrote in message
news:A1******** *************** ***********@mic rosoft.com...
I have some client-side JavaScript that I want to run whenever a pulldown
<SELECT> is changes on th client. I'm trying to do this as follows...

<select id="MyPulldown " onchange="handl eProblemChange( );"
runat="server">

If I do this, I get client-side javascript errors because the id
"MyPulldown " doesn't exist. It's been mangled into this crazy-named thing
that I can't use.

How do I fix this so that the ID that I give it ends up being the same on
both the server and client side?

Alex

Apr 26 '06 #4
Hi Alex,

For client-side html element's event handler, you can pass the additional
reference to the element itself to the handler function. For example,
suppose we have the following html select list,

<select id="lst" runat="server" onchange="lst_o nchange(this);" >
......
</select>

and the script handler function is like below:

function lst_onchange(ls t)
{
// use lst to reference the select element directly
}

Thus, we even do not need to care about the actual client-side ID of the
rendered select html element.

BTW, for control whose clientside ID will be mangled, we can use its
"ClientID" server-side property to get its client-side(managled).

For example, for the above instance, we use a non-parameter handler
function, and register it dynamically in codebehind so that we can output
the correct client-side of the select list:

=======aspx==== ========
<select id="lst" runat="server" onchange="handl echange();" >
<option title="item1" value="item1">i tem1</option>
<option title="item2" value="item2">i tem2</option>
<option title="item3" value="item3">i tem3</option>
</select>
=============== =

==========code behind=======
protected void Page_Load(objec t sender, EventArgs e)
{
string script = @"
<script language='javas cript'>
function handlechange()
{
var lst = document.getEle mentById('lstid ');

alert(lst.selec tedIndex);
}

</script>
";

Page.ClientScri pt.RegisterClie ntScriptBlock(t his.GetType(),
"lst_onchan ge",
script.Replace( "lstid", lst.ClientID)
);

}
=============== =============

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
=============== =============== =============== =====

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 26 '06 #5
Wow! So you're saying that these controls are considered to be nested inside
other controls by virtue of being contained inside a MasterPage? That's a
little ugly isn't it? It's just that not being able to rely on the ID of an
object seems to defeat a lot of the magic that ASP.NET created in the first
place for having aceess to an object both on the client and the server side.

"bruce barker (sqlwork.com)" wrote:
when a control is nested in another control, the parents names is added.
this keeps the names unique. think about placing a named control in a
reapter. you can use the UniqueId to get the actual rendered id.

simpler is to pass the control to function, so you do not need the name:

<select id="MyPulldown " onchange="handl eProblemChange( this);"
runat="server">

<script.>
function handleProblemCh ange(e)
{
alert(e.id + ' ' + e.options[e.selectedIndex].value); // display
control id and value
}
</script>
-- brice (sqlwork.com)

"Alex Maghen" <Al********@new sgroup.nospam> wrote in message
news:A1******** *************** ***********@mic rosoft.com...
I have some client-side JavaScript that I want to run whenever a pulldown
<SELECT> is changes on th client. I'm trying to do this as follows...

<select id="MyPulldown " onchange="handl eProblemChange( );"
runat="server">

If I do this, I get client-side javascript errors because the id
"MyPulldown " doesn't exist. It's been mangled into this crazy-named thing
that I can't use.

How do I fix this so that the ID that I give it ends up being the same on
both the server and client side?

Alex


Apr 26 '06 #6
Hey Alex,

Have you had a chance to have a look at my last reply? As I mentioned we
can programmaticall y get any server control's client-side ID through the
"ClientID" property (after they have been correctly added into their parent
NamingContainer ). ClientID is just the one useful to our client-side
script, and for server-side, we use the "ID" property to locate a
servercontrol in its NamingContainer .

Also, as for master page in ASP.NET 2.0, it is somewhat like a Usercontrol,
and it is the concrete page(which applied the Master page) that will
include the master page as a sub control, rather than the Master Page
include the concrete page.

BTW, we can turn on the ASP.NET page's output Trace through the @Page
directive in aspx file, thus, the runtime page will display the whole
page's control Tree which is quite useful for understanding the page's
control structure.

Anyway, please feel free to post here if you have any question on this.

Regards,

Steven Cheng
Microsoft Online Community Support
=============== =============== =============== =====

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 27 '06 #7
Hi Alex,

Any further progress on this? Also, I've found your other new threads on
the script manipulating in ASP.NET 2.0 pages and have posted some other
suggestion there. Anyway, if there is anything we can help ,please feel
free to post here.

Regards,

Steven Cheng
Microsoft Online Community Support
=============== =============== =============== =====

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 1 '06 #8

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

Similar topics

3
4620
by: Drulli Rokk | last post by:
Hi, Here's a question that has cost this newbie two days of headache already: How can I get my XSLT stylesheet to specify a maximum number of elements to process? I'm now using <xsl:for-each> which processes all the child-nodes at the particuar point in my XML (RSS) file. But what I really want, is this: if number of child-nodes > n then process only the first n
1
3109
by: Rafaela K. Azinhal | last post by:
Hi, I'm a newbie and have a question: I have following PivotTable object in a HTML page <html> <body> <object class='ptdrillthrough' classid="clsid:0002E552-0000-0000-C000-000000000046" id="pt">
16
9188
by: network-admin | last post by:
We have Problems with Access query on Oracle 10g Database with ODBC Connection. The Query_1 is such as select * from xtable where ycolumn <"S" Result = ODBC Faild ----------------------------------------------------
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9312
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,...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 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...
0
4593
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...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.