AJAX Calendar extender on modalpopup returns null - asp.net

I'm having some trouble getting a datetime from calextender on a modalpopup I have.
<asp:TextBox ID="txtPopEndDate" runat="server" Enabled="false"></asp:TextBox>
<img id="calButton" alt="" title="Show Calendar" src="~/App_Themes/Main/img/calendar.png"
runat="server" height="20" style="cursor: hand;" />
<ajax:CalendarExtender Animated="true" TargetControlID="txtPopEndDate"
runat="server" PopupButtonID="calButton"
Enabled="true" ID="calExtender" Format="dd/MM/yyyy"/>
are the controls I have to select the date, now when I press the ok button I handle the data including the date. However, if I try to fetch the date using calExtender.selectedDate property I get nothing. the same goes for manually getting the string from the textbox and parsin that.
I have to mention that the other data from textboxes and dropdowns on that popup work without a glitch. any help here would be greatly appreciated.

Your textbox has enabled = false. I think this spits out a disabled attribute set to true in the html, therefore the post of the form would not send the value. Why dont you want the textbox enabled?
Im also assuming your trying to grab the value after a postback ? Is that true?

yep,
in the meanwhile I found a solution to the problem. as you said, the textbox apparently needs to be enabled. this means the option for erroneous dates.
I got the value from the textbox and parsed it that way with a DateTime.TryParse to secure a correct date.
if anyone has a better option, let me know

We used Enabled="false" on TextBoxes linked to the CalendarExtender all through our application without a problem. Then I added a date picker to a page which refused to co-operate. The TextBox value was never available on PostBack.
I found the solution here:
http://www.west-wind.com/weblog/posts/2005/Dec/20/ASPNET-20-ReadOnly-behavior-change-when-EnableViewState-is-false
What I found was:
I could set ReadOnly = true, then retrieve the value from the Request on PostBack with TextBox1.text = Request[TextBox1.UniqueID].
I could use TextBox1.Attribute.Add("readonly", "readonly"), then the Text property was set as I expected.
I could not retrieve the value with Enabled = false; or TextBox1.Attribute.Add("disabled", "disabled")
We didn't have ViewState disabled on this page and we didn't get this behaviour on other pages, so I guess this behaviour can be triggered by something else too.

Related

LinkButton not firing ASP.NET validators

I have a form that currently uses an control to submit a form. Everything works perfectly. So now the new requirement is for the "submit' button to be a link. Changing it to a LinkButton control, without changing a SINGLE other thing, breaks the validation.
There is a bit too much code to post in a SO question and I know there's a bit of a lack of detail here, but is there any reason why a LinkButton wouldn't fire ASP.NET validation the same way a Button control would? In theory, they should both operate exactly the same, no?
The current submit button:
<asp:Button ID="btnSubmit" TabIndex="9" Text="Send" ValidationGroup="Forward" runat="server" />
The new submit button:
<asp:LinkButton ID="btnSubmit" TabIndex="9" Text="Send" ValidationGroup="Forward" runat="server" />
The Link button should fires the validation the same way a normal button does, my concerns in your case would be the following:
make sure these is nothing in the server side code stopping this.
make sure in the javascript code there is nothing stopping the "
ASP.NET controls that fire validation has a property called CauseValidation
Be sure all controls should fire validation, has this property set to True
Add attribute CauseValidation="True" to your control but if you want to fire this at particular line at code behind you can use validate the form by the following code:
FormID.Validate();
I know this is old but it has never answered. Did your validator have a "controlTovalidate"? Currently it would appear as if the validator was not firing but in reality it is. It just does not have anything that it is 'watching'. Hope if anyone reaches this thread that this helps even if it is just a little bit.
I was unable to determine the cause of this issue but was able to solve it:
I set the CausesValidation="false" and added at the top of the onclick event this.Validate(linkButton.ValidationGroup) this allows the event to get to the code behind and validation to occur.

Strange behaviour of asp.net button on Firefox of pressing twice

If I have this asp.net button:
<asp:Button ID="Button_Save" Width="150px" OnClick="Button_Save_Click" runat="server" Text="Save" />
In order to prevent the user to press the button twice to insert two data records, I added this in the code behind:
Button_Save.Attributes.Add("onclick", "this.disabled='true';" + ClientScript.GetPostBackEventReference(Button_Save_Data, null) + ";");
It works fine with IE and Chrome. However, in Firefox, every time the user presses the button once, I got two data records inserted.
After some time googling, I modified the button this way by adding: UseSubmitBehavior="false":
<asp:Button ID="Button_Save" Width="150px" UseSubmitBehavior="false" OnClick="Button_Save_Click" runat="server" Text="Save" />
Then it also works with Firefox, only one data record inserted. But then I will have to add that setting for every button on my web application. It requires a lot of work.
However, I think this is really a big problem if that is the case always with asp.net app running on Firefox. Or did I implement something wrong?
Thanks in advance.
You would be better off adding checks to your SAVE code to prevent duplicate POSTs. I've used code like yours and still had duplicate records because someone hit the Refresh button at the wrong time.
EDIT:
Here is the code I use. Key is to match a value on the server with a value coming back in the postback.
In Page_Load:
If Not IsPostBack Then
Session("rcdupdate") = Server.UrlEncode(System.DateTime.Now.ToString)
End If
In Page_Prerender
ViewState("rcdupdate") = Session("rcdupdate")
And in the Save routine
If Session("rcdupdate").ToString = ViewState("rcdupdate").ToString Then
... write the data to the database ...
Session("rcdupdate") = Server.UrlEncode(System.DateTime.Now.ToString)
End If
You can extend asp:button and add your new functionality, then fine/replace all of your asp:buttons. Going forward you can then change all your buttons in once place.

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.

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;

Resources