admin管理员组

文章数量:1317906

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..

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:

<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.

So, how can I get the control based on the String JSProperty of its ClientInstanceName?

Thanks in advance.

Related posts but not quite what I need:

How to access the value of an ASPxTextBox from JavaScript

DevExpress: How do get an instance of a control client-side and access its client-side members?

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..

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:

<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.

So, how can I get the control based on the String JSProperty of its ClientInstanceName?

Thanks in advance.

Related posts but not quite what I need:

How to access the value of an ASPxTextBox from JavaScript

DevExpress: How do get an instance of a control client-side and access its client-side members?

Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked May 15, 2012 at 17:19 SoenhaySoenhay 4,0586 gold badges38 silver badges61 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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.

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.

<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>

本文标签: javascriptHow to access a DevExpress ASPx control via JSProperties on the clientStack Overflow