do postback after enter hit on changing text in textbox - javascript

How can I make postback after user changes textbox text and hits enter ?
Is only javascript real solution. Doesnt asp.net give any out from box solution for such a problem ?
Thank You for any hints.

Beside the javascript solutions, you can place the textbox with a submit button inside a panel and make use of DefaultButton property of the panel.
Below is the code example: -
Your aspx will look like this
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
Now, when you press enter inside the text box the button click event occurs, same as by clicking the button.

Related

set focus on different textboxes as different div is visible on a web page

I've a page where on page load event I've set focus on a textbox using javascript. But after button press I've to set focus on different text box of different asp:Panel. Is this possible using javascript? Currently I am doing this via code behind page.
<script type="text/javascript">
$(function () {
document.getElementById('<%=txtPassengerId.ClientID%>').focus();
});
</script>
<asp:Panel ID = "firstPanel" >
<asp:TextBox ID="txtPassengerId" runat="server"/>
<asp:Button ID="btn_search" runat="server" Text="Search" OnClick="btn_search_Click" />
</asp:Panel>
<asp:Panel ID = "secondPanel" >
<asp:TextBox ID="txtDestination" runat="server"/>
</asp:Panel>
After the button pressed secondPanel is visible and I need to focus on txtDestination text box. Is this possible using javascript?
Yes, you have to use the selector you've used "document.getElementById" and use the .focus() function.
document.getElementById("secondpanel").style.visibility = "visible";
document.getElementById("txtDestination").focus();
should do it. if you need more just comment.

Pass asp.net controls as params to javascript function

I am trying to get a javascript function to work properly with my asp.net controls. I have two radio buttons and three textboxes.
When the first radio button is clicked I would like to disable the third textbox and when the second radio button is clicked I would like to disable the third text box. See the attachment for a screenshot.
I have tested that the function works by adding "OnClick" to the first radio button and setting AN ALERT WHICH IS WORKING.
However, I am not sure how to pass the controls in to the function to have access to them. Anyone know how to do this?
Screenshot
ASP.NET
<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="DateTimeQuery" OnClick="TimeRangeClickEvent()"/>
<asp:TextBox ID="startdatetext" Enabled="true" runat="server" MaxLength="10"></asp:TextBox>
<asp:TextBox ID="enddatetext" Enabled="true" runat="server" MaxLength="10"></asp:TextBox>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="DateTimeQuery" OnClick="TimeRangeClickEvent() />
<asp:TextBox ID="withinthepast" Enabled="true" runat="server" Text="1"></asp:TextBox>
Javascript
<script>
function TimeRangeClickEvent(How do I pass asp.net controls here)
{
alert('Working');
}
</script>
You should use ClientID control properties to get IDs of HTML elements and then work with them from Javascript. More info.
In Javascript:
var radio1 = document.getElementById('<%=RadioButton1.ClientID%>');
When you have all your HTML elements you just need to handle necessary change/clicked/selected events in javascript to make it work.

Text attribute of an asp label using JavaScript is not staying changed

I have a button:
<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick()"/>
that runs the following JavaScript function when it gets clicked:
function ResetClick() {
document.getElementById("<%=lblError.ClientId%>").innerHTML = "";
}
That changes the text on this label:
<asp:Label ID="lblError" runat="server" Style="color: Red;" Text=""></asp:Label>
Unfortunately, my code behind also changes the label text to an error message using the following code:
lblError.Text = "No data is available"
When I click the reset button, the text is momentarily cleared and then it fills again with the error message. Is there any way to have the code behind set a message in the label and let the JavaScript clear the message?
If you do not want the button to post back then you should add return false; to the OnClientClick attribute:
<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick(); return false;"/>
PS: adding as an answer since it sounds like it was helpful.

How to disable submit button which caused post back on ASP.NET UpdatePanel

I have an UpdatePanel inside page with couple of buttons. Each button cause PostBack event. But I found that user can click multiple times in button and crash application. I want ot disable all buttons on first click until postback will be handled and new content of UpdatePanel arrived.
I tried to bind (via jquery) to 'click' event of button, set 'disabled; attribute and call 'submit' on parent form. Unfortunately is seems that this method not working - content of UpdatePanel still old.
After that I tried to override 'submit' event but faced another issue - how to submit form again if I came to my handler each time? It may be fixed with introducing flag variable, but I dislike it - another variable just increase code entropy.
Maybe I can do it somehow with ASP.NET methods?
Apply ajax UpdateProgress and put your code in UpdatePanel as to disable Button after first click:
<aspAtlas:UpdateProgress ID="upUpdatePanelProgress" runat="server" DisplayAfter="0"
DynamicLayout="true" AssociatedUpdatePanelID="updpnlProgram">
<ProgressTemplate>
<asp:Panel ID="Panel1" CssClass="overlay" runat="server">
<img runat="server" id="img1" src="../Images/UpdateProgress.gif" alt="Loading..." /><br />
<br />
<b>Loading...</b>
</asp:Panel>
</ProgressTemplate>
</aspAtlas:UpdateProgress>
<aspAtlas:UpdatePanel runat="server" ID="updpnlProgram" UpdateMode="Conditional">
<ContentTemplate>
<div>
//--Your code in this div
</div>
</ContentTemplate>
<Triggers>
<aspAtlas:PostBackTrigger ControlID="Button_ID" />
</Triggers>
</aspAtlas:UpdatePanel>

How can i check a radio button by clicking on asp text field using javascript

Hi
While working on asp, i want if i click on my asp text field, a radio button should be checked, but i dont find any onClick event available for asp text field, and if i use onchange, it is from the server end, i want this situation without page refresh, just like we do in html fields using javascript, please help!!!
Try this one:
HTML:
<asp:TextBox ID="txtSelect" runat="server" Width="200px" Text="Click here"> </asp:TextBox>
<br />
<asp:RadioButton ID="radCheck" runat="server" />
JQUERY:
$("#txtSelect").click(function(){
$("#radCheck").attr("checked",true);
});
OnFocus is probably the event I would use.
You can use Ajax if you don't wanna refresh the page

Categories