jQuery not working on Popup Window - asp.net

I am using ASP.Net and jQuery + jQuery UI. Everything works fine with the jQuery on any other page, however when I create a popup window with window.open(...) jQuery seems to no longer function.
I have all of the script files included on the Popup's Master page, so am not sure why it won't fire.
Any Thoughts?

I am not sure if you use window.open you are creating a page class that inherits from the master page.
So JQuery is probabaly not included in that pop up. In addition popups aren't nice. people download all kind of things so they don't open. but it can be done like this:
protected void Button1_Click(object sender, EventArgs e)
{
string queryString =
"http://localhost:39208/TreeView.aspx?param1="
+ TextBox1.Text.Trim();
string newWin =
"window.open('" + queryString + "');";
ClientScript.RegisterStartupScript
(this.GetType(), "pop", newWin, true);
}
ClientScript.RegisterStartupScript will bring your jquery in the pop up

Related

Passing data from a aspx page to a aspx popup

Following is the scenario I am facing, I have a aspx page in which I have added my ascx user control, I also have a <a href control which call a js function to open a aspx popup.
when I open the popup, I need to send the data present in the ascx control to my popup. can you please guide me what method can I use to achieve this other than using session, as the data can be updated in many different places hence maintaining in session will be difficult.
Thanks
try with these syntaxe ( RegisterStartupScript + window.open)
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "newWindow", "window.open('Test.aspx?ID=" + _cId + "','_blank','status=1,toolbar=0,menubar=0,location=1,scrollbars=1,resizable=1,width=30,height=30);", false);
You said that you are using a js function to open the aspx popup.
then it is simple.
1. Read the Data from the controls of the User control by using javascript
var variable1 = document.getElementByID("ControlId").value;
var variable2 = document.getElementByID("ControlId").value;
2. Pass this data as query string to the next page
window.open("http://www.w3schools.com?id=" + variable1 + "&name=" + variable2);
You can read this data from querystring from the next page
If you cant sent data as querystring , can try some other ways
1. Try to post the form to the other page using target="_blank".
we can dynamically change the form action if needed.
OR
2. Make use of the window.opener object from the popup to read the data from controls the opener page.
var variable1 = window.opener.getElementById("ControlId").value
Create a hidden variable in your ascx.
<asp:HiddenField runat="server" ID="hdnId" />
On page load of ascx set the value to be sent to the hidden variable
protected void Page_Load(object sender, EventArgs e){
hdnId.Value = <value to be sent to pop-up>;
}
Register a ajax manager in the code behind.
protected override void OnInit(EventArgs e)
{
RadAjaxManager.GetCurrent(this.Page).ClientEvents.OnRequestStart = "ajaxMngr_RequestStart;";
base.OnInit(e);
}
In the ajaxMngr_RequestStart() JS
function ajaxMngr_SA_RequestStart(sender, args) {
var oPatId = "<%=hdnSendPatId.Value %>";
//add code to open the pop-up, add oPatId as part of the URL
}
}
I use Telerik, which makes helps a lot in managing pop-ups. Let me know, if this helps.
Cheers!

link and post user input to another pg in new window

I have a page in my site that has allows users to search for different items, and when they choose the item, the "link".text next to it changes to how many times that item has been loaned, and when they click on it, I want a new window to pop up and display this history. My problem is the item they chose from the first page isn't posting to the next page.
I've tried many different things. The asp:linkbutton does everything I want, except it won't open the link up in a new window! There's no target attribute for that element, like there is for and tags. I tried adding OnClickEvent="'open.window(history.aspx')" instead, but user input isn't posted. Seems like MS deliberately took out the 'target' attribute for controls that were meant to post back to your web site >_
I know there are work arounds to my approach, such as session variables, but does anyone have any ideas on how to do specifically what I'm trying to do?
You could try wiring up the OnClick event of the LinkButton and in the event handler do something like:
protected void LinkButton1_Click(object sender, System.EventArgs e)
{
// Do your server-side stuff here getting the new window arguments.
string windowArgs = "";
string newWindowUrl = "Page2.aspx?WindowArgs=" + windowArgs;
string javaScript =
"<script type='text/javascript'>" +
"window.open('" + newWindowUrl + "');" +
"</script>";
this.RegisterStartupScript("", javaScript);
}
hope it helps!

ASP.NET and jQuery - call from codebehind

This is a problem i've tried so solve before but gived up.
Basicly i'm using ModalPopupExtenders (from AJAX.NET) to display Panels with a few content (text, controls, etc). And i'm calling it from codebehind. And it works well.
But now i want to replace the ModalPopup with some jQuery dialog box. The problem is calling it from codebehind.
As far as i know, i have to register the jQuery libraries on RegisterStartup event, but i've tried it and call the jQuery from codebehind but with no sucess.
Can someone help me? I really want to replace the ModalPopup, they gives me to much trouble.
Thanks in advance.
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "registerDialog",
"$(function() { $('#dialog').dialog({autoOpen:false, show:'blind'}); });", true);
}
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "openDialog",
"$('#dialog').dialog('open');", true);
}
Is this the correct way? I've to register it first to stay hidden.
Thanksю
If you're using a ScriptManager, use RegisterStartupScript(), like this:
ScriptManager.RegisterStartupScript(this, GetType(), "modalscript",
"$(function() { $('#dialog').dialog(); });", true);
If you're not using a ScriptManager/UpdatePanels, use the equivalent ClientScriptManager version.
It's important to remember to wrap your code in a document.ready handler (IE has the most issues without it), so your elements (in my example, id="dialog") are in the DOM and ready.
You do not actually call jQuery from code behind, you just write some extra javascript code that's run on page load (after the PostBack).
In this start up code you make the jQuery calls.

ASP.NET Button to update properties and show jQuery Modal

I have the following ASP.NET code:
<div id="panelIssue" runat="server" style="width: 450px; height: 320px;">
<gsl:IssueUC ID="ucIssue" runat="server"
OnItemSaved="ucIssue_ItemSaved"
OnItemCancelled="ucIssue_ItemCancelled" />
</div>
and then have an asp:Button on the page that simply call some methods and set some properties of the custom user control like
protected void btnNewIssue_Click(object sender, EventArgs e) {
ucIssue.ChangePanelMode(PanelModeEnum.Add);
ucIssue.FirmID = Convert.ToInt32(Page.Session["FirmID"]);
ucIssue.loadObject();
}
I know that I can use the div to show a jquery modal dialog but the question is how to set the usercontrol properties and call their methods?
I can evaluate also a different approach (e.g. changing the usercontrol).
Thanks in advance
Lorenzo
What I can suggest is that in the click event, after setting the control properties, you can emit a piece of javascript that will create and display a modal when the page finishes loading upon reaching the client.
Try something along the lines of the following snippet (not tested, may need small tweaking to work, but I hope you get the big idea):
protected void btnNewIssue_Click(object sender, EventArgs e) {
ucIssue.ChangePanelMode(PanelModeEnum.Add);
ucIssue.FirmID = Convert.ToInt32(Page.Session["FirmID"]);
ucIssue.loadObject();
//emit client script that will create and show the modal dialog
ClientScriptManager clientScriptManager = Page.ClientScript;
string scriptText = "$(document).ready(\n" +
"function() {\n" +
"$('#panelIssue').dialog({\n" +
"autoOpen: false,\n" +
"modal: true,\n" +
"width: 450,\n" +
"height: 320,\n" +
"title: \"Some title\",\n" +
"resizable: false,\n" +
"draggable: false });\n" +
"$('#panelIssue').dialog('open');\n" +
"});";
clientScriptManager.RegisterClientScriptBlock(this.GetType(), "MyScript", scriptText, true);
}
You should also look at the capabilities of the dialog creation in jQuery to see what other settings will fit your scenario better. I just used a few of the params that I have in an example of mine.

ASP.NET WebForms + Postback then open popup

I have a LinkButton that has to postback to perform some logic.
Once it is finished, instead of loading the page back up in the browser, I want to leave it alone and pop open a new window.
So far, the best idea I've had is to put the LinkButton in an UpdatePanel, and have it render some JavaScript out when it reloads, yet I think that is totally hacky. Also, if I recall right, JavaScript within a update panel won't run anyways.
Any other ideas?
Use LinkButton.PostBackUrl to set a different page to POST to, and some client script to get a new window (and the old target restored so that future postbacks work normally). The 2nd page can use PreviousPage to get access to any needed state from the original page.
<script runat="server">
void lnk_Click(object sender, EventArgs e) {
// Do work
}
</script>
<script type="text/javascript">
var oldTarget, oldAction;
function newWindowClick(target) {
var form = document.forms[0];
oldTarget = form.target;
oldAction = form.action;
form.target = target;
window.setTimeout(
"document.forms[0].target=oldTarget;"
+ "document.forms[0].action=oldAction;",
200
);
}
</script>
<asp:LinkButton runat="server" PostBackUrl="Details.aspx" Text="Click Me"
OnClick="lnk_Click"
OnClientClick="newWindowClick('details');" />
Here is the code:
protected void Button1_Click(object sender, EventArgs e)
{
// Do some server side work
string script = "window.open('http://www.yahoo.com','Yahoo')";
if (!ClientScript.IsClientScriptBlockRegistered("NewWindow"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(),"NewWindow",script, true);
}
}
One thing you could try is to have your LinkButton OnClick event do its processing, then register a Page.ClientScript.RegisterStartupScript with the popup code, which will put some Javascript into the tag to fire off after the page loads. This should launch your new window after the processing completes.
EDIT: Reading your comment, I believe you can still use this approach, have your results stored in a session variable, and then have the popup page pull the results from there.

Resources