html editor cursor focus problem - javascript

I have a page with textbox and an html editor. When the page is loaded the cursor is focussing on the html editor. instead i want to focus the cursor on the textbox how can i do that.

I got the answer by setting the Autofocus property to false of the html editor

You have a couple of options here. You can use JavaScript:
<body onload="javascript:document.Form1.TextBox1.focus();">
or, if you are using a Form, then you can do the following:
<form id="Form1" defaultfocus="textbox" runat="server">
<asp:textbox id="textbox1" runat="server"/>
<asp:textbox id="textbox2" runat="server"/>
<asp:button id="button1" text="Button1" runat="server"/>
</form>

If you plan on using mroe Javascript based stuff, may I recommend jQuery?
http://www.jquery.com/
and use:
$(document).ready(function() {
$('#formID .textboxClass').focus();
});

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.

do postback after enter hit on changing text in textbox

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.

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

Why does .NET render javascript for a button when there's a custom validator on the page?

I've got two questions - first of all, why does .net render a javascript onclick event for asp buttons when there's a custom validator on the same page, and secondly, how can I get rid of the javascript?
It works fine when javascript is turned off, so I don't know what the point of it is. Here's a mini example:
<form id="form1" runat="server">
<asp:Button ID="Button1" OnClick="Button1_Click" Text="test" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
OnServerValidate="Stuff_Validate" EnableClientScript="false">
</asp:CustomValidator>
</form>
This will generate the following html for the button:
<input type="submit" name="Button1" value="test" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />
Without the custom validator, it's:
<input type="submit" name="Button1" value="test" id="Button1" />
Any ideas?
Thanks,
Annelie
If you look at what System.Web.UI.WebControls.Button does you can see why:
In GetPostBackOptions(), if you have any validators on the page (in the current validation group), then it will generate a script for the postback.
Then in AddAttributesToRender(), if any scripts are present, they are rendered out in the onclick event.
If you need to get around this, then you are going to have to build a custom Button
The javascript is added because the custom validation is done via javascript. It is meant to be there. How do you think the validation is done? If you got rid of the javascript the validation would not work.

Categories