Disable html required tag in vb.net - asp.net

i need to disable the required tag of this textbox when a submit button is click
<asp:Textbox runat="server" id="username" required name="username" type="text" placeholder="myusername"/>
i tried to write the following code but it did not work
Dim tbUserName As TextBox =Page.FindControl("username")
tbUserName.required = False
can you please help me !

Since required is not part of the ASP.NET TextBox control's properties, thus there is no server-side property equivalent.
You can use the following to remove it:
username.Attributes.Remove("required")

Try this:
tbUserName.Enabled = False
Also, "Required" isn't a valid propert for the Textbox control. ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.aspx

required is client-side attribute which does not have direct equivalent on server-side.
You can try
username.Attributes("required") = "false"
this works when you're already on the server. If you need to do this on client - handle form's onsubmit event and do something like
$get('username').setAttribute('required', 'false');
Of course the easiest way would be simple remove that attribute from textbox markup.

Related

HTML5 Types in ASP.NET

Site will exclusively be used on mobile devices. So for fields requiring only numeric input, I want to bring up the numeric keypad. I can successfully do this by using an html input element with the type set to tel. I want to add an asp:RequiredFieldValidator to this field and based on MSDN.
I need to set the input to runat="server". When I do this, I get this error;
tel is not a valid type for an input tag.
If I remove the runat="server", I get
Unable to find control id Contract referenced by the ControlToValidate property of ''
Here is my code:
<input type="tel" runat="server" id="Contract"></input>
<asp:RequiredFieldValidator runat="server" ControlToValidate="Contract"
ValidationGroup="IfOnRent" Text="*"
ErrorMessage="Contract Required">
</asp:RequiredFieldValidator>
Am I just out of luck and have to code my own validations?
You may want to leave "type" undeclared in your code "front". You can set this in the code behind (maybe in the Page_Init or Page_Load):
this.Contract.Attributes.Add("type", "tel");
If you have Microsoft .NET Framework 4 Reliability Update 1 (KB2533523) installed, than TextBox accepts new html5 input types (you would need it for UpdatePanel to recognize them on postback too).
Or you can just inherit HtmlInputText in your own control (any .net version - any input type):
[ToolboxData("<{0}:Input runat='server' />"), Themeable(true)]
public sealed class Input: HtmlInputText { }
If you are using C#
Contract.Attributes["type"]="tel"
Writing inline types for html inputs may produce parse error in .net. So add the above line of code in your PageLoad()
you can use
Make sure that the submit button has the same validationgroup property value as the requiredfieldvalidator

Disabling an ASP.net textbox without actually disabling it?

Within an ASP.Net application I have, there is a textbox that gets a date from a CalendarExtender. When the textbox is populated it checks that date with another date on the form and displays a modalpopupextender popup if the dates are wrong. However, I DO NOT want to allow user input into this textbox, so when I set the ReadOnly field to false and tried Enabled to false, it doesn't allow manual entry however it ALSO disabled the postback and will not call the TextChanged event to fire the modalpopupextender.
So is there a way to disable manual entry and not set it to ReadOnly?
I figured it out, simply enter onkeypress="return false;" within the HTML tag
Try this
<asp:textbox id="txt1" onfocus="blur()" runat="server"/>
this worked for me.
Add the below properties in the tag of textbox
onkeydown="return false" onpaste="return false"
ex:
<asp:TextBox ID="TillDate_TextBox" runat="server" onkeydown="return false" onpaste="return false"></asp:TextBox>
the first property block typing in textbox and the second property block pasting in it
I'm not familiar with the exact components you are using, however the usual way to accomplish things like this is the following. Have selecting the date on the calendar modify the value of a hidden form field. This will restrict the user from editing the value directly. Then create another element like a div or a span, and use javascript to update the span/div to the value selected on the calendar.

How to fill a Label.Text - Property via jQuery

I use ASP.NET and have a label control on my page, which I fill with
the jQuery-Command
$('#<%= myLabel.ClientID %>').html(content);
.val() does not seem to work with this.
Somehow, I have Problems getting the content in code-behind. In the code, the myLabel.Text-Property is still empty.
If you want to display the value on the client and have it available on the page, you need an input that'll get sent to the code-behind when you POST like this:
$('#<%= myLabel.ClientID %>').html(content);
$('#<%= myInput.ClientID %>').val(content);
<asp:Label Id="myLabel" runat="server" />
<asp:HiddenField ID="myInput" runat="server" />
In the code-behind:
myInput.Value
I think your problem is that labels (rendered as span tags) are inherently read-only in the asp.net world. They're not meant to be used as 'input' controls, and as such changes to their HTML on the client-side are ignored on the server-side, where values are set based on ViewState.
To do what you are asking, you'd have to notify the server of the change as well, such as by using AJAX. The only issue here is ajax webmethods in your code behind are static, and because of this can't access the page's control set to change the .Text value.
In the end the easiest option is to make use of hidden fields as Nick said. These are technically 'input' controls and their values changed on the client-side are sent to the server as you desire. You'd just have to keep the label/span and hidden field/input synchronized on the client.
Hope this helps.

How to get value in a textbox that is readonly

I have a textbox.In its onclick iam calling a javascript fn to call calendar.Texbox is readonly. Clicking on textbox calendar is coming and value is showing in textbox. But on clicking submit button where I have written code to save, there i am not getting value in textbox. What may be the reason for that?
I suspect that your text box has the disabled attribute instead of readonly which prevents it from posting its value to the server.
I had this problem too, there is a really simple workaround
Instead of making the textbox ReadOnly using properties option. Do it thru the code behind adding this code:
YourTextBox.Attributes.Add("readonly", "readonly");
You can get the value by using
Request.Form[YourTextBox.UniqueID]
I'm guessing the textbox is disabled so that people have to use the calendar control to enter a date? The textbox is just for showing the selected date?
If you want the textbox to stay readonly (i.e. disabled client-side), have a hidden input that has the value you want to handle on the server is the way to go probably. So add an additional input for to the page. Use
<asp:HiddenField ... runat="server" />
So the client-side code that updates your readonly textbox will also update your hidden input.
if readonly property is true, its not break your .cs call. you can use this as always:
here are your inputs:
<asp:TextBox ID="txtBraid" runat="server" Text="Im sooo readonly" ReadOnly="True"></asp:TextBox>
<asp:Label ID="lblBraid" runat="server" Text="Im gonna change, i promise"></asp:Label>
on .cs page put these to onClick function or something:
lblBraid.Text = txtBraid.Text;

Adding javascript to the OnBlur property of an ASP.NET text box control

Is there a way to specify some JavaScript to execute on the OnBlur event of an ASP.NET text box? It seems to me like if I add any event handlers to the TextBox object they will just cause postbacks to the server isntead of doing what I want. Basically, I just want to be able to have the textbox be rendered in this HTML:
<INPUT type="text" onblur="alert('1234')" />
Thanks!
Could also go for:
<asp:TextBox runat="server" onblur="Javascript:alert('1234');" />
if you dont feel like setting it up in the codebehind.
Im guessing the reason why you end up with postbacks, must be because you have set AutoPostBack on the textbox to true. That makes the textbox postback when the client-side onchange event is triggered. Switch it to false, and it will act as a normal input-element.
In your codebehind, add this:
myTextBox.Attributes.Add("onblur","alert('1234');");

Resources