javascript 如何通过客户端上的 JSProperties 访问 DevExpress ASPx 控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10605754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access a DevExpress ASPx control via JSProperties on the client
提问by Soenhay
Lets say that I have several DevExpress controls and one of them is a button. On that button I want to add the ClientInstanceNames of each of the other controls so that I can access them in the buttons client side click event..
假设我有几个 DevExpress 控件,其中一个是按钮。在那个按钮上,我想添加每个其他控件的 ClientInstanceNames,以便我可以在按钮客户端单击事件中访问它们。
c#:
C#:
String strID = "MyButton";
ASPxButton btn = new ASPxButton() { ClientInstanceName = strID , Text = "Click Here", Width = new Unit("100%"), AutoPostBack = false, CssFilePath = strCssFilePath, CssPostfix = strCssPostFix };
btn.ClientSideEvents.Click = "btnClick";
btn.JSProperties.Add("cp_MyTxtBx", strID );
I want to do something similar to this...
我想做类似的事情......
js:
js:
<script type="text/javascript">
function btnClick(s, e) {
var theTxtBx = document.getElementById(s.cp_MyTxtBx);
theTxtBx.SetText('some text');
}
</script>
But that doesn't work. I know that I could do it like this:
但这不起作用。我知道我可以这样做:
<script type="text/javascript">
function btnClick(s, e) {
MyTxtBx.SetText('some text');
}
</script>
But these controls are dynamically created and I will not know their ClientInstanceNames until run time.
但是这些控件是动态创建的,直到运行时我才会知道它们的 ClientInstanceNames。
So, how can I get the control based on the String JSProperty of its ClientInstanceName?
那么,如何根据其 ClientInstanceName 的 String JSProperty 获取控件?
Thanks in advance.
提前致谢。
Related posts but not quite what I need:
相关帖子但不完全是我需要的:
How to access the value of an ASPxTextBox from JavaScript
如何从 JavaScript 访问 ASPxTextBox 的值
DevExpress: How do get an instance of a control client-side and access its client-side members?
回答by Filip
If I understood you correctly this is what you need:
如果我理解正确,这就是你所需要的:
var theTxtBx = window[s.cp_MyTxtBx];
Every devex control with ClientInstanceName set is registered as global variable.
每个设置了 ClientInstanceName 的 devex 控件都注册为全局变量。
回答by Sam
You could hack something...
你可以破解一些东西......
Basically, as you dynamically create the textboxes give them a unique client instance name.
基本上,当您动态创建文本框时,会给它们一个唯一的客户端实例名称。
At then end of your page load, you can emit some javascript that declares and array and sets the elements to equal the textbox objects.
在页面加载结束时,您可以发出一些 javascript 声明和数组并将元素设置为等于文本框对象。
var ServerSideList = new List<string>();
while(CreatingTextBoxes)
{
...
ServerSideList.Add(uniqueTextboxName);
}
....
var sb = new System.Text.StringBuilder();
sb.AppendLine("var clientSideList = [];");
foreach(var s in ServerSideList)
{
sb.Append("clientSideList.push(");
sb.Append(s);
sb.AppendLine(");");
}
AspLiteralObject.Text = sb.ToString();
In the client side button click event, you could then iterate over the clientSideList array.
在客户端按钮单击事件中,您可以遍历 clientSideList 数组。
<script type="text/javascript">
function btnClick(s, e) {
var i;
var theTxtBx;
for(i = 0; i < clientSideList.length; i++)
{
theTxtBx = clientSideList[i];
theTxtBx.SetText('some text');
}
}
</script>