| |

VerySource

 Forgot password?
 Register
Search
View: 711|Reply: 4

How to use asp.net generated control id in javascript

[Copy link]

3

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-22 23:00:01
| Show all posts |Read mode
I saw an example of linkage of a drop-down list box. Problems encountered during implementation:
The original example is to use the control directly in .aspx, the code is as follows:
<SELECT id = "province" runat = "server" NAME = "province"> </ SELECT>
The generated HTML is as follows:
<select name = "province" id = "province"> </ select>

When I implemented it, I used a web user control (.ascx). The code in the control is the same as above.
Generated HTML:
<select name = "CtrlUserCenter1: _ctl0: province" id = "CtrlUserCenter1__ctl0_province"> </ select>

The example javascript needs to use the ID of the control, a piece of code is as follows:
for (i = 0; i <s.length-1; i ++)
  document.getElementById (s [i]). onchange = new Function ("change (" + (i + 1) + ")");

But now the generated IDs are too different.
How to do?

What if I start with .net?
What if I start with javascript?
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-2-5 11:15:01
| Show all posts
var id = "<% = this.province.ClientID%>";
Reply

Use magic Report

0

Threads

73

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-2-5 18:45:01
| Show all posts
shenmue024's method is good, haha
In the user control,
<select name = "CtrlUserCenter1: _ctl0: province" id = "CtrlUserCenter1__ctl0_province"> </ select>
<script language = javascript>
var provinceId = "<% = this.province.ClientID%>";
</ script>

But this control in ASPX should be written before the code that uses the control's ID.
Reply

Use magic Report

0

Threads

10

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-8 18:45:01
| Show all posts
cs file write province.ClientID to get the client ID of the control
Write obj = document.getElementById ('<% = province.ClientID%>') in js;
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-8 22:30:01
| Show all posts
Same solution asshenmue024
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list