ASP.NET Ajax Find Component Problem - asp.net

I've got a page where I have a ModalPopUpExtender which I want to show from code.
This is my site structure which is a web form within a nested masterpage:
...
<asp:Content ID="con" ContentPlaceHolderID="mainContent" runat="server">
<asp:MultiView ID="tabMultiView" runat="server">
<asp:View ID="generalTab" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server">
</asp:ScriptManager>
<ajaxToolkit:ModalPopupExtender ID="newAddressModalPopup" CancelControlID="newAddressDialogCancelButton"
BackgroundCssClass="modalBackground" TargetControlID="newAddressLink" PopupControlID="newAddressDialogDiv"
runat="server">
</ajaxToolkit:ModalPopupExtender>
...
open dialog
<script type="text/javascript">
function openNewAddressDialog() {
$find('<%= newAddressModalPopup.ClientID %>').show();
}
</script>
...
The find method always returns null. I also tried findComponent, etc. It's always null. When I debugged the method I noticed that the components collection (which is kind of a dictionary with the control ID as key) is empty.
What could the problem be? BTW, I am using jQuery stuff on the page as well.
Thanks a lot!

Ok I found it. The TargetControl was not rendered in HTML because it was in another view.

In your JavaScript function try using document.getElementById("ctl00_ContentPlaceHolder1_newAddressModalPopup"). Maybe it works. Let me know if you get issues.
Or try Setting BehaviorID="someid" to the modal popupextender and use this JavaScript code:
function changeValue()
{
var myBehavior = $find("myBehavior1");
myBehavior.show();
}
Or:
var modalDialog = $find("newAddressModalPopup");
// Get reference to modal popup using the Ajax API $find() function.
if (modalDialog != null) {
modalDialog.show();
}

Related

clientidmode static is not working with updatepanel which forces full postback

I have RadioButtonList & Listvie in my page. I am using update panel to avoid postback Now my radioButtonList works as filter for listview. My Problem is in my radiobuttonList I have to use clientIDmode=static but if I do that then my updatepanel has no use since there is full postback when radiobuttonList gets changed. How to solve this problem without removing clientIdmode=static. I seen some solution for kind of same post but really didn't understand. Please help me.
My code has following structure.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="areasList" CssClass="mark" AutoPostBack="true" runat="server" ClientIDMode="static" RepeatLayout="Flow">
</asp:RadioButtonList>
ListviewHere
</ContentTemplate>
</asp:UpdatePanel>
There is an issue with your script.
You script is not being called after postback.
So use as below,
<script type="text/javascript">
// below will execute after ajax postback
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
//script
}
// executes after page load first time
//script
</script>
You can set the ClientIDMode of the RadioButtonList to AutoID (or not specify the attribute in the markup if that is the default value) and use a binding expression in client code to get the actual ID of the control:
$("#<%= areasList.ClientID %>")
or
document.getElementById('<%= areasList.ClientID %>')

Asp.net Page moves down after load

I've noticed that after using <asp:panel> or <asp:UpdatePanel> my page is jumping/scrolling down after the page has finished rendering. Also after a asynchronous post back, the page moves to the same place instead of staying right where it is. I don't have any focus set in the code behind of my page.
It's probably worth mentioning that on page load I am updating some asp.net controls within the <asp:UpdatePanel>. Mostly database driven dropdowns.
I'm wondering if anyone knows of any reason why a page would jump down. Is there some default focus or something that happens to certain asp.net controls?
Thanks in advance for your help,
Things I've Tried:
How to keep whole page scroll position after asynchronous postback
Update1:
I've used David Stratton script to get the correct position. However the page still jumps down then jumps back to the correct position. My goal is to have the page not jump at all.
This is the mark-up of my page, there are control between these.
<asp:UpdatePanel ID="UpdatePanel9" runat="server">
<asp:Panel ID="tagpanel" runat="server" DefaultButton="btnAddTag">
</asp:Panel>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel5" runat="server">
<asp:Panel ID="pnlIncidentInfo" runat="server">
</asp:Panel>
</asp:UpdatePanel>
Note The issue described here is NOT related to the MaintainScrollPositionOnPostback. I've had this issue myself and it's related to the UpdatePanel control.
From
http://weblogs.asp.net/andrewfrederick/archive/2008/03/04/maintain-scroll-position-after-asynchronous-postback.aspx
First, set up a div just inside the ContentTemplate named "scrollDiv".
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="scrollDiv">
The rest of your page content goes here.
</div>
</ContentTemplate>
<asp:UpdatePanel>
and then the JavaScript, as the article says, goes after the ScriptManager on your page
<script type="text/javascript">
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
xPos = $get('scrollDiv').scrollLeft;
yPos = $get('scrollDiv').scrollTop;
}
function EndRequestHandler(sender, args) {
$get('scrollDiv').scrollLeft = xPos;
$get('scrollDiv').scrollTop = yPos;
}
</script>
Perhaps you're using MaintainScrollPositionOnPostBack?

Telerik RadWindowManager

I use Telerik radWindowManager to display alert windows.
I have code like this:
<telerik:RadWindowManager ID="window1" runat="server" ReloadOnShow="true"
EnableEmbeddedBaseStylesheet="false" EnableEmbeddedSkins="false" OnClientClose="javascript:alert('test')">
...
</telerik:RadWindowManager>
The problem is when I use this code without "OnClientClose" it works fine but when I add "OnClientClose" the alert is not displayed.
Thanks.
The OnClientClose attribute takes pointer to a JS method, not a call.
Which is why Nima M's solution works
From the Telerik website, RadControls for ASP.NET AJAX > Documentation > Client-Side Events:
The RadWindow control has a number of properties whose value is the name of a javascript function that executes when specific client-side events occur.
That is, not inline JavaScript. The equivalent properties on the RadWindowManager class work the same way.
You can probably write a separate method to handle OnClientClose:
<telerik:RadWindowManager ID="window1" runat="server" ReloadOnShow="true"
EnableEmbeddedBaseStylesheet="false" EnableEmbeddedSkins="false" OnClientClose="OnClientCloseHandler">
...
</telerik:RadWindowManager>
<telerik:RadCodeBlock ID="rcbModal" runat="server">
<script language="javascript" type="text/javascript">
function OnClientCloseHandler(radWindow) {
//Do Domething Here
}
</script>
</telerik:RadCodeBlock>

Prevent ASP.net __doPostback() from jQuery submit() within UpdatePanel

I'm trying to stop postback on form submit if my custom jQuery validation returns false.
Is there any way to prevent the __doPostback() function finishing from within the submit() function?
I'd assumed:
$('#aspnetForm').submit(function () { return false; });
would do the trick, but apparently that's not the case: does anyone have a suggestion?
The submit() function does block the postback (it won't postback if you pause at a breakpoint in firebug), but I can't seem to stop the event happening after the submit() function is complete!
Cheers, Ed
EDIT
OK, I had a quick mess about and discovered that the fact that the button I'm using to cause the postback is tied to an updatepanel as an asyncpostbacktrigger seems to be the problem: If I remove it as a trigger (i.e. cause it to product a full postback), the is no problem preventing the postback with return false;
Any ideas why the async postback would not be stoppable using return false?
You have to use the client side PageRequestManager to properly handle AJAX submit events. (e.g. prevent an async postback.)
If you are not in total control of the page, there can be JavaScript on the page that just calls __doPostBack() without going through any page logic.
In this case - in addition to the above -, you have to store the old window.__doPostBack() and provide your own - as #tucaz mentioned in his comments.
(...and as you mentioned, it can get quite perplexing with chaining.)
For regular submits (non-AJAX), you can provide an event handler as others have pointed out.
This page might be of help and has some code samples that use PageRequestManager. In particular:
initialize : function()
{
...
this._onSubmitHandler = Function.createDelegate(this, this._onSubmit);
this._onPartialUpdateEnd = Function.createDelegate(this, this._onUpdateEnd);
if (typeof(Sys.WebForms)!== "undefined" && typeof(Sys.WebForms.PageRequestManager)!== "undefined")
{
Array.add(Sys.WebForms.PageRequestManager.getInstance()._onSubmitStatements, this._onSubmitHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._onPartialUpdateEnd);
}
else
$addHandler(document.forms[0], "submit", this._onSubmitHandler);
},
Edit:
Following the above, this, for example works fine for me (.Net 3.5 SP1, Button1 is trigger in the updatepanel, etc...):
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.js" charset="utf-8"
type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
</form>
<script type="text/javascript">
(function($, undefined) {
var submitHandler = function(e) {
return false;
}
if (typeof (Sys.WebForms) !== "undefined" && typeof (Sys.WebForms.PageRequestManager) !== "undefined") {
Array.add(Sys.WebForms.PageRequestManager.getInstance()._onSubmitStatements, submitHandler);
} else {
$("form").submit(submitHandler);
}
})(jQuery);
</script>
</body>
</html>
$('#yourPostButton').click(function(){
return false;
});
This should do!

jQuery Click event on asp:button

I have a server side button as
<asp:Button ID="btnSummary" runat="server" OnClick="btnSummary_Click" Text="Next" />
I want to attach the jQuery Click event using its ID and NOT using the alternative class attribute way.
I tried to attach the click event as:
$("#btnSummary").click(function()
{
alert("1");
});
But, its click event is not fired. Also, I have also tried $("id[$btnSummary]").
Is there any way to attach the click event on asp:button using jQuery without the class attribute on the button?
Some of the options you have can be found here
How to stop ASP.NET from changing ids in order to use jQuery
EDIT:
After reading your comments on other answers, it sounds like you need to bind the onclick event handler inside of ASP.NET AJAX's pageLoad, so that it is bound on every postback, including asynchronous postbacks. On the other hand, $(document).ready() binds only once on initial page load
function pageLoad(sender, args)
{
$("#<%=btnSummary.ClientID %>").click(function()
{
alert("1");
});
}
Dave Ward wrote a nice article on the difference between pageLoad and $(document).ready().
Add ClientIDMode="Static" to your asp:button, something like this:
<asp:Button ID="Button1" runat="server" ClientIDMode="Static" Text="Button" />
This will make the ID remain the same. It disables the autogenerated names for this control.
Here is the reference for this: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
Check if the id attribute is in the html source when you run the site.
Do you have the click function inside a document ready function ?
-
$(document).ready(function() {
// put all your jQuery goodness in here.
});
EDIT:
Since its a server side control and the ID changes, you will need to dynamically update the javascript on every page load with the variable.
ASP.NET generates a UniqueID for client-side stuff, you can use that to bind the event. That ID is generated based on the position of that Control inside a ControlCollection and different INamingContainers, it's ugly and you can't guess it...
Add this kind of code somewhere on your page to hook up that button.
<script type="text/javascript">
$(function() { $("#<%=btnSummary.ClientID%>") }).click(function(){/*...*/});
</script>
I'm little confused here now.
Let me explain:
1. I added a button on the page:
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<div>
2. Then I added a JavaScript and jQuery:
<script type="text/javascript">
$(document).ready(function() {
$("#Button1").click(function() {
alert("Hello world!");
});
});
</script>
3. The generated html is this:
<div>
<input type="submit" name="Button1" value="Button" id="Button1" />
<div>
Now, I don't see ASP.NET (asp.net 3.5) changing the ids. Why do I see
different behavior?
Btw. This does work when I hit the button!
Thanks.
Assigning the selector to the class worked for me.
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="My Button" CssClass="myButton"/>
<script>
jQuery(".myButton").click(function () {
alert("clicked");
});
</script>
Please use the following syntax : $("[id*=Button1]") to reference any asp control
use pageLoad when using updatepanels because document.ready only runs once on initialization of the page and loses its binding on partial postbacks.
PageLoad gets run everytime so will rebind every partial postback.
I had the same problem, this works for me
<asp:Button ID="Button1" runat="server" Text="ASP Button" OnClientClick="return false;" />
Its the addition of return false that seems to make the difference. Hope this helps.
regards
Peter
ASP.NET adds to you id (example: id "selectButton" becomes
"ctl00_middleContent_gvPeople_ctl04_selectButton");
Use the jquery syntax to search for the part of the id that doesn't change.
$("[id='_selectButton']")
Please try below:
document.getElementById("<%=button1.ClientID %>").click();
Wrap this <asp:Button/> inside a html <div> container.
<div id="testG">
<asp:Button ID="btn1" runat="server" Text="TestButton" />
</div>
$(document).ready(function () {
$("#testG").click(function () {
alert("1");
//your code
});
});
You have reached there. There are two ways i guess,
Either inspect the control from Browser and See the Control ID, looks like ct1000_ btnSummary. use this ID on Click event of jQuery.
replace code
$("#ctl00_contentplcedholder_btnSummary").click(function()
{
alert("1");
});
Next one is quite easy just add an attribute ClientIdMode="static" to your control and use your actual Jquery click event it will fire.

Resources