Soenhay
Soenhay

Reputation: 4048

How to access a DevExpress ASPx control via JSProperties on the client

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?

Upvotes: 3

Views: 13260

Answers (2)

Filip
Filip

Reputation: 3347

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.

Upvotes: 5

Sam
Sam

Reputation: 2201

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>

Upvotes: -1

Related Questions