如何在asp.net中获取自定义文本框控件的javascript'value'属性

时间:2011-04-07 08:17:36

标签: asp.net sharepoint custom-controls

我创建了一个自定义控件,其中包含TextBox作为子控件(并且还包含一些其他控件)。在此之后,我将此控件放在一个页面中,我试图通过javascript获取TextBox值(即文本)。但我无法直接获得“价值”财产。

自定义控制代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web.UI.WebControls; 

namespace AjaxServerControlDevelopment 
{ 
    public class MyTextBox: CompositeControl 
    { 
        /// <summary> 
        /// TextBox reference variable 
        /// </summary> 
        public TextBox objTextBox; 


        /// <summary> 
        /// The TextChanged event 
        /// </summary> 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly")] 
        public event EventHandler TextChanged; 

        /// <summary> 
        /// The ButtonClick event 
        /// </summary> 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly")] 
        public event EventHandler<EventArgs> ButtonClick; 


            #region Class constructors 


    /// <summary> 
    /// Default constructor 
    /// </summary> 
    public MyTextBox() 
    { 
        ////Instantiates the wrapped control 

        objTextBox = new TextBox(); 

        objTextBox.TextChanged += new EventHandler(objTextBox_TextChanged); 


    } 
        #endregion 

    void objTextBox_TextChanged(object sender, EventArgs e) 
    { 
        if (TextChanged != null) 
        { 
            TextChanged(this, e); 
        } 
    } 


    #region Property:Text 
    /// <summary> 
    ///       Gets or sets the Text of TngTextBox 
    /// Returns: 
    ///     A System.String Object. The default value is System.Null 
    /// </summary> 
    public string Text 
    { 

        get 
        { 

                return objTextBox.Text; 

        } 

        set 
        { 

                objTextBox.Text = value; 

        } 


    } 
    #endregion 

      #region ChildControls 
    /// <summary> 
    /// Allows us to attach child controls to our composite control 
    /// </summary> 
    protected override void CreateChildControls() 
    { 
        Controls.Clear(); 
        this.Controls.Add(objTextBox); 
        base.CreateChildControls(); 
    } 
      #endregion 
}

ControlDemo页码:

<head runat="server"> 
<title></title> 
<script type="text/javascript"> 
    function met1() { 
        var objTextBox = document.getElementById('<%=MyTextBox1.ClientID %>'); 
        //alert(objTextBox.children[0].value); 
        alert(objTextBox.value); 
    } 
</script> 

       

    <cc1:MyTextBox ID="MyTextBox1" runat="server" /> 
    <br /> 

    <asp:Button ID="Button1" OnClientClick="met1();" runat="server" Text="Button" /> 

</form> 

在DemoPage的ViewSource中,此控件在Span Element

内呈现为input Element

所以如果我像这样写

,我会得到TextBox值
document.getElementById('<%=MyTextBox1.ClientID%>').children[0].value

如何公开'value'属性,如下所示:document.getElementById('&lt;%= MyTextBox1.CleintID%&gt;')。value?

1 个答案:

答案 0 :(得分:1)

您正在尝试获取的值MyTextBox1实际上不是文本框,因此它没有值。

该怎么做:

版本1 - 将您的脚本更改为:

<script type="text/javascript"> 
    function met1() { 
        alert('<%=MyTextBox1.Text %>'); 
    } 
</script>

这足以在javascript中创建文本。

版本2 - 向TextBox类中添加一个属性,如下所示:

public TextBox ObjTextBox
{
    get { return objTextBox; }
}

然后像这样更改你的脚本:

<script type="text/javascript"> 
    function met1() { 
        var objTextBox = document.getElementById('<%=MyTextBox1.ObjTextBox.ClientID %>'); 
        alert(objTextBox.value); 
    } 
</script>

这也应该有用。

我希望这有帮助!