Ejemplo de procesamiento de datos de devolución
En este ejemplo se muestra un cuadro de texto personalizado que tiene una propiedad de texto que cambia tras la devolución. Una vez cargados los datos de devolución, el control provoca un evento TextChanged
. Para generar el ejemplo, vea las instrucciones en Ejemplos de control de servidor.
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;
namespace CustomControls{
public class MyTextBox: Control, IPostBackDataHandler {
public String Text {
get {
return (String) ViewState["Text"];
}
set {
ViewState["Text"] = value;
}
}
public event EventHandler TextChanged;
public virtual bool LoadPostData(string postDataKey,
NameValueCollection values) {
String presentValue = Text;
String postedValue = values[postDataKey];
if (!presentValue.Equals(postedValue)){
Text = postedValue;
return true;
}
return false;
}
public virtual void RaisePostDataChangedEvent() {
OnTextChanged(EventArgs.Empty);
}
protected virtual void OnTextChanged(EventArgs e){
if (TextChanged != null)
TextChanged(this,e);
}
protected override void Render(HtmlTextWriter output) {
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
}
}
}
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Collections.Specialized
Namespace CustomControls
Public Class MyTextBox
Inherits Control
Implements IPostBackDataHandler
Public Property Text() As String
Get
Return CType(ViewState("Text"), String)
End Get
Set
ViewState("Text") = value
End Set
End Property
Public Event TextChanged As EventHandler
Public Overridable Function LoadPostData(postDataKey As String, values As NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
Dim presentValue As String = Text
Dim postedValue As String = values(postDataKey)
If Not presentValue.Equals(postedValue) Then
Text = postedValue
Return True
End If
Return False
End Function
Public Overridable Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
OnTextChanged(EventArgs.Empty)
End Sub
Protected Overridable Sub OnTextChanged(e As EventArgs)
RaiseEvent TextChanged(Me, e)
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
output.AddAttribute(HtmlTextWriterAttribute.Type, "text")
output.AddAttribute(HtmlTextWriterAttribute.Value, me.Text)
output.AddAttribute(HtmlTextWriterAttribute.Name, me.UniqueID)
output.RenderBeginTag(HtmlTextWriterTag.Input)
output.RenderEndTag()
End Sub
End Class
End Namespace
Utilizar el control en una página
En la siguiente página ASP.NET se utiliza el control personalizado MyTextBox
que se definió en el ejemplo anterior.
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="C#" runat=server>
private StringBuilder message = new StringBuilder("");
private void Text_Changed(Object sender,EventArgs e){
message.Append("The text in" + sender.ToString()+ " was changed.");
message.Append("<br>You entered " + Server.HtmlEncode(Box.Text) +".");
}
protected override void Render(HtmlTextWriter output) {
base.Render(output);
output.Write(message.ToString());
}
</script>
<body>
<form method="POST" action="MyTextBox.aspx" runat=server>
The text box below is an instance of a custom text box.<br>
It raises a text changed event when the text in the text box changes.<br><br>
Enter your name: <Custom:MyTextBox Text=" " OnTextChanged = "Text_Changed" id = "Box" runat=server/>
<br><br>
<asp:Button Text = "Submit" runat = server/>
</form>
</body>
</html>
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="VB" runat=server>
Private message As New StringBuilder("")
Private Sub Text_Changed(sender As Object, e As EventArgs)
message.Append(("The text in" & sender.ToString() & " was changed."))
message.Append(("<br>You entered " & Server.HtmlEncode(Box.Text) & "."))
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
MyBase.Render(output)
output.Write(message.ToString())
End Sub
</script>
<body>
<form method="POST" action="MyTextBox.aspx" runat=server>
The text box below is an instance of a custom text box.<br>
It raises a text changed event when the text in the text box changes.<br><br>
Enter your name: <Custom:MyTextBox Text=" " OnTextChanged = "Text_Changed" id = "Box" runat=server/>
<br><br>
<asp:Button Text = "Submit" runat = server/>
</form>
</body>
</html>