How to set passwordchar property to asp.net TextBox? - c#

I have one TextBox for Password and i want to set character for password, so which property can be used in asp.net?
<asp:TextBox TextMode="Password" runat="server" />

PasswordChar property is under this namespace, System.Windows.Forms. It is not available for asp .net web applications.
You could try JavaScript to change it into other characters such as "*" or "#".
You can see the following example to do so:
<script type="text/javascript">
function onTextChange(obj)
{
document.getElementById('hdnActualTextValue').value = obj.value;
obj.value = '';
for(int i=0; i<obj.value.length;i++)
{
obj.value +='*';//OR ANY OTHER CHARACTER OF YOUR CHOICE
}
}
</script>
<asp:TextBox ID="txtValue" runat="server" onblur="javascript:onTextChange(this);"
onkeyup="javascript:onTextChange(this);" onkeypress="javascript:onTextChange(this);"></asp:TextBox>
<asp:HiddenField ID="hdnActualTextValue" runat="server" />
OR maybe you could use input in HTML.
<label>PW: <input type="password"></label>

I think if you are using the Web Forms then in that the Password Characters are fixed. You can not change unless you use Javascript to change some behavior at run time
When we use WebForms in that PasswordCharacters are set by default.
Check out this link. You can get some Idea what I am telling you.

Simply use TextMode property in asp
<asp:TextBox ID="txtValue" runat="server" TextMode ="Password"> </asp:TextBox>

Related

Find input value and associated html label text in asp.net

I have the following setup:
<label for="<%= inpUserName.ClientID%>">Email</label>
<asp:TextBox id="inpUserName" runat="server" />
As you may noticed the label is not asp's label. How do I get the value of the input and the text of the label?
Edit
I am trying to loop through the posted form keys and get associated labels.
You could use <asp:Label> directly, just need to specify AssociatedControlID
<asp:Label ID="UsernameLabel"
Text="Email"
AssociatedControlID="inpUserName"
runat="server">
<asp:TextBox id="inpUserName" runat="server" />
Make the label server-side:
<label runat="server" id="lblEmail" for="<%= inpUserName.ClientID%>">Email</label>
Then you can access its text in code behind like this:
// label text
string labelText = lblEmail.InnerText;
// input value
string inputValue = inpUserName.Text;
change to:
<label runat="server" id="ClientID" for="<%= inpUserName.ClientID%>">Email</label>
You can find the textbox using the Id var txt = inpUserName.Text; for the label you can't, since its not a server side control and its not a request variable (input with value for instance) then you can't, unless you make it server side too and access in the same manner.

Changing value of input element without changing element id

I have an ASPX page with an input element. On page load I set the value of that input element. Later on I try to get the contents of that field using the elements id(using javascript) but it fails because the id has been changed. Is there any way to stop C# from changing the elements id?
input element:
<input id="conferenceDate" runat="server" style="width:100%;" />
Setting the date in the Page_Load function:
conferenceDate.Value = DateTime.Now.ToString();
The id after the element vale has been set:
<input name="ctl00$PlaceHolderMain$conferenceDate" type="text" id="ctl00_PlaceHolderMain_conferenceDate" style="width:100%;" value="7/21/2014 11:30:55 AM" />
UPDATE: I have tried the following but neither work the id is still changed:
<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static" />
and
When I try to select them later with jQuery I have to use the altered id:
$("ctl00_PlaceHolderMain_conferenceDate").val()
In addition to setting the ClientIDMode to static, you can also find the id using the ClientID property.
<%= conferenceDate.ClientID %>
Using your sample information you can find it like this:
Single or double quotes and dont forget the hash mark (or dot if its a class).
$("#<%= conferenceDate.ClientID %>").val()
Try setting the ClientIDMode of the Textbox to "Static" (More info: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx):
<input id="conferenceDate" runat="server" ClientIDMode="Static" style="width:100%;" />
You can set the ClientIDMode to Static.
This will prevent the controls ID from changing at runtime.
E.g:
<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static" />
Why not to use the asp:textbox and set the ClientIDMode to static?
<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static"></asp:TextBox>

How to make an asp Validator validate through jQuery/javascript

I've got an asp:RequiredFieldValidator, which checks an asp:TextBox to see if it is blank or not.
On a button press, I would like to make the validator validate the textbox. This must be done through jQuery/javascript, as the button is an html input button.
Any ideas? I've read many resources on the web, but have not managed to accomplish this (i.e. the calling of validation through jquery)
You can trigger validation from JavaScript like this:
var isValid = Page_ClientValidate("");
If you only want to validate controls in a certain group just pass the group name into the function:
var isValid = Page_ClientValidate("GroupName");
Here's a quick example:
<script type="text/javascript">
validateStuff = function(){
return Page_ClientValidate("ValidateTextBox");
}
</script>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"
ValidationGroup="ValidateTextBox"
Display="Dynamic"
ErrorMessage="*" ...>
</asp:RequiredFieldValidator>
<input type="button" value="Click Me" onclick="return validateStuff();" />
if its jQuery validation your after - there are few better / easier to implement than this one -> http://bassistance.de/jquery-plugins/jquery-plugin-validation/
All you need is a CustomValidator. Add OnServerValidate for server-side, and ClientValidationFunction to your javascript. Here is a reasonably good intro to CustomValiditors but no jQuery reference: https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx

get asp:textbox value in client side using javascript function doesn't work

This code doesn't display the value, I don't know why?
I have server control:
<asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll" />
in javascript function:
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
I enter text then click on button that call the javascript function, but the alert box doesn't display the entered text.
EDIT: when I initialize the text with Text="some text", it is displayed in alert, I want to enter text in client side and get the value of it in the Javascript function.
Thanks
Using label or textbox visible set false so it can access the value in JavaScript
Sol
1)
Make a div and set style="display:none;" so label is not display at UI(browser) but value can access in JavaScript.
This is because you server Control is called "txtTest" not "txtEventDescription"
change your javascript function to :
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
EDIT: ok, I see you've now changed the post to show the code and renamed the js control, so above is no longer relevant (for those who are confused by my answer) :-)
The problem is the Visible="false" - this control will not render into the client and will therefore not be accessible via javascript (as the HTML element does not exist client side)
So, hide the element using CSS and then call alert on it. Sample snippet
CSS
.hide-element {
display: none;
}
HTML Markup
<asp:TextBox ID="txtTest" runat="server"
Columns="23"
CssClass="white-scroll hide-element"
Rows="3"
TextMode="MultiLine"/>
JavaScript
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
This way you will definitely get an alert.
You alert is empty because you have not set the property Text for your asp:Textbox
Make it Visible="true" to your textbox and than test.
<asp:TextBox ID="txtTest" runat="server" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll" />
if "txtTest" textbox has visible="false" in that case its not render on html code on client machine and if it hasn't on client's html code then how javascript calls this textbox. Because when javascript search this textbox by its id it doesn't find and it gives an error.
you can assign any other custom attribute to the control i.e.
<asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll"
clientID="myClientID" />
and then access control using jquery like
var txtVal = $('textbox[clientID=myClientID]').val();
hope it helps.

Setting display in .NET code

I have a modal popup with a formview with a default mode of Edit. The controls inside the form are bound to data. I have JavaScript that shows a textbox and a label only if a certain value is selected from a dropdownlist. I want to do this on load also.
<tr>
<td align="left">
<asp:DropDownList
ID="ddl5"
AutoPostBack ="false"
onchange = showifother('12',this.value);"
SelectedValue='<%# Eval("event_type") %>'
runat="server"
DataSourceID="SqlDataSource5"
AppendDataBoundItems ="true"
DataTextField="Description"
DataValueField="ID">
<asp:ListItem
Selected="True"
style="color:gray"
Value="" >Causes of Damage</asp:ListItem>
</asp:DropDownList>
<tr id="treditpropdamage" style="display:none">
<td align="left">
<asp:TextBox
ID="txt2"
runat="server"
TextMode ="MultiLine"
ondatabinding="TextBox2_DataBinding"
ValidationGroup ="Editprop"
Text = '<%# Bind("bounddata") %>'></asp:TextBox>
</td>
</tr>
function showifother(num, value)
{
var treditpropdamage = document.getElementById('treditpropdamage');
if (num == '12')
{
if (value == '4')
{
treditpropdamage.style.display = '';
}
else
{
treditpropdamage.style.display = 'none';
}
}
}
How can I set the tr tag to visible if 4 is the value of the bound value for the Dropdownlist in C#? Adding Runat="server" is not an option because I want to access the controls in JavaScript via document.getelementbyid
A few choices:
Add the style (display:none) on the server side (this means you have duplicate logic in the C# and Javascript)
Check the value of your dropdown when the form loads (putting something like: showifother('12',document.getElementById('ddl5').value in your javascript might help)
I'd really recommend you download and learn to use a framework, like jQuery, dojo or Prototype .js - they'll speed up your development and provide you with lots of tools to make your life easier.
If you're using a MasterPage I think this would do:
1) set the body tag of your Master Page or Page to runat="server" and give it an id: id="MainBody"
2) Reference the javascript file like you would normally do:
<-script src="yourJavascriptFile.js" type="text/javascript">
3) Now, in code-behind you can attach the onload attribute to the body tag:
protected void Page_Load(object sender, EventArgs e)
{
MasterPageBodyTag = (HtmlGenericControl)Page.Master.FindControl("MainBody");
MasterPageBodyTag.Attributes.Add("Onload", "YourJSFunction()");
}
Keep in mind that I have not tested the above example :)
You can set the TR's display style using a quick script block and a ternary operator
<tr id="treditpropdamage"
style="display:<%= ddl5.SelectedValue == "12" ? "" : "none" %>">
Also as an FYI you can still access runat="server" elements in javascript, you just need to provide javascript with the elements clientID. You can do that by injecting the id into the javascript like so:
<script>
var element = document.getElementById("");
</script>
Use jQuery! Download the .js from http://jquery.com/ or link the jQuery lib from Google's CDN http://code.google.com/apis/libraries/devguide.html#jquery
Here is my sample code:
http://jsfiddle.net/jphellemons/rPK52/
if ($("#ddl5").val() == 4)
$("#treditpropdamage").show();
With Asp.Net 4 you can use clientidmode
otherwise use:
if ($("#<%=ddl5.ClientId%>").val() == 4)
$("#treditpropdamage").show();

Categories