Я создал один пользовательский элемент управления, который содержит TextBox в качестве дочернего элемента управления (а также содержит некоторые другие элементы управления).После этого я поместил этот элемент управления на страницу, и я пытаюсь получить значение TextBox (т.е. текст) через JavaScript.Но я не могу получить свойство 'value' напрямую.
Код пользовательского элемента управления:
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>
В ViewSource на DemoPage этот элемент управления отображается как Span Element
внутри input Element
Итак, я 'получаю значение TextBox, если я напишу так