How to access the value of an ASPxTextBox from JavaScript

一笑奈何 提交于 2019-12-10 14:08:14

问题


Suppose I have an DevExpress ASPxTextBox whose id is "instrument". I want to access the value of the text box at client side. So I need to write a javascript.

If it was a normal asp text box, I could have accessed the text box by writing code like

var instrumentElement = document.getElementById('<%=instrument.ClientID%>')

But the same approach is not working for the DevExpress's Text Box.

How can I access an ASPxTextBox ? I am using Developer Express Version 7.2.

Here is some more thorough code snippet -

<div style="display: inline; float: left;">
    <dxe:ASPxTextBox ID="InstrumentQuantity" runat="server" Width="170px">
    </dxe:ASPxTextBox>
</div>

<div style="display: inline; float: left;" onclick="incOrDecQty(0);">
    <asp:ImageButton ID="decrementQuantity" runat="server" 
            Height="16px" Width="16px" ImageUrl="~/images/left.png" 
            AlternateText="Decrease Quantity" PostBackUrl="javascript:void(0);"/>
</div>

<div onclick="incOrDecQty(1);">
    <asp:ImageButton ID="incrementQuantity" runat="server" 
            AlternateText="Increase Quantity" ImageUrl="~/images/right.png" 
            Height="16px" Width="16px" PostBackUrl="javascript:void(0);" />
</div>

That was the ASP Code. The corresponding Javascript is as follows :

function incOrDecQty()
{
    var element = document.getElementById('<%=InstrumentQuantity.ClientID%>');
    var lotSize = parseInt(document.getElementById('<%=LotSize.ClientID%>')
        .innerHTML, 10);
    var currentValue = parseInt(element.value,10);

    if(arguments[0] == 1)
        currentValue += lotSize;
    else if((currentValue - lotSize) >= 0 )
        currentValue -= lotSize;

    element.value= currentValue;            
}

回答1:


You can set the ClientInstanceName property on the AspxTextBox.

<dxe:ASPxTextBox ID="InstrumentQuantity" 
 runat="server" Width="170px" 
 ClientInstanceName="MyTextBox"> 
</dxe:ASPxTextBox> 

ClientSide:

function DoSomething()
{
    var theText = MyTextBox.GetValue(); //GetValue() is the DevExpress clientside function

    MyTextBox.SetValue('this is the value i want to use'); //Sets the text

}

The devexpress documentation has some pretty good info on their client side scripts. Go to this link, click on Reference, then click on DevExpress.Web.ASPxEditors.Scripts on the menu.




回答2:


Here are all the Client Side methods and events on the ASPxTextBox.

http://www.devexpress.com/Help/?document=ASPxEditors/DevExpressWebASPxEditorsScriptsASPxClientTextBox_ctortopic.htm




回答3:


It's possible that the third party textbox does not make use of the ClientID. You might try UniqueID, though this may not be a globally acceptable fix (might not work in all browsers).



来源:https://stackoverflow.com/questions/2200808/how-to-access-the-value-of-an-aspxtextbox-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!