You should do it on client side (edit: as suggested by MightyLampshade you can find on of 1000 examples here), you do not need a round-trip to server for that. If you have, let's say, a clear button:
$("#clear").click(function() {
$(".can-be-cleared").val("");
});
Please note that this will clear all elements with class can-be-cleared
(I assume you may do not want to clear each input but a specific set, if it's not the case just replace it with input[type=text]
).when you click an element with id clear
.
If each "clear" button has to be tied with a single specific text box then you have to repeat them (because when you click on the button textbox won't be focused any more). As alternative you may remember last focused text box. Let's see both:
<input type="text" id="textbox1"/>
<button class="clear-button" data-textbox="textbox1">clear</button>
JavaScript for this is:
$(".clear-button").click(function() {
$("#"+$(this).data("textbox")).val("");
});
Simpler alternative (I prefer this if you do not have any other special requirement) may be to keep track of last focused textbox:
var lastFocused = undefined;
$("input[type=text]").focus(function () {
lastFocused = $(this);
});
$("#clear-field").click(function () {
if (lastFocused !== undefined) {
lastFocused.val("");
}
});
Of course do not forget to match $("#clear-field")
with right ID you used for your clear button, in this case I assume:
<button id="clear-field">Clear</button>
Anyway if you really need some server side processing (for any other reason), TextBox
that generated event is in sender
parameter:
Dim textbox As TextBox = DirectCast(sender, TextBox)
textbox.Text = String.Empty