Focus method not working on asp.net gridview - asp.net

I have a web page with gridview. I received a requirement that the first checkbox on the gridview should have the focus when the page loads. I put the following code:
CheckBox chkBox = (CheckBox)grdMenuPublicHealth.Rows[0].FindControl("chkSelect");
//chkBox.Checked = true;
chkBox.Focus();
I put the Checked method in to make sure the checkbox was being found, and it works. However, the Focus() method doesn't appear to be working. Has anyone run into this before?
Thanks in advance for any guidance.

I found that the following JQuery worked:
<script type="text/javascript">
$(document).ready(function () {
$('input[type=checkbox]:first').focus();
});
</script>

Related

ClientScript.RegisterClientScriptBlock not working

I have a popup in my page which I am trying to show on dropdownlist selected index changed event.
Here is register statement
ClientScript.RegisterClientScriptBlock(GetType(),"id", "ShowApplicationPopUp()",true);
Here is my javascript function
function ShowApplicationPopUp() {
$('#NewCustomerMask').show("slow");
$('#NewCustomerApplicationPopUp').show("slow");
}
Both of my divs are initially hidden by using display:none; statement.
The problem is when my dropdownlist is changed the popup is not seen at all.I tried putting an alert statement to check if the function is called , and the alert statement is fired.Any ideas as to what I am doing wrong.
Any suggestions are welcome.
Thanks.
When you use RegisterClientScriptBlock the Javascript code is inserted early in the page, so it will run before the elements are loaded.
Use RegisterStartupScript instead, which places the code at the end of the form.
I too could not get this code to work but thanks to the above I now have working code. Note, I have a linkbutton inside an Ajax Update Panel.
in my code behind aspx.cs page is:
protected void OpenButton_Click(object s, EventArgs e)
{
// open new window
string httpLink = "../newWindow.aspx";
ScriptManager.RegisterStartupScript(this, GetType(), "script", "openWindow('" + httpLink + "');", true);
}
in my apsx page is first the link to jQuery source, then second the JavaScript for the openWindow function:
<script src="../js/jquery-1.10.1.js" type="text/javascript"></script>
<script type="text/javascript">
function openWindow(url) {
var w = window.open(url, '', 'width=1000,height=1000,toolbar=0,status=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1');
w.focus();
}
</script>
and the link that makes it all happen:
<asp:LinkButton Text="Open New Window" ID="LnkBtn" OnClick="OpenButton_Click" runat="server" EnableViewState="False" BorderStyle="None"></asp:LinkButton>
Im not a jQuery expert and must attribute some of this to the following blog:
https://blog.yaplex.com/asp-net/open-new-window-from-code-behind-in-asp-net/

How To add TextBoxes Data in asp.net using Onblur event

I am creating a ASP.net web application where i need to add different text boxes data and save it in another text box .But using java Script and onblur event. How can we do please say in clear and understanding way because am new to It.
Thanks
You could use jQuery's blur event:
$('#TextBox1').blur(function() {
alert('Handler for .blur() called.');
});
To expand on this and do some calculation/set the value of another textbox we could do something like:
<script type="text/javascript">
$(document).ready(function () {
$("input[id$=TextBox1]").blur(function() {
$("input[id$=TextBox2]").val('Some text')
});
});
</script>

scroll position on Ajax's Timer OnTick event

Inside the one of contentPlaceHolder of the masterpage I'm using Iframe. And In Iframe suppose, IfrmPage.aspx I'm using update panel where timer control is updating the page for each certain duration.
So, what I'm trying to do is settng the focus on textbox after OnTick event. I'm using following line:
ScriptManager1.SetFocus(this.txtMessage.ClientID);
This works correctly. However, Main page or I should say masterpage's scrollbar changes their position which is annoying.
Please share your valuable experience.
thanks.
Have you tried using MaintainScrollPositionOnPostback on the page declaration of IfrmPage.aspx?
After seaching on the google I found the answer.
Now I'm setting the focus to txtMessage(textbox) using javascript and remove this code:
ScriptManager1.SetFocus(this.txtMessage.ClientID);
final answer is here:
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(pageLoaded);
function pageLoaded(sender, args) {
if (args.get_panelsUpdated().length > 0) {
$get('txtMessage').focus();
}
}
</script>

ASP.NET UpdatePanel stopping JQuery from working

I have an update panel on my page with some links that have onClick events that trigger a JQuery box to pop up. This works fine unless an AJAX postback has occurred. I saw some code on another post:
Page.ClientScript.RegisterStartupScript(TypeOf(Page), 'ajaxTrigger1', 'Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);', true);
Page.ClientScript.RegisterClientScriptBlock(TypeOf(Page), 'EndRequest1', 'function EndRequestHandler(sender, args){AsyncDone();}', true);
which I have used but it doesn't seem to be working. I keep getting AsyncDone is not defined. Does anyone have any ideas as to what I can do to sort the issue out please?
Details: ASP.NET 2, IIS7
Thanks in advance.
The problem is that when the updatepanel fires, it replaces a chunk of html in the dom, which means it replaces the elements that you have bound the click event to.
To bypass this look at jquery .live() or .delegate() which keeps looking for events matching the selector you provide, or if you want to bind to content every time the update panel refreshes content look that the jQuery updatepanel plug-in.
I sorted this by not using the document.ready in jQuery. Instead I used:
function pageLoad(sender, args) { //code }
I haven't seen any adverse effects yet so hopefully this will sort my issue.
I used update panel just only to stop the whole page refresh in asp.net which even included the jquery.But after using the update panel somewhat the problem was solved but created another problem. jquery script was not working inside the updatepanel.
This issue is now solved.
Example: In case of jquery Date picker, Usual call of Jquery script appears like this :
<script type="text/javascript">
$(function() {
$("#datepickers").datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true
});
});
</script>
Even I had used the same code as
Page.ClientScript.RegisterStartupScript(TypeOf(Page), 'ajaxTrigger1',
'Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);', true);
Page.ClientScript.RegisterClientScriptBlock(TypeOf(Page), 'EndRequest1', 'function EndRequestHandler(sender, args){AsyncDone();}', true);
Solution Appears here: http://kopila.com.np/
But it didnt worked for me.Later I came to know that The jquery script wont work after partial postback i.e.our ajax request.If we try to call any JQuery script inside the update panel do whatever script wont work after the first ajax request.
Example: When you are using asp.net update panel call it again as follows:
<script type="text/javascript">
function pageLoad(sender, args) {
if (args.get_isPartialLoad()) {
$("#datepickers").datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true
});
}
}
</script>
For More Details Go to: http://kopila.com.np

asp.net generating onclick event automatically for button

ASP.NET seems to be generating its own onclick event for any buttons that are generated.
Looks like this
javascript:__doPostBack(
This is preventing jQuery from working correctly.
Does anyone know how to stop asp.net engine from doing this?
Many thanks
You just need to add a return false to your jquery code that handles the button.
$("myaspbutton").click(function(e){
//your code
return false;****
});
<asp:Buttton runat="server" ID="myButton" Text="MyButton" OnClientClick="alert('hi');" />
Update:
you can bind the click event dynamically:
$('#<%=myButton.ClientID%>').click(function(e) {
//your code here
e.preventDefault();
});

Resources