asp.net – 如何从JavaScript访问ASPxTextBox的值

假设我有一个DevExpress ASPxTextBox,其id为“instrument”.我想访问客户端文本框的值.所以我需要写一个 javascript.

如果它是一个普通的asp文本框,我可以通过编写像var instrumentElement = document.getElementById(‘<%= instrument.ClientID%>‘)这样的代码来访问文本框但是同样的方法对DevExpress来说不起作用文本框.

如何访问ASPxTextBox?我使用的是Developer Express版本7.2.

这是一些更全面的代码片段 –

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

这就是ASP代码.相应的Javascript如下:

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

解决方法

您可以在AspxTextBox上设置ClientInstanceName属性.

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

客户端:

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

}

devexpress文档在客户端脚本上有一些非常好的信息.转到此link,单击Reference,然后单击菜单上的DevExpress.Web.ASPxEditors.Scripts.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....