I'm trying to use asp:
<asp:TextBox ID="txtInput" runat="server" TextMode="MultiLine"></asp:TextBox>
I want a way to specify the maxlength property, but apparently there's no way possible for a multiline textbox. I've been trying to use some JavaScript for the onkeypress event:
onkeypress="return textboxMultilineMaxNumber(this,maxlength)"
function textboxMultilineMaxNumber(txt, maxLen) {
try {
if (txt.value.length > (maxLen - 1)) return false;
} catch (e) { }
return true;
}
While working fine the problem with this JavaScript function is that after writing characters it doesn't allow you to delete and substitute any of them, that behavior is not desired.
Have you got any idea what could I possibly change in the above code to avoid that or any other ways to get round it?
Use a regular expression validator instead. This will work on the client side using JavaScript, but also when JavaScript is disabled (as the length check will be performed on the server as well).
The following example checks that the entered value is between 0 and 100 characters long:
<asp:RegularExpressionValidator runat="server" ID="valInput"
ControlToValidate="txtInput"
ValidationExpression="^[\s\S]{0,100}$"
ErrorMessage="Please enter a maximum of 100 characters"
Display="Dynamic">*</asp:RegularExpressionValidator>
There are of course more complex regexs you can use to better suit your purposes.
try this javascript:
function checkTextAreaMaxLength(textBox,e, length)
{
var mLen = textBox["MaxLength"];
if(null==mLen)
mLen=length;
var maxLength = parseInt(mLen);
if(!checkSpecialKeys(e))
{
if(textBox.value.length > maxLength-1)
{
if(window.event)//IE
e.returnValue = false;
else//Firefox
e.preventDefault();
}
}
}
function checkSpecialKeys(e)
{
if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
return false;
else
return true;
}
On the control invoke it like this:
<asp:TextBox Rows="5" Columns="80" ID="txtCommentsForSearch" MaxLength='1999' onkeyDown="checkTextAreaMaxLength(this,event,'1999');" TextMode="multiLine" runat="server"> </asp:TextBox>
You could also just use the checkSpecialKeys function to validate the input on your javascript implementation.
keep it simple. Most modern browsers support a maxlength attribute on a text area (IE included), so simply add that attribute in code-behind. No JS, no Jquery, no inheritance, custom code, no fuss, no muss.
VB.Net:
fld_description.attributes("maxlength") = 255
C#
fld_description.Attributes["maxlength"] = 255
Roll your own:
function Count(text)
{
//asp.net textarea maxlength doesnt work; do it by hand
var maxlength = 2000; //set your value here (or add a parm and pass it in)
var object = document.getElementById(text.id) //get your object
if (object.value.length > maxlength)
{
object.focus(); //set focus to prevent jumping
object.value = text.value.substring(0, maxlength); //truncate the value
object.scrollTop = object.scrollHeight; //scroll to the end to prevent jumping
return false;
}
return true;
}
Call like this:
<asp:TextBox ID="foo" runat="server" Rows="3" TextMode="MultiLine" onKeyUp="javascript:Count(this);" onChange="javascript:Count(this);" ></asp:TextBox>
Things have changed in HTML5:
ASPX:
<asp:TextBox ID="txtBox" runat="server" maxlength="2000" TextMode="MultiLine"></asp:TextBox>
C#:
if (!IsPostBack)
{
txtBox.Attributes.Add("maxlength", txtBox.MaxLength.ToString());
}
Rendered HTML:
<textarea name="ctl00$DemoContentPlaceHolder$txtBox" id="txtBox" maxlength="2000"></textarea>
The metadata for Attributes:
Summary: Gets the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control.
Returns: A System.Web.UI.AttributeCollection of name and value pairs.
use custom attribute maxsize="100"
<asp:TextBox ID="txtAddress" runat="server" maxsize="100"
Columns="17" Rows="4" TextMode="MultiLine"></asp:TextBox>
<script>
$("textarea[maxsize]").each(function () {
$(this).attr('maxlength', $(this).attr('maxsize'));
$(this).removeAttr('maxsize');
});
</script>
this will render like this
<textarea name="ctl00$BodyContentPlac
eHolder$txtAddress" rows="4" cols="17" id="txtAddress" maxlength="100"></textarea>
Another way of fixing this for those browsers (Firefox, Chrome, Safari) that support maxlength on textareas (HTML5) without javascript is to derive a subclass of the System.Web.UI.WebControls.TextBox class and override the Render method. Then in the overridden method add the maxlength attribute before rendering as normal.
protected override void Render(HtmlTextWriter writer)
{
if (this.TextMode == TextBoxMode.MultiLine
&& this.MaxLength > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, this.MaxLength.ToString());
}
base.Render(writer);
}
$('#txtInput').attr('maxLength', 100);
Use HTML textarea with runat="server" to access it in server side.
This solution has less pain than using javascript or regex.
<textarea runat="server" id="txt1" maxlength="100" />
Note: To access Text Property in server side, you should use txt1.Value instead of txt1.Text
I tried different approaches but every one had some weak points (i.e. with cut and paste or browser compatibility). This is the solution I'm using right now:
function multilineTextBoxKeyUp(textBox, e, maxLength) {
if (!checkSpecialKeys(e)) {
var length = parseInt(maxLength);
if (textBox.value.length > length) {
textBox.value = textBox.value.substring(0, maxLength);
}
}
}
function multilineTextBoxKeyDown(textBox, e, maxLength) {
var selectedText = document.selection.createRange().text;
if (!checkSpecialKeys(e) && !e.ctrlKey && selectedText.length == 0) {
var length = parseInt(maxLength);
if (textBox.value.length > length - 1) {
if (e.preventDefault) {
e.preventDefault();
}
else {
e.returnValue = false;
}
}
}
}
function checkSpecialKeys(e) {
if (e.keyCode != 8 && e.keyCode != 9 && e.keyCode != 33 && e.keyCode != 34 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40) {
return false;
} else {
return true;
}
}
In this case, I'm calling multilineTextBoxKeyUp on key up and multilineTextBoxKeyDown on key down:
myTextBox.Attributes.Add("onkeyDown", "multilineTextBoxKeyDown(this, event, '" + maxLength + "');");
myTextBox.Attributes.Add("onkeyUp", "multilineTextBoxKeyUp(this, event, '" + maxLength + "');");
Here's how we did it (keeps all code in one place):
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"/>
<% TextBox1.Attributes["maxlength"] = "1000"; %>
Just in case someone still using webforms in 2018..
Have a look at this. The only way to solve it is by javascript as you tried.
EDIT:
Try changing the event to keypressup.
The following example in JavaScript/Jquery will do that-
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<script type="text/javascript">
function count(text, event) {
var keyCode = event.keyCode;
//THIS IS FOR CONTROL KEY
var ctrlDown = event.ctrlKey;
var maxlength = $("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val().length;
if (maxlength < 200) {
event.returnValue = true;
}
else {
if ((keyCode == 8) || (keyCode == 9) || (keyCode == 46) || (keyCode == 33) || (keyCode == 27) || (keyCode == 145) || (keyCode == 19) || (keyCode == 34) || (keyCode == 37) || (keyCode == 39) || (keyCode == 16) || (keyCode == 18) ||
(keyCode == 38) || (keyCode == 40) || (keyCode == 35) || (keyCode == 36) || (ctrlDown && keyCode == 88) || (ctrlDown && keyCode == 65) || (ctrlDown && keyCode == 67) || (ctrlDown && keyCode == 86))
{
event.returnValue = true;
}
else {
event.returnValue = false;
}
}
}
function substr(text)
{
var txtWebAdd = $("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val();
var substrWebAdd;
if (txtWebAdd.length > 200)
{
substrWebAdd = txtWebAdd.substring(0, 200);
$("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val('');
$("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val(substrWebAdd);
}
}
This snippet worked in my case. I was searching for the solution and thought to write this so that it may help any future reader.
ASP
<asp:TextBox ID="tbName" runat="server" MaxLength="250" TextMode="MultiLine" onkeyUp="return CheckMaxCount(this,event,250);"></asp:TextBox>
Java Script
function CheckMaxCount(txtBox,e, maxLength)
{
if(txtBox)
{
if(txtBox.value.length > maxLength)
{
txtBox.value = txtBox.value.substring(0, maxLength);
}
if(!checkSpecialKeys(e))
{
return ( txtBox.value.length <= maxLength)
}
}
}
function checkSpecialKeys(e)
{
if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
return false;
else
return true;
}
#Raúl Roa Answer did worked for me in case of copy/paste. while this does.
$("textarea[maxlength]").on("keydown paste", function (evt) {
if ($(this).val().length > $(this).prop("maxlength")) {
if (evt.type == "paste") {
$(this).val($(this).val().substr(0, $(this).prop("maxlength")));
} else {
if ([8, 37, 38, 39, 40, 46].indexOf(evt.keyCode) == -1) {
evt.returnValue = false;
evt.preventDefault();
}
}
}
});
you can specify the max length for the multiline textbox in pageLoad Javascript Event
function pageLoad(){
$("[id$='txtInput']").attr("maxlength","10");
}
I have set the max length property of txtInput multiline textbox to 10 characters in pageLoad() Javascript function
This is the same as #KeithK's answer, but with a few more details. First, create a new control based on TextBox.
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyProject
{
public class LimitedMultiLineTextBox : System.Web.UI.WebControls.TextBox
{
protected override void Render(HtmlTextWriter writer)
{
this.TextMode = TextBoxMode.MultiLine;
if (this.MaxLength > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, this.MaxLength.ToString());
}
base.Render(writer);
}
}
}
Note that the code above always sets the textmode to multiline.
In order to use this, you need to register it on the aspx page. This is required because you'll need to reference it using the TagPrefix, otherwise compilation will complain about custom generic controls.
<%# Register Assembly="MyProject" Namespace="MyProject" TagPrefix="mp" %>
<mp:LimitedMultiLineTextBox runat="server" Rows="3" ...
Nearly all modern browsers now support the use of the maxlength attribute for textarea elements.(https://caniuse.com/#feat=maxlength)
To include the maxlength attribute on a multiline TextBox, you can simply modify the Attributes collection in the code behind like so:
txtTextBox.Attributes["maxlength"] = "100";
If you don't want to have to use the code behind to specify that, you can just create a custom control that derives from TextBox:
public class Textarea : TextBox
{
public override TextBoxMode TextMode
{
get { return TextBoxMode.MultiLine; }
set { }
}
protected override void OnPreRender(EventArgs e)
{
if (TextMode == TextBoxMode.MultiLine && MaxLength != 0)
{
Attributes["maxlength"] = MaxLength.ToString();
}
base.OnPreRender(e);
}
}
MaxLength is now supported as of .NET 4.7.2, so as long as you upgrade your project to .NET 4.7.2 or above, it will work automatically.
You can see this in the release notes here - specifically:
Enable ASP.NET developers to specify MaxLength attribute for Multiline asp:TextBox. [449020, System.Web.dll, Bug]
This is absolutely working:
use textarea and value
<div class="form-group row">
<label class="col-md-4 col-form-label">Description</label>
<textarea id="txtDescription" class="form-control" style="height: 40px ; width :250px; " runat="server" maxlength="500" tabindex="32" ></textarea>
</div>
</div>
In code:
While saving:-
BusinessLayer.Description = txtDescription.Value.ToString();
While taking back the value :-
txtDescription.Value = BusinessLayer.Description.ToString();
Important points:
Don't forget to use runat="server"
Don't forget to use value while converting
Related
How to handle ok-cancel button in prompt in asp.net?I want to when click on cancel,I can do a new job?
this is my code:
<asp:Button ID="Button1" runat="server" OnClientClick="return a();" Text="Button" />
function a()
{
var s = prompt("hello");
if (s != 123) {
alert("enter number correctly");
return true;
}
else
return false;
}
If you want to cancel the postback you need to return false, otherwise true (so the opposite):
function a()
{
var s = prompt("hello");
if (s != 123) {
alert("enter number correctly");
return false;
}
else
return true;
}
The most elegant way would be to use jQuery UI's dialog.
You can do this task using confirm box :-
<asp:Button ID="Button1" runat="server" OnClientClick="return disp_confirm();" Text="Button" />
Use this script on button press :-
function disp_confirm()
{
var r=confirm("Press a button!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
return false;
}
If you want to do this using JavaScript than the following from here will do exactly what's required. Otherwise there are some very good and established JQuery plugins that can give you pretty much endless options.
if (confirm("Your question")) {
// do things if OK
}
I am developing an open source project for rendering HTML5 using ASP.NET. Here you can take a look:
http://asphtml5.codeplex.com/
now I have a problem with update panel in posting back the input values that have type other than 'text'. as you might know, html 5 has introduced several input types, for example 'number', 'tel', 'search', etc. Now if I render such controls, everything works fine in normal situations, but if I put them inside an UpdatePanel, no value will be posted back and the value will be reset.
here is a small piece of code that produces the same error:
<asp:UpdatePanel runat="server" ID="UP">
<ContentTemplate>
<p>
Enter A Number:
<asp:TextBox runat="server" ID="Number2" type="number" />
</p>
<asp:Button Text="Submit" runat="server" ID="BtnSubmit" OnClick="BtnSubmit_Click" />
<p>
You entered :
<asp:Label Text="" ID="LblValue" runat="server" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
if you test this code on a browser that supports html 5, lets say Chrome as an example, a Numeric Up-Down field will be shown. but if you click on the submit button, it will lose the value that you have entered.
here is the code for event handler:
protected void BtnSubmit_Click(object sender, EventArgs e)
{
LblValue.Text = Number2.Text;
}
what I have already tried is reading UpdatePanel, ScriptManager and ScriptManagerProxy classes codes, nothing found.
I think I might need to create my own UpdatePanel and/or ScriptManager classes for use.
Could anyone help me, and tell me where to check.
Interestingly enough, the ASP.NET 4.0 AJAX framework does seem to be aware of HTML 5 input types (see code), yet as #TimSchmelter pointed out, this is confirmed by Microsoft as a bug.
This may give you a starting point for debugging the behavior and/or overriding the default behavior and finding a solution.
It could also be an error on the server-side processing code for these input types, although I'm not sure why they should/would care about an async postback versus a normal postback.
this._textTypes = /^(text|password|hidden|search|tel|url|email|number|range|color|datetime|date|month|week|time|datetime-local)$/i;
function Sys$WebForms$PageRequestManager$_onFormSubmit(evt) {
var i, l, continueSubmit = true,
isCrossPost = this._isCrossPost;
this._isCrossPost = false;
if (this._onsubmit) {
continueSubmit = this._onsubmit();
}
if (continueSubmit) {
for (i = 0, l = this._onSubmitStatements.length; i < l; i++) {
if (!this._onSubmitStatements[i]()) {
continueSubmit = false;
break;
}
}
}
if (!continueSubmit) {
if (evt) {
evt.preventDefault();
}
return;
}
var form = this._form;
if (isCrossPost) {
return;
}
if (this._activeDefaultButton && !this._activeDefaultButtonClicked) {
this._onFormElementActive(this._activeDefaultButton, 0, 0);
}
if (!this._postBackSettings || !this._postBackSettings.async) {
return;
}
var formBody = new Sys.StringBuilder(),
count = form.elements.length,
panelID = this._createPanelID(null, this._postBackSettings);
formBody.append(panelID);
for (i = 0; i < count; i++) {
var element = form.elements[i];
var name = element.name;
if (typeof(name) === "undefined" || (name === null) || (name.length === 0) || (name === this._scriptManagerID)) {
continue;
}
var tagName = element.tagName.toUpperCase();
if (tagName === 'INPUT') {
var type = element.type;
if (this._textTypes.test(type)
|| ((type === 'checkbox' || type === 'radio') && element.checked)) {
formBody.append(encodeURIComponent(name));
formBody.append('=');
formBody.append(encodeURIComponent(element.value));
formBody.append('&');
}
}
else if (tagName === 'SELECT') {
var optionCount = element.options.length;
for (var j = 0; j < optionCount; j++) {
var option = element.options[j];
if (option.selected) {
formBody.append(encodeURIComponent(name));
formBody.append('=');
formBody.append(encodeURIComponent(option.value));
formBody.append('&');
}
}
}
else if (tagName === 'TEXTAREA') {
formBody.append(encodeURIComponent(name));
formBody.append('=');
formBody.append(encodeURIComponent(element.value));
formBody.append('&');
}
}
formBody.append("__ASYNCPOST=true&");
if (this._additionalInput) {
formBody.append(this._additionalInput);
this._additionalInput = null;
}
// truncated for length
All right then, I figured out a way to fix that.
First of all I found the problem with the code that Tim Medora provided. It all was the this. modifier's fault. So this JavaScript fixed the problem:
var _textTypes = /^(text|password|hidden|search|tel|url|email|number|range|color|datetime|date|month|week|time|datetime-local)$/i;
function Sys$WebForms$PageRequestManager$_onFormSubmit(evt) {
...
if (_textTypes.test(type) ||
(((type === 'checkbox') || (type === 'radio')) && element.checked)) {
formBody.append(encodeURIComponent(name));
formBody.append('=');
formBody.append(encodeURIComponent(element.value));
formBody.append('&');
...
}
Now I had to inject my function into ScriptResource.axd.
For now I found a way that seems to work:
I created a class ScriptResouceHandler that extends System.Web.Handlers.ScriptResourceHandler in namespace DotM.Html5.Handlers.
in its ProcessRequest I called base.ProcessRequest(context) to do its job. But I wanted to add my function to the one rendering original function. I found out that it was when the encrypted ZSystem.Web.Extensions,4.0.0.0,,31bf3856ad364e35|MicrosoftAjaxWebForms.debug.js| was passed.
Another problem was that in System.Web.Handlers.ScriptResourceHandler, 'Page.DecryptString' method which is internal is called to decrypt the query string param.
so there was no way for me to invoke that method via reflection.
here is the code:
protected sealed override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
if (CypherContainsAjax(context.Request.QueryString["d"]))
context.Response.Write(OnFormSubmit);
}
private bool CypherContainsAjax(string cypher)
{
var text = DecryptString(cypher);
if (text == null)
return true; //Then Add it everywhere. What else could I do? :D
return text.Contains("MicrosoftAjaxWebForms");
}
private string DecryptString(string cypher)
{
if (PageDecryptString == null)
return null;
return (string)PageDecryptString.Invoke(null, new object[] { cypher });
}
private static MethodInfo PageDecryptString;
static ScriptResourceHandler()
{
PageDecryptString = typeof(Page).GetMethod("DecryptString", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
}
You may call this some kind of ugly hacking ...
This was already fixed in .Net 4 Reliability Update 1 (there is also a version 2, but it does not contain the first one): http://support.microsoft.com/kb/2533523
But if you are using AjaxControlToolkit, it uses its own internal MicrosoftAjaxWebForms.js which is an older fork, and there is still no official fix for it - you can use mine solution from here: http://ajaxcontroltoolkit.codeplex.com/workitem/27041
So - you can either include the fixed ToolkitScriptManager with your project (a bloat, I know), or you can try to include the new version of MicrosoftAjaxWebForms.js by experimenting with vanilla ScriptManager properties AjaxFrameworkMode="Explicit", Scripts or CompositeScript.
Use the AjaxFrameworkMode property to enable all Microsoft Ajax script files, to disable all Microsoft Ajax script files, or to explicitly include individual script files.
The Scripts collection does not contain the core Microsoft Ajax Library scripts. The scripts in the core library are rendered automatically; they do not have to be registered with the ScriptManager control. However, if you want to override a core script or any control script and substitute a different version of the script, you can add your version to the Scripts collection.
<asp:ScriptManager runat="server">
<Scripts>
<asp:ScriptReference Path="~/MicrosoftAjaxWebForms.js" />
</Scripts>
</asp:ScriptManager>
You can get the new version of MicrosoftAjaxWebForms.js from System.Web.Extensions assembly Resources (with .Net Reflector) on machines with installed Reliability Update 1.
I have the following jquery function for filtering the contents of a listbox on the onkeyup event from a textbox.
function DoListBoxFilter(listBoxSelector, filter, keys, values) {
var list = $(listBoxSelector);
var selectBase = '<option value="{0}">{1}</option>';
list.empty();
for (i = 0; i < values.length; ++i) { //add elements from cache if they match filter
var value = values[i];
if (value == "" || value.toLowerCase().indexOf(filter.toLowerCase()) >= 0) {
var temp = String.format(selectBase, keys[i], value);
list.append(temp);
}
}
}
It works great for small to medium size lists, but it is a little bit slow when working with lists over 300-400 items... Can anyone help with some ideas to optimize the javascript a little bit to speed up the function?
The function is invoked with the following code:
$('#<% = txtSearch.ClientID %>').keyup(function() {
var filter = $(this).val();
DoListBoxFilter('#<% = lstPars.ClientID %>', filter, keys_<% = this.ClientID %>, values_<% = this.ClientID %>);
});
To use this, I bind an asp.net listbox and also populate two javascript arrays (key and value) on the page.
This IS storing the data in two places on the page, but using this method I am able to use the postback of the listbox to get the selected value without using javacript to extract the value and cache it in a hidden div. (it also saves having to run the function at page load on the clients browser.. and that is really the function where I am seeing the slowness, so storing in two places speeds up the page rendering)
I found that I needed to use the javascript array approach because most browsers don't acknowledge any attempts to hide an option tag... only Firefox appears to do it.
I'm not sure its possible to optimize and speed this code up any more, but if anyone has any ideas I would appreciate it.
Thanks,
Max Schilling
I am also using the same code to filter the list box but with a bit of change, in my code first I am comparing the searched value/word with the option items and if match got success then only filtering the list.
var existText = values[i].substring(0, filter.length);
if (existText.toLowerCase() == filter.toLowerCase())
Below is the full code:
function DoListBoxFilter(listBoxSelector, filter, keys, values) {
var list = $(listBoxSelector);
var selectBase = '<option value="{0}">{1}</option>';
list.empty();
for (i = 0; i < values.length; ++i) {
var existText = values[i].substring(0, filter.length);
if (existText.toLowerCase() == filter.toLowerCase()) {
var value = values[i];
if (value === "" || value.toLowerCase().indexOf(filter.toLowerCase()) >= 0) {
var temp = '<option value="' + keys[i] + '">' + value + '</option>';
list.append(temp);
}
}
}
}
var keys = [];
var values = [];
var options = $('#CountryList option');
$.each(options, function (index, item) {
keys.push(item.value);
values.push(item.innerHTML);
});
$(document).ready(function () {
$('input#CountrySearch').on('keyup', function () {
var filter = $(this).val();
DoListBoxFilter('#CountryList', filter, keys, values);
});
});
You can also see a demo here. In this demo I have used a list containing the more than 500 list items and its working fine without any performance issue.
It looks like you might be suffering in terms of performance with large lists because you are appending each item one at a time that matches the filter. I would build up an array of matches (or create a documentFragment) and then append that to the DOM in one go.
function DoListBoxFilter(listBoxSelector, filter, keys, values) {
var list = $(listBoxSelector);
var selectBase = '<option value="{0}">{1}</option>';
list.empty();
var i = values.length;
var temp = [];
var option, value;
while (i--) {
value = values[i];
if (value && value.toLowerCase().indexOf(filter.toLowerCase()) !== -1) {
option = String.format(selectBase, keys[i], value);
temp.push(option);
}
}
// we got all the options, now append to DOM
list.append(temp.join(''));
}
Following link helped me, though it is javascript.
Search ListBox items using JavaScript
<head id="Head1" runat="server">
<title>Demo</title>
</head>
<script type="text/javascript" language="javascript">
function SearchList()
{
var l = document.getElementById('<%= ListBox1.ClientID %>');
var tb = document.getElementById('<%= TextBox1.ClientID %>');
if(tb.value == ""){
ClearSelection(l);
}
else{
for (var i=0; i < l.options.length; i++){
if (l.options[i].value.toLowerCase().match(tb.value.toLowerCase()))
{
l.options[i].selected = true;
return false;
}
else
{
ClearSelection(l);
}
}
}
}
function ClearSelection(lb){
lb.selectedIndex = -1;
}
</script>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" onkeyup="return SearchList();"/><br />
<asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="250px">
<asp:ListItem>Vincent</asp:ListItem>
<asp:ListItem>Jennifer</asp:ListItem>
<asp:ListItem>Shynne</asp:ListItem>
<asp:ListItem>Christian</asp:ListItem>
<asp:ListItem>Helen</asp:ListItem>
<asp:ListItem>Vladi</asp:ListItem>
<asp:ListItem>Bee</asp:ListItem>
<asp:ListItem>Jerome</asp:ListItem>
<asp:ListItem>Vinz</asp:ListItem>
<asp:ListItem>Churchill</asp:ListItem>
<asp:ListItem>Rod</asp:ListItem>
<asp:ListItem>Mark</asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>
I have created my own code to provide date masking and validation for TextBox control in asp.net. Below is the code. The code works perfectly.
function IsValidDate(ctrlID)
{
var validDate=true;
var myT=document.getElementById("ctl00_ContentPlaceHolder1_CandidateResume1_TabContainer1_TabPanel2_Education1_"+ctrlID);
var mm=myT.value.substring(0,2);
var dd=myT.value.substring(5,3);
var yy=myT.value.substring(6);
if(mm!=0 && mm>12){
myT.value=""; validDate=false;
}
else
{
if((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
{
if(mm==2 && dd>29){
myT.value=""; validDate=false;
}
}
else
{
if(mm==2 && dd>28){
myT.value=""; validDate=false;
}
else
{
if(dd!=0 && dd>31){
myT.value=""; validDate=false;
}
else
{
if((mm==4 || mm==6 || mm==9 || mm==11) && (dd!=0 && dd>30)){
myT.value=""; validDate=false;
}
}
}
}
}
if(validDate==false)
{
myT.style.backgroundColor='#FF0000';
myT.focus;
}
else
myT.style.backgroundColor='#FFFFFF';
}
function maskDate(ctrlID)
{
var myT=document.getElementById("ctl00_ContentPlaceHolder1_CandidateResume1_TabContainer1_TabPanel2_Education1_"+ctrlID);
var KeyID = (window.event) ? window.event.keyCode : 0;
if((KeyID>=48 && KeyID<=57) || KeyID==8)
{
if(KeyID==8)
return;
if(myT.value.length==2)
{
myT.value=myT.value+"/";
}
if(myT.value.length==5)
{
myT.value=myT.value+"/";
}
}
else
{
window.event.keyCode=0;
}
}
The problem -
I am attaching these functions to the textbox as -
TextBox1.Attributes.Add("onFocusout","IsValidDate('TextBox1');");
TextBox1.Attributes.Add("onKeyPress","maskDate('TextBox1');");
If you look at the javascript code I have collected the control id in myT variable. I have also passed the id of textbox while attaching the js functions using Attributes.Add()
My problem is that i dont want to pass the id of the textbox as i am already attaching it. That is i want to write the code as
TextBox1.Attributes.Add("onFocusout","IsValidDate();");
TextBox1.Attributes.Add("onKeyPress","maskDate();");
My question is how can i get the id of textbox to which i have attached these functions witin JS code.
NOTE: I DONT WANT TO PASS CONTROL NAME OR CONTROLS CLIENTID WHILE ADDING ATTRIBUTES. PLEASE NOTE THAT I WANT TO REPLACE
TextBox1.Attributes.Add("onFocusout","IsValidDate('TextBox1');");
WITH
TextBox1.Attributes.Add("onFocusout","IsValidDate();");
I WANT TO ATTACH THESE FUNCTIONS WITH MULTIPLE TEXTBOXES.
AS I AM USING .Attributes.Add(...) I WANT TO GET THE SAME CONTROLS CLIENTID WITHIN JS CODE.
Your help is highly appreciated.
Thanks and Regards
Mohammad Irfan
var txtControl = document.getElementById("<%= txtControl.ClientID %>");
Control.ClientID
Either pass TextBox1.ClientID to the function, or change the function call to be IsValidDate(this.id). But as you don't want to pass these in, you can place the TextBox1.ClientID in your javascript or use jquery to find it using $('[id*=TextBox1]').
The .Net generated code for a form with the "DefaultButton" attribute set contains poor javascript that allows the functionality to work in IE but not in other browsers (Firefox specifcially).
Hitting enter key does submit the form with all browsers but Firefox cannot disregard the key press when it happens inside of a <textarea> control. The result is a multiline text area control that cannot be multiline in Firefox as the enter key submits the form instead of creating a new line.
For more information on the bug, read it here.
This could be fixed in Asp.Net 3.0+ but a workaround still has to be created for 2.0.
Any ideas for the lightest workaround (a hack that doesn't look like a hack =D)? The solution in the above link scares me a little as it could easily have unintended side-effects.
I use this function adapted from codesta. [Edit: the very same one, I see, that scares you! Oops. Can't help you then.]
http://blog.codesta.com/codesta_weblog/2007/12/net-gotchas---p.html.
You use it by surrounding your code with a div like so. You could subclass the Form to include this automatically. I don't use it that much, so I didn't.
<div onkeypress="return FireDefaultButton(event, '<%= aspButtonID.ClientID %>')">
(your form goes here)
</div>
Here's the function.
function FireDefaultButton(event, target)
{
// srcElement is for IE
var element = event.target || event.srcElement;
if (13 == event.keyCode && !(element && "textarea" == element.tagName.toLowerCase()))
{
var defaultButton;
defaultButton = document.getElementById(target);
if (defaultButton && "undefined" != typeof defaultButton.click)
{
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation)
event.stopPropagation();
return false;
}
}
return true;
}
It seems that the fix codesta.com that harpo link to is no longer necessary, since the fix event.srcElement is not integrade in ASP.NET 3.5. The implementation of DefaultButton does however still have some problems, because it is catching the Enter key press in too many cases. For example: If you have activated a button in the form using tab, pressing Enter should click on the button and not submit the form.
Include the following JavaScript code at the bottom of your ASP.NET web page to make Enter behave the way it should.
// Fixes ASP.NET's behavior of default button by testing for more controls
// than just textarea where the event should not be caugt by the DefaultButton
// action. This method has to override ASP.NET's WebForm_FireDefaultButton, so
// it has to included at the bottom of the page.
function WebForm_FireDefaultButton(event, target) {
if (event.keyCode == 13) {
var src = event.srcElement || event.target;
if (!(
src
&&
(
src.tagName.toLowerCase() == "textarea"
|| src.tagName.toLowerCase() == "a"
||
(
src.tagName.toLowerCase() == "input"
&&
(
src.getAttribute("type").toLowerCase() == "submit"
|| src.getAttribute("type").toLowerCase() == "button"
|| src.getAttribute("type").toLowerCase() == "reset"
)
)
|| src.tagName.toLowerCase() == "option"
|| src.tagName.toLowerCase() == "select"
)
)) {
var defaultButton;
if (__nonMSDOMBrowser) {
defaultButton = document.getElementById(target);
}
else {
defaultButton = document.all[target];
}
if (defaultButton && typeof (defaultButton.click) != "undefined") {
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
}
return true;
}
For this particular issue, the reason is because javascript generated by
ASP.NET 2.0 has some IE only notation: event.srcElement is not availabe in
FireFox (use event.target instead):
function WebForm_FireDefaultButton(event, target) {
if (!__defaultFired && event.keyCode == 13 && !(event.srcElement &&
(event.srcElement.tagName.toLowerCase() == "textarea"))) {
var defaultButton;
if (__nonMSDOMBrowser) {
defaultButton = document.getElementById(target);
}
else {
defaultButton = document.all[target];
}
if (defaultButton && typeof(defaultButton.click) !=
"undefined") {
__defaultFired = true;
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
If we change the first 2 lines into:
function WebForm_FireDefaultButton(event, target) {
var element = event.target || event.srcElement;
if (!__defaultFired && event.keyCode == 13 && !(element &&
(element.tagName.toLowerCase() == "textarea"))) {
Put the changed code in a file and then do
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("js1", "JScript.js");
}
Then it will work for both IE and FireFox.
Source:
http://www.velocityreviews.com/forums/t367383-formdefaultbutton-behaves-incorrectly.html