Showing posts with label textbox. Show all posts
Showing posts with label textbox. Show all posts

Wednesday, March 28, 2012

Changing Background Color Of Text box ?

Hello,

I need something as Follow..

I am having one Data Entry form. Here I have many controls (All type of controls like Textbox, Dropdownlis, Checkboxex esc...)

Now when user focus any text box then its Background color should change. Can we do it using ATLAS, xmlScript, Javascript...

one more problem is that I am having nearly 20 Text boxes on my page so which way I can get this functionality using less efforts...

Atlas is a really "heavy" way to do this. I'd just do it with Javascript and CSS.

<style type="text/css">

.normal

{

background-color:inherit;

}

.focus

{

background-color:yellow;

}

</style>

<script type="text/Javascript">

function changeTextbox(control){

var x = document.getElementByID(control);

if (x.classname == "normal"){

x.classname = "focus";

}

else {

x.classname = "normal")

}

}

</script>


Nice..

But the Parameter CONTROL in function... comes from where ?

Means from where we can pass it ?


You pass the control paramater when you call the function from the onFocus even of your textbox. It would be just as easy to capture the sender though, and get the calling object of the function, so it didnt need a paramater.
You don't need atlas or ajax at all, some javascript and dhtml will do:
foreach (Control ctrlin Form.Controls){if (ctrl.GetType() ==typeof(TextBox)) { ((TextBox)ctrl).Attributes.Add("onfocus","javascript:this.style.backgroundColor='red'"); ((TextBox)ctrl).Attributes.Add("onblur","javascript:this.style.backgroundColor='white'"); }}

Change the event that causes the popupcontrol to shows the control

I want to use popupcontrol with a textbox, but I want it shows the popup just when the onKeyDown event fires. How can I do it (if it can be done)?You'll want to look at modifying the way PopupControlBehavior.js's this.initialize function attaches its events. You'll probably need to customize the events a little, but it shouldn't require many changes.

Change TextBox Text and Force UpdatePanel to Update

Can somebody show me a way of forcing an updatepanel to update using javascript.

Thanks

Hi~ Once I used a button to trigger the update and invoked the button.click() in javascript..

I know this is not some elegant solution.. but you can give it a try when there's no better way~

Change textbox css class onfocus

I'd like to have a textbox that changes css classes (bgcolor primarily) when the user is typing in the box (onfocus). I'd also like to have the textbox validated onblur and if the textbox is invalid change css class again.

I've accomplished all of this using maskededit and maskededitvalidator, however for a basic string entry such as a name I don't want any mask characters to be used in the textbox. My app will be used by very computer illiterate people, and I'm pretty sure having the ____ in the box or even having spaces will cause a lot of confusion and trouble so I would like to avoid that.

What is the best way for me to accomplish this? Also FYI, I'm a former ASP/javascript developer trying to make the move to .net.

Your best bet is to register a .js file in your scriptmanager's Scripts collection.

In that .js file, create a function called pageLoad() (the framework automatically wires that for you to fire when all the other html elements and ajax compoennts are done loading).

In your pageLoad function add an event handler to your input control like so:

$addHandler($get('inputControlId'),'focus',handler);

handler is the name of a function that you define next.

function handler(ev){
Sys.UI.DomElement.toggleCssClass(ev.target,'onfocusclassname');

}

where 'onfocusclassname' is defined in your CSS document as the style you want applied to the control when the control has focus.

Hope that helps.


That helps a ton, that's basically what I wanted to do I wasn't sure exactly how to do it. And am still not 100% sure, such as where i register this .js file, but I can probably figure that out. I assume I would then make a 'blur' handler as well.

paul.vencill:

$addHandler($get('inputControlId'),'focus',handler);


Do I need to do this for each textbox? inputControlId appears to be hardcoded. I want to have this functionality for all text boxes in an application, which will be hundrerd of textboxes once all the pages add up.

And I guess I would have to write my own client side validation functions then, in order to make the text box red after an invalid entry.


Well, there's a number of ways to skin any cat, of course. First, your questino on registering; the way I prefer to do it is to use the scriptmanager on the page:

<asp:ScriptManager id='scm' runat='server'> <Scripts> <asp:ScriptReference Path='myscript.js' /> </Scripts></asp:ScriptManager>

Like any control, you could also register it in the codefile, your choice.

As for your question on the volume of handlers you need to add, the $addHandler function requires as its first parameter a reference to a DOM element (hence using $get('id'); which returns a reference to the element itself). If you want all your text inputs then use document.getElementsByTagName() and then filter inside a loop like so:

var inputs = document.getElementsByTagName("INPUT");var count = inputs.length;for (var i=0;iif(input.type && input.type.toUpperCase() =='TEXT') $addHandler(input,'focus',handler);}

Now, for extra fun, if you want to add both a focus and a blur handler to the same object, then instead of $addHandler you can use $addHandlers (with an 's'). It's a convenient shortcut: int he above function change the $addHandler line to:

$addHandlers(input,{'focus' : focusHandler, 'blur' : blurHandler});

As for adding your own validation; yes, I'd submit that it'd probably be easier than trying to get the validation controls to play nicely with your custom script. Just make sure to validate on the server, too, to catch values from clients that have javascript disabled.

Change Textbox background with ValidatorCallout.

Hi All,

I want to replicate what is done here:

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ValidatorCallout/ValidatorCallout.aspx

When the submit button is clicked, the 'name' and 'phone number' backgrounds change color...

Any help would be great.

Thanks

Keegan

Hi Keegan,

There is a pretty good sample contained inside the AJAX Control Toolkit's source code. You can find it under SampleWebSite\ValidatorCallout directory. Here is the sample code. Please pay attention to the "Bold" part.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title> <style> .validatorCalloutHighlight { background-color: lemonchiffon; } </style></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <table> <tr> <td>Name:</td> <td><asp:TextBox runat="server" ID="NameTextBox" BorderStyle="solid" BorderWidth="1px" BorderColor="#a9a9a9" /></td> </tr> <tr> <td>Phone Number:</td> <td><asp:TextBox runat="server" ID="PhoneNumberTextBox" BorderStyle="solid" BorderWidth="1px" BorderColor="#a9a9a9" /></td> </tr> </table> <br /> <asp:RequiredFieldValidator runat="server" ID="NReq" ControlToValidate="NameTextBox" Display="None" ErrorMessage="b><br />A name is required."> <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="NReqE" TargetControlID="NReq" HighlightCssClass="validatorCalloutHighlight" /> <asp:RequiredFieldValidator runat="server" ID="PNReq" ControlToValidate="PhoneNumberTextBox" Display="None" ErrorMessage="b><br />A phone number is required.<div style='margin-top:5px;padding:5px;border:1px solid #e9e9e9;background-color:white;'><b>Other Options:</b><br /><a href='javascript:alert("No phone number available in profile.");'>Extract from Profile</a></div>"> <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="PNReqE" TargetControlID="PNReq" HighlightCssClass="validatorCalloutHighlight" Width="350px" /> <asp:RegularExpressionValidator runat="server" ID="PNRegEx" ControlToValidate="PhoneNumberTextBox" Display="None" ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}" ErrorMessage="b><br />Please enter a phone number in the format:<br />(###) ###-####"> <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="PNReqEx" TargetControlID="PNRegEx" HighlightCssClass="validatorCalloutHighlight" /> <asp:Button ID="Button1" runat="server" Text="Submit"/><br /><br /> </form></body></html>

I hope this help.

Best regards,

Jonathan


Hi Jonathan,

Thank you very much for the reply. It pointed me in the right direction:

I created a class in my linked CSS file:.validatorCalloutHighlight{background-color:Red;}

and set the property of myValidatorCalloutExtender to call:HighlightCssClass="validatorCalloutHighlight".

Worked like a charm.

Thanks again.

Keegan

change FilteredTextBoxExtender filter type dynamically

Hi,

Im trying to change the Filter Type of FilteredTextBoxExtender on a checkbox onClick handler.

(The textBox which is my traget control is also a the target control of an AutoCompleteExtender control).

The user can switch between searching an item by its Number ID or by Its name)

therefore I need to change the type dynamically according to the checked property of a check box.

it works fine until I try to change the valid characters. I get the following exception on client side:

Microsoft JScript runtime error: Sys.ArgumentTypeException: Object of type 'String' cannot be converted to type 'AjaxControlToolkit.FilterTypes'.
Parameter name: value

HTML:

<ajaxToolkit:FilteredTextBoxExtenderID="FilteredTextBoxExtender1"runat="server"BehaviorID="FilteredTextBoxTxtStock"TargetControlID="txtStkNum"FilterType="Numbers, Custom">

</ajaxToolkit:FilteredTextBoxExtender>

  
1function chkNameList_click()2 {34 var autoComplete = $find('AutoCompleteBehavior');5 var TextBoxFilter = $find('FilteredTextBoxTxtStock');67 $get('<%=txtStkNum.ClientID%>').value ="";89if($get('UserContext').checked ==true)//Names10 {11 $get('<%=txtStkNum.ClientID%>').className ="txtStockHeb";//right alignment (text box)12 TextBoxFilter.set_ValidChars("?????????????????????? ");1314 }15else//Numbers16 {17 $get('<%=txtStkNum.ClientID%>').className ="txtStockEng";//left alignment (text box)18 TextBoxFilter.set_FilterType("Numbers");19 }20 }

 Thanks,
Tal

Hi Tal88,

Based on my research , I think your problem is mainly caused by TextBoxFilter.set_FilterType("Numbers");

Below is the FilterTypes,

AjaxControlToolkit.FilterTypes.prototype = {
Custom : 0x1, //1
Numbers : 0x2, //2
UppercaseLetters : 0x4, //3
LowercaseLetters : 0x8 //4
}

So if you want to set the FilterTypes to Custom, you should use TextBoxFilter.set_FilterType(1);

Numbers will be TextBoxFilter.set_FilterType(2);

If the FilterTypes is set to "Custom,Numbers,LowercaseLetters", please use TextBoxFilter.set_FilterType(7); //1+2+4

My Ajax Control Toolkit's version is V10618.

I hope this help.

Best regards,

Jonathan

Change element on click

Hi,

I'm trying to create an AJAX-enabled control thas has the following functionality:

it's a Label that, when clicked, changes into a TextBox. The user can enter some text into the textbox en when he clicks outside the box (onblur) the textbox changes to a label again (now containing the entered text).

For this I created a server control inheriting from Label and a corresponding client control

The question is: can I change the displayed control in the _onClick event of the client control.

It should be something like:

_onClick : function(e){
this.element = $create(TextBox);
},

Does anyone know the right methods for this?

Thanks,

Thomas

Hmmm... I don't know if you can replace a control like that really. In that case, you would have to go to the parent element in the DOM tree, remove the label element and then add the text element. But that's very cumbersome.

What if you create both Label and TextBox and you toggle visibility on the two? Try changing the css attribute "display". Set it to block to show the element and none to hide it. Could help you with code in case you get stuck, just let me know. Good luck!

Monday, March 26, 2012

Change a textbox style property on failed validation

Can someone point me in the right direction for implementing a textbox that has it's border changed when a RequiredFieldValidator fails? Ideally this would be using either an extender or the control toolkit.

Thanks.

Hi,

The easiest way would be using CustomerValidator control, then add javascript function to change the style if validation fails.
Hope this helps.


Thanks, I ended up using a validator extender that styles the textbox if the validation fails.


Thats nice...Smile

Catching key press events. For example username control without postback

Hi atlas developers and implementers,

For example I have a textbox and I want that on every key press the written string will be controlled from database and it should say whether it is found on database or not. I'm using atlas 2.0. It is like username control without postback.

How can I do this..

Kind Regards,

Fatih U?AR.
Postglobal Ltd.
R&D ManagerHi,

basic example + bonus behavior:

<%@. Page Language="C#" %><%@. Import Namespace="System.Data" %><%@. Import Namespace="System.Web.Services" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> private DataTable data = null; protected void Page_Load(object sender, EventArgs e) { BuildData(); } private void BuildData() { data = new DataTable(); data.Columns.Add(new DataColumn("Username", typeof(string))); data.Rows.Add("garbin"); data.Rows.Add("leoscorpio82"); } [WebMethod] public bool CheckForUsername(string username) { bool userExists = data.Select("Username = '" + username + "'").Length > 0; return userExists; }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <atlas:ScriptManager ID="sm" runat="server"></atlas:ScriptManager> <div> <span>Username:</span> <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox> </div> </form> <script type="text/javascript"> function txtUsername_Keypress(sender, e) { PageMethods.CheckForUsername($object('txtUsername').get_text(), onCheckComplete); } function onCheckComplete(result) { $object('txtUsername').get_style().backgroundColor = (result) ? '#ffffff' : '#ff0000'; } </script> <script type="text/xml-script"> <page> <components> <textBox id="txtUsername"> <behaviors> <keyUpBehavior keyup="txtUsername_Keypress" /> </behaviors> </textBox> </components> </page> </script> <script type="text/javascript"> // A behavior to handle the keyup event. Type.registerNamespace('AtlasExamples'); AtlasExamples.KeyUpBehavior = function() { AtlasExamples.KeyUpBehavior.initializeBase(this); // Private members. var _keyupHandler; // Events. this.keyup = this.createEvent(); // Initialize / Dispose. this.initialize = function() { AtlasExamples.KeyUpBehavior.callBaseMethod(this, 'initialize'); _keyupHandler = Function.createDelegate(this, keyupHandler); this.control.element.attachEvent('onkeyup', _keyupHandler); } this.dispose = function() { this.control.element.detachEvent('onkeyup', _keyupHandler); _keyupHandler = null; AtlasExamples.KeyUpBehavior.callBaseMethod(this, 'dispose'); } // Descriptor. this.getDescriptor = function() { var td = AtlasExamples.KeyUpBehavior.callBaseMethod(this, 'getDescriptor'); td.addEvent('keyup', true); return td; } // Event Handler. function keyupHandler() { //debug.trace(event.keyCode); this.keyup.invoke(this, Sys.EventArgs.Empty); } } AtlasExamples.KeyUpBehavior.registerClass('AtlasExamples.KeyUpBehavior', Sys.UI.Behavior); Sys.TypeDescriptor.addType('script', 'keyUpBehavior', AtlasExamples.KeyUpBehavior); </script></body></html>

hello ,

the answer is ok but where is the 'AtlasExamples' behavior

i'll be glad for you answer

kind regards,


Hi,

AtlasExamples is a namespace (defined with a call to Type.registerNamespace at the beginning of the Javascript code) while the behavior is the AtlasExamples.KeyUpBehavior class defined inside the Javascript code block (it inherits from the base Sys.UI.Behavior class).


hi garbin yes i know but i don't have the dll file of the namespace. so i can't get it work. I need necessary files.. I'll be glad if you send me the files tofatihu@.postglobal.com

regards,


Hi,

sorry, but what do you mean by "the dll of the namespace"?


I mean, I run the code you wrote but it didn't work, what should I do, it didn't validate

Hi,

ah, sorry, didn't notice that the code was written for CTPs :)

Here's the updated code:

<%@. Page Language="C#" %><%@. Import Namespace="System.Data" %><%@. Import Namespace="System.Web.Services" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> private static DataTable data = null; protected void Page_Load(object sender, EventArgs e) { BuildData(); } private void BuildData() { data = new DataTable(); data.Columns.Add(new DataColumn("Username", typeof(string))); data.Rows.Add("garbin"); data.Rows.Add("leoscorpio82"); } [WebMethod] public static bool CheckForUsername(string username) { bool userExists = data.Select("Username = '" + username + "'").Length > 0; return userExists; }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="TheScriptManager" runat="server"> <Scripts> <asp:ScriptReference Assembly="Microsoft.Web.Preview" Name="PreviewScript.js" /> </Scripts> </asp:ScriptManager> <div> <span>Username:</span> <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox> </div> </form> <script type="text/javascript"><!-- function txtUsername_Keypress(sender, e) { PageMethods.CheckForUsername($get('txtUsername').value, onCheckComplete); } function onCheckComplete(result) { $get('txtUsername').style.backgroundColor = (result) ? '#ffffff' : '#ff0000'; } //--> </script> <script type="text/xml-script"> <page xmlns="http://schemas.microsoft.com/xml-script/2005" xmlns:cc="AtlasExamples"> <components> <textBox id="txtUsername"> <behaviors> <cc:keyUpBehavior keyUp="txtUsername_Keypress" /> </behaviors> </textBox> </components> </page> </script> <script type="text/javascript"> // A behavior to handle the keyup event. Type.registerNamespace('AtlasExamples'); AtlasExamples.KeyUpBehavior = function(element) { AtlasExamples.KeyUpBehavior.initializeBase(this, [element]); } AtlasExamples.KeyUpBehavior.prototype = { // Initialize / Dispose. initialize : function() { AtlasExamples.KeyUpBehavior.callBaseMethod(this, 'initialize'); $addHandlers(this.get_element(), {'keyup':this._onKeyUp}, this); }, dispose : function() { $clearHandlers(this.get_element()); AtlasExamples.KeyUpBehavior.callBaseMethod(this, 'dispose'); }, // Event Handler. _onKeyUp : function(evt) { this._raiseEvent('keyUp', Sys.EventArgs.Empty); }, // Event add_keyUp : function(handler) { this.get_events().addHandler('keyUp', handler); }, remove_keyUp : function(handler) { this.get_events().removeHandler('keyUp', handler); }, _raiseEvent : function(eventName, eventArgs) { var handler = this.get_events().getHandler(eventName); if (handler) { if (!eventArgs) { eventArgs = Sys.EventArgs.Empty; } handler(this, eventArgs); } } } // Descriptor. AtlasExamples.KeyUpBehavior.descriptor = { events: [{name: 'keyUp'}] } AtlasExamples.KeyUpBehavior.registerClass('AtlasExamples.KeyUpBehavior', Sys.UI.Behavior); </script></body></html>