Disabling an ASP.net textbox without actually disabling it? - asp.net

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.

Related

Set Default Button Enter - Master Page

Hi everybody i have the next problem. I have to set a button as default when i press Enter. I can use DefaultButton in the Form because now all my pages inherits from Master Page and i have a Content from the Master Page and this isn't work. Somebody could give me any alternative to solve this please. Thanks
According to Enter Key - Default Submit Button:
ASP.NET 2.0 introduces a wonderful work around for this. By simply
specifying the "defaultbutton" property to the ID of the ,
whose event you want to fire, your job is done.
The defaultbutton property can be specified at the Form level in the
form tag as well as at panel level in the definition tag.
The form level setting is overridden when specified at the panel
level, for those controls that are inside the panel.
Also, the Event Handler for the specified button, fires thereby
simulating a true submit button functionality.
Like this
<form id="form1" runat="server" defaultbutton="Button1">
<div>
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
</div>
Or you can achieve this by
Page.Form.DefaultButton = crtlLoginUserLogin.FindControl("LoginButton").UniqueID
or just
Page.Form.DefaultButton = LoginButton.UniqueID
This will work.
One way is through to recursively search through all your child pages controls and find the first button, get the id and set the default button of your form.
Although I have never tried this, I dont think its a very good idea as it is slow and error prone.
An alternative may be to do it through javascript/jquery, see this answer:
Submit form with Enter key without submit button?

ASP.NET: checkboxlist with textbox?

I'm working in ASP.NET and I have a CheckBoxList where I want one of the options to be basically like "Other: _." So I need to include a textbox where the user can fill in their own option. It doesn't seem like there's a way to include a textbox inside of a checkboxlist, however. What's the best way to make this work?
-UPDATE-
If using a separate textbox control, how do I position it so it will align correctly with the checkbox?
Make the textbox a separate control on the page, then in your codebehind, check to see if other is checked. If it is, pull the value of the textbox, and use that.
To answer the question in your edit: You'll have to play with the CSS of the page to get it positioned correctly. How you do it depends on the layout of the page, among other things. I recommend posting some of the HTML from your page in another question and ask about how to position them.
What #Kyle Trauberman said...
Make the textbox a separate control on
the page, then in your codebehind,
check to see if other is checked. If
it is, pull the value of the textbox,
and use that.
Plus use javascript to hide or gray out the option unless the checkbox is selected.
string test="";
<asp:CheckBoxList ID="chk_list" runat="server">
<asp:ListItem Value="00">xxxx</asp:ListItem>
</asp:CheckBoxList>
<asp:TextBox ID="other" runat="server"></asp:TextBox>
inside the for loop
if (chk_list.Items[i].Value == "00")
{
test +=chk_list.Items[i].Text + other.Text;
}

Can't change an asp page because of a required validator

On one of my asp.net pages I have a few textboxes and a required validator attached to them. Everything is ok with validation, but when I want to change page to another required validator doesn't allow it because I don't fill in a textbox :/
How to allowed change page without fill a validate textbox? I must create a condition in "exit page" (how is it named ?) event disabled required validation?
Please help ;)
If you're using some control to move to the next page, set it's CausesValidation property to False.
For example, if you're clicking a button that moves you to the next page, it would be like:
<asp:LinkButton id="myButton" runat="server" CausesValidation="False" .... />

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;

Ajax dropdown limit to list

pretty much what the title says.
I am using an Ajax Drop Down as illustrated here:
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/DropDown/DropDown.aspx
using linkbuttons ... is there a way to limit to list?
Thank you.
Edit: I think it was VB 6 maybe that you could select "LimitToList" in a drop down. Meaning the user can only select the values in the drop down and not enter his own data.
Since you're extending a textbox, I think the best option would be to attach an event listener that voids keypresses, you could do this in the ASPX:
<asp:Textbox id="txtFoo" onkeypress="return false;" runat="server"/>
Or, in the code behind:
txtFoo.Attributes.Add("OnKeyPress","return false;");
This will prevent a user from typing in the textbox, essentially creating the effect you want.
A bonus side effect is that a user is allowed to free type an entry if javascript is disabled and the dropdown extender doesn't work.

Resources