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

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');");

Related

Adding on change event to dropdownlist declared with a select class

Issue adding on change event to a control.
I currently have a page that uses this in the aspx page to declare a dropdownlist
<select class="dropdownPhase" />
I am trying to add a on change event to the above dropdownlist something like what i did on another page. the problem is i cannot change it to be declared like a normal dropdownlist is it possible to add the onchange event?
<asp:DropDownList ID="dropdownPhase" onchange="ApplicationRecording.prototype.onPhaseChange1(this,$(this).closest('tr'));" />
You can add the client-side event to the DropDownList in code:
dropdownPhase.Attributes["onChange"] = "someJavaScriptFunction();";
You might have to set AutoPostBack to False.
Hope this helps.

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 manually set button input type in ASP.NET?

I have a jQueryUI dialog with some textboxes, plus a button. Right now, the asp:Button tag used to generate the button automatically sets its type as type="submit". The structure of the dialog is such that pressing enter at any of the textboxes should not call the button click event. It seems like the cleanest way to solve the problem, if it is doable, is to manually set the button's type to something other than submit. Is there a way to do this?
Edit: Forgot to mention this, but the button text is bound to a database item by <%# Eval() %>, which doesn't work without a server-side tag.
"
UseSubmitBehavior="false"
OnClientClick="return" />
The UseSubmitBehavior property tells ASP.NET to render as type="button" and inserts a __doPostBack() function call into the onclick property. The text of the OnClientClick property gets prepended to the client-side onclick event (with a trailing semicolon if you don't include it yourself), therefore putting return simply short-circuits the event handler and ends up doing nothing at all.
You can use just a regular html button and set it to whatever you want. It doesn't have to be a serverside asp button.
In your aspx file:
<input type="button" value="My Button" onclick="DoSomeScript();" />
I don't think you can set the Button control's AutoPostBack to false, which would normally do what you want.
In this case, I would simply not use asp:Button and just use <input type='button'>.

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;

ASP.NET CheckBox disabling postback with javascript

I'm trying to wire up a CheckBox to handle an event when check/unchecked. If the user has JavaScript enabled, use that, otherwise use a postback.
Here is my code:
<asp:CheckBox ID="ApplicationInProcessCheckBox" runat="server"
Text="Application In Process" AutoPostBack="true"
oncheckedchanged="ApplicationInProcessCheckBox_CheckedChanged"
onclick="return false;" />
The return false in the javascript onclick event is disabling the postback. However, it also won't let the box check or uncheck. (I have more code to add to the javascript event... I just want to get the concept working first).
What am I doing wrong?
I think we can't post back on clicking checkbox without Javascript enabled.

Resources