TextBox with Default value - asp.net

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.

Related

Placeholder/Example text in textbox for user

What is the step to get example text to show up in an asp.net textbox.
For example, textbox w/ ID = "textboxDate" has [mm/dd/yyyy] inside it for the user to reference.
I believe you want a placeholder attribute:
<asp:TextBox ID="placeholderTextBox" runat="server" placeholder="mm/dd/yyyy"></asp:TextBox>
but placeholder doenst work for many IE browser because placeholder is html5 thing.
try to use modernizr framework. it works for all browsers including all IE
here is a sample code for you.
if(modernizr.input.placeholder) {
//insert placeholder polyfill script here.
}
Visual Studio maybe not knowing the attribute. Attributes which are not registered with ASP.net are passed through and rendered as is.
<asp:TextBox ID="TextBox1" runat="server" Width="187px" placeholder="mm/dd/yyyy"></asp:TextBox>
So the above code (basically) renders to:
<input type="text" placeholder="mm/dd/yyyy"/>
You can allways use:
<input ID="placeholderTextBox" type="text" runat="server" placeholder="mm/dd/yyyy" />
and on code behind you can get or set the value using
Dim myValue As String = placeholderTextBox.value or placeholderTextBox.value = "whatsoever"

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

ASP.NET Enter key triggering unwanted button

I have this on the top of a page (in MasterPage.master)
<asp:Panel ID="panSearch" runat="server">
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:ImageButton ID="btnSearch" runat="server" ImageUrl="~/images/iconSearch.gif" onclick="btnSearch_Click" />
</asp:Panel>
The button keeps getting fired when I press the enter key on another TextBox down the page (in an aspx page)
<input type="text" id="txtTagName">
<input type="button" value="Tag" id="btnAddTagOk">
I couldn't find any JavaScript that does that.
Anyone has any idea why it's happening?
Hitting enter will submit the default form. If you have several controls which you want control over the enter button you need to hook in javascript on the control to intercept the enter key press, and then do your own logic.
Something like this
<input type="text" id="txtTagName" onkeydown="if (event.keyCode == 13) document.getElementById('btnAddTagOk').click()"/>
you might have some other button with its type as type="submit". The browser by default submits the form when enter is pressed on textboxes.
you might want to try following on your asp:panel
<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch">
</asp:Panel>

how not to have setfocus on textbox?

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">

ASP.NET : Reading form variable values in the action page of search form

I have a website where i want to implement search functionality.So i added the below code to have a search box in my html page
<form id="search" method="post" action="Results.aspx">
<input id="txtSearchKey" type="text" name="txtSearchKey" />
<input id="Submit1" type="submit" value="submit" /><br />
<br />
</form>
In Results.aspx, I want to read the value user has entered in the txtSearchKey text box. What is the ideal way to do this ? I used
string strKey = Request.Form["txtSearchKey"].ToString();
But it throw a null reference exception.
I don't want to have all pages in ASP.NET.I want to have only the result page as ASP.NET
Could be because you do not have a NAME attribute on the textbox field. That's the value that is used as the key in the Request.Form collection. An input field without a name attribute will not be submitted, I think.
e.g.:
<input id="txtSearchKey" type="text" name="txtSearchKey" />
You can get your txtSearchKey field by this :
string strKey = PreviousPage.Request.Form["txtSearchKey"].ToString();
But, instead of using form action to forward your search to another page, you can use a button with PostBackUrl property like that :
<asp:Button runat="server" ID="btnSearch" PostBackUrl="Search.aspx" />
Because in ASP.NET, to have more then one form is not allowed.
Is there any reason you don't use
form runat="server"
and then drag a TextField and a Button in this form. Then doubleclick the button and write code you want.
If you want to do it your way you need to give your s a name="txtMySearchKey" for it to work
The way you are going about things is not really the way you work in ASP.NET web forms. The preferred way is to use asp.net server controls and events to abstract the process you are trying to achieve. For instance, your form should really be something like this (note the runat="server" attribute that enables you to reference the controls programmatically):
<form id="form1" runat="server">
<div>
<asp:Panel ID="PanelSearch" runat="server" DefaultButton="ButtonSubmit">
<asp:TextBox ID="TxtSearchKey" runat="server" /><br />
<asp:Button ID="ButtonSubmit" Text="Submit" runat="server"
onclick="ButtonSubmit_Click" /><br />
</asp:Panel>
</div>
</form>
Then, in your code behind you would handle the ButtonSubmit_Click event like this to enable you to get the value from the TxtSearchKey textbox:
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
string strKey = TxtSearchKey.Text;
}
See the Quickstart example for the TextBox control for more info.
Just do not use .toString() after the Request.form... it will not give a null reference after that.

Resources