how not to have setfocus on textbox? - asp.net

i have a TextBox control on my masterpage
<asp:TextBox ID="txtSubject" runat="server" Width="280px" CssClass="formtxt"
onblur="if(this.value=='') this.value='Enter text,150 characters only';"
onfocus="if(this.value == 'Enter text,150 characters only') this.value='';"
Text="Enter text,150 characters only" CausesValidation="false"></asp:TextBox>
the problem is that, i dont want the textbox to have a setfocus due to the watermark on the textbox; if i have a setfocus on the textbox the watermark is now showing.

Have a look at Form.DefaultFocus- setting to empty string might help.

Your code works for me, but this might depend on some settings not shown in your sample (e.g. DefaultFocus attribute of the Form element) or the browser being used.
As an alternative, you could use the onkeydown event instead of onfocus:
<asp:TextBox ID="txtSubject" ...
onblur="if(this.value=='') this.value='Enter text,150 characters only';"
onkeydown="if(this.value == 'Enter text,150 characters only') this.value='';" ...>
</asp:TextBox>
Update: I was able to reproduce the behavior you described, when I set <form DefaultFocus="txtSubject">

Related

ASP.NET Provide Styling for TextBox

I am using a multiline textbox as following:
<asp:TextBox runat="server" TextMode="MultiLine" Height="200" Width="500" ReadOnly="true" Font-Names="calibri" Text="Terms and Conditions Next text will go over.. >
What I need to do is to only make Terms and Conditions in bold. Rest of the sentence is not in bold. I tried using a span with a style of bold but that did not affect anything.
How do I do styling for a Textbox? Keep in mind this is a read only textbox.
If i understand you right, u need this
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/HTMLEditorExtender/HTMLEditorExtender.aspx
<script>
function onContentsChange() {
alert('contents changed');
}
</script>
<asp:TextBox runat="server"
ID="txtBox1"
TextMode="MultiLine"
Columns="50"
Rows="10"
Text="Hello <b>world!</b>" />
<ajaxToolkit:HtmlEditorExtender
ID="htmlEditorExtender1"
TargetControlID="txtBox1"
OnClientChange="onContentsChange"
runat="server" >
</ajaxToolkit:HtmlEditorExtender>
what you need is more than a regular textbox, you should either use devexpress or ajax toolikt, or some other ajax libraries.
you might consider looking for library that gives you rich formatting abilities

ASP.net Custom Validator message not displaying

I have a div which acts like a modal popup. Inside that, I need a validation for which I setup a custom validator. But the message doesn't get fired, though the alert box does.
My code:
if ((oldFromTime <= newFromTime) && (oldToTime > newFromTime)) {
alert("Choose time ahead of the ones saved earlier.!");
arguments.IsValid = false;
}
else {
arguments.IsValid = true;
}
And my custom validator
<asp:CustomValidator id="cboToTimeMins_CustomValidator" ControlToValidate="cboToTimeMins" ClientValidationFunction="validateTime"
Display="static" ErrorMessage="Selected time range falls in the range of the ones saved earlier...! Choose another." runat="server" ValidationGroup="Timetable"/>
cboToTimeMins is my dropdown box, and I need to set the validation message based on the value selected from it.
Is there something wrong in my code?
P.S. I am in need of only CLIENT SIDE validation.
Here's my solution. I removed the custom validator for the Dropdown and added it to a button instead. Also, I removed the alert message in the Javascript function.
Here's my example solution
<td class="normal">Price<span class="required">*</span></td>
<td class="normal" colspan="6">
<asp:TextBox ID="txtPrice" CssClass="text" Enabled="true" runat="server" MaxLength="10" Width="100px" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPrice"
ErrorMessage="* Please Input Your Price" Display="Dynamic" ValidationGroup="hdrValidation"/>
</td>
And you need to validate
Page.Validate("hdrValidation")
If Not Page.IsValid Then Exit Sub

TextBox with Default value

I want to use a textbox that shows a text.
For example : Someone#example.com. When the user clicks on it, the textbox is cleared and ready for the user to type.
If you are working on newer browsers then you can use placeholder property which is new in HTML 5
<asp:TextBox ID="textBox1" runat="server" placeholder="Someone#exmaple.com"></asp:TextBox>
otherwise you can also use onfocus and onblur event to do this as described here
you can use this, is so easy
<input class="status" type="text" size="5" placeholder="started" />
placeholder show you the text you want
hope it helps you!
You can do the following:
Add textbox to your webpage.
Set the default text in the obj.Text property.
Add properties ( focus & blur events).
On focus event, check if the value = Someone#exmaple.com , then empty the text box.
On blur, check if the textbox is empty, then set the text : Someone#exmaple.com
Hope that helps
You can use like this
<TextBox ID="txtone" runat="server" tooltip="Enter comments"
onblur="if(this.value=='') this.value='Someone#exmaple.com';"
onfocus="if(this.value == 'Someone#exmaple.com') this.value='';"
Text="Someone#exmaple.com"></TextBox>
Try this for a server control:
<asp:TextBox ID="textBox1" runat="server" placeholder="Someone#exmaple.com"></asp:TextBox>
For an HTML control:
<input Type="Text" ID="textBox1" placeholder="Someone#exmaple.com" />
You can use this ajax if you want a cross browser compatible version.
<ajaxToolkit:TextBoxWatermarkExtender ID="EmailClear" runat="server" TargetControlID="EmailAddress" WatermarkText="Someone#example.com" />
You just have to add this under each field.

Stop Postback in Image Button after validation failed

I have a aspx page containing a text box and an image button for search. I have used compare validator (to check for integer values) with the textbox. But the page reloads on the image button click even if I enter alphanumeric characters, along with showing the error message.
I tried using a regularexpressionvalidator instead but the problem persists.
But when i used a simple asp:button instead and binded it with textbox validation, its working fine (i.e. postback does not occur on incorrect value of textbox) and same is true with dropdownlist also (no postback occuring).
Please suggest.
Here's the code-
#peroija : Here's the code
<asp:ImageButton ID="btnSearch" runat="server" OnClick="btnSearch_Click"
ToolTip="Search" ValidationGroup="valControl" CausesValidation="true" />
<asp:TextBox ID="txtWidth" CssClass="TextFont" runat="server"
Width="233px" MaxLength="20"
ValidationGroup="valControl" CausesValidation="true"></asp:TextBox>
<asp:CompareValidator runat="server" ID="cmpValWidth"
ErrorMessage="Please enter integer values" ControlToValidate="txtWidth" Display="Dynamic"
Operator="DataTypeCheck" ValidationGroup="valControl"Type="Integer"/>
Sounds to me like you need to write
if(!isPostBack)
{
"your code"
}
in the code behind. To prevent the code from being run if the page is not viewed for the first time
Remove this from your textbox, you only need it on the validator and the button:
ValidationGroup="valControl" CausesValidation="true"
If javascript is disabled, then there will be no client side validation, so always check the validity on the server side also:
if(Page.IsValid)
{
"your btnSearch_Click code"
}

validation on textbox (no space)

i have a webpage in that i have a text box, my requirement is i don't want to give space in the textbox if user give space in textbox it give indication no space in the textbox
If you intended to capture a value where spaces aren't a valid character, you could use a RegularExpressionValidator:
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="txtBox"
ErrorMessage="Spaces are not allowed!" ValidationExpression="[^\s]+" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
This would prevent "hello world" and "data base" since they contain spaces, and would only allow "helloworld" and "database" as valid values. You must use a RequiredFieldValidator in conjunction with it to prevent blank entries since the RegularExpressionValidator does not prevent that on its own.
Specify the name of the textbox in the ControlToValidate property.
You can use the RegularExpressionValidator control:
<asp:TextBox runat="server" ID="txt1" />
<asp:RegularExpressionValidator
runat="server" ErrorMessage="Spaces are not permitted"
ControlToValidate="txt1"
ValidationExpression="[^\s]+" />
The pattern [^\s]+ means "one or more characters that is not a space". So if any of the characters is a space, it will fail.
It might help you all
to remove your spaces in your document please try It.
<asp:TextBox runat="server" ID="txttitlename" />
<asp:RegularExpressionValidator runat="server" ErrorMessage="Spaces are not acceptable" ontrolToValidate="txttitlename" ValidationExpression="[^\s]+" />
You can use HTML page or Asp.Net page
only pattern="[^\s]+" in side TextBox
<input id="Text1" pattern="[^\s]+" type="text" />

Resources