.net与母版页如何使用变量在.aspx页面上使用javascript查找控件 [英] .net with master page how to use a variable to find a control on an .aspx page using javascript
本文介绍了.net与母版页如何使用变量在.aspx页面上使用javascript查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello...
I am trying to use javascript document.getElementById() to find a server control on my page.
The page uses a master page.
The challenge is the control has a variable name.
In this case, these are textboxes with IDs like customer_123, customer_654
The javascript function is passed a variable for each control like thisid
I can always use something like document.getElementByID('ctl00_maincontent_customer' + thisid)).value
But, I do not like using that hard-codes syntax and prefer to use
document.getElementByID('<%=thiscontrolid.ClientID%>').value
I cannot find a way to create the [thiscontrolid] as a variable whose value is partially passed to the function.
For example. Something like:
function getMyControl(thisid)
{
document.getElementByID("<%=customer_[thisid].ClientID%>").value
}
But I have not been able to find a syntax that will allow me to do this.
Any suggestions would be appreciated.
What I have tried:
<pre>document.getElementByID("<%=customer_" & thisid.ClientID%>").value
推荐答案
你可以尝试jquery,这很容易。< br $>
webform1代码:
Hi,
you can try jquery and it is very easy.
webform1 code:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"><br />
<div runat="server" id="customer_1"><br />
I am first control</div><br />
<div runat="server" id="customer_2"><br />
I am second control</div> <br />
</asp:Content>
在MasterPage上使用以下脚本:
On MasterPage use below script:
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
//需要提供jquery路径
//need to give jquery path
<script type="text/javascript">
( document )。ready( function (){
getMyControl( 1 跨度>);
getMyControl( 2 );
});
function getMyControl(thisid){
var createnew = < span class =code-string> [id
(document).ready(function() { getMyControl(1); getMyControl(2); }); function getMyControl(thisid) { var createnew = "[id
= customer _ + thisid + ];
var controlhtml =
=customer_" + thisid + "]"; var controlhtml =
这篇关于.net与母版页如何使用变量在.aspx页面上使用javascript查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文