ASP.NET JavaScript popup window failing at getting a property - c#

In my ASP.NET application, my popup window is returning null for a specific variable.
When I click a button this JavaScript gets fired:
function Picker_OrderDefaultFirst() {
OpenPageWithParam('OrderPermanentItems', '?ID=' + "<%=CommitteeUId %>" + "&btn=1", returnOrderPicker);
return false;
}
returnOrderPicker looks like:
function returnOrderPicker() {
alert("Test fra returnOrderPicker");
}
OpenPageWithParam looks like:
function OpenPageWithParam(PageName, Query, ReturnFunction) {
var _window = null;
InitBlocker();
switch (PageName) {
case "OrderPermanentItems":
_window = PopupCenter(ROOT_DIR + "/Forms/Pickers/PermanentItemsPicker.aspx" + Query, PageName, 590, 600);
break;
}
_window.ReturnFunction = ReturnFunction;
return _window;
}
I'm setting the popup window's property ReturnFunction to be ReturnFunction of data from previous page. (In this example returnOrderPicker Function).
But, when window pops up, the value of window.ReturnValue is undefined.
Calling in popup window looks like:
<script type="text/javascript" language="javascript">
window.onload = function (sender, eventArgs) { alert(window.ReturnFunction + " Window PermanentItemPickerPopup "); }
</script>
Any ideas?

Related

How to send a Asp label value( from server side) from popup window to parent window dropdownlist

Trying to send label value from popupwindow to parent window drop down list
Create a global variable that you pass this value in it
Create Global.asax file, In it create public static string X;
Go to your page access the dropdown value save it in this X by calling Global File
Then Restore in in your Main Page
I got it by using, (Changed to client side)
popup window code:
<script type="text/javascript">
function updateParent(test) {
var oVal = test;
window.opener.updateParent(oVal);
window.top.close();
}
</script>
Parent window code:
function updateParent(oVal) {
var region = oVal;
$('#ddlreg option:contains(' + region + ')').each(function () {
if ($(this).text() == region) {
$(this).attr('selected', 'selected');
return false;
}
return true;
});
}

How to pass value from parent asp.net dropdownlist to textbox in popup using javascript

Hello i am failing to pass the value from dropdownlist in the parent aspx form to textbox in the child aspx form
Parent javascript
: The First script is to open the popup window
<script type="text/javascript">
var popup;
function NewCustOpn() {
popup = window.open("NewCustomer.aspx","Popup",toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=0,width=520,height=350,left = 250,top = 50");
}
</script>
This is the second script on the parent page to get the value of the dropdownlist
<script type = "text/javascript">
function parentFunc()
{
return document.getElementById ("<%=DropDownList1.ClientID%>").value;
}
</script>
The child page javascript:
<script type = "text/javascript">
window.onload = function ()
{
if(window.opener != null && !window.opener.closed)
{
var val = window.opener.parentFunc();
var textbox = document.getElementById("<%=TextBox1.ClientID%>");
textbox.Value = val;
}
}
</script>
When the popup opens TextBox1 is empty.
Your problem is simple. Just replace the below line from your child page's js function
textbox.Value = val;
to
textbox.value = val; // lowercase "v"
or justdo a direct assignment like this
document.getElementById("<%=TextBox1.ClientID%>").value = val;
Or another possible solution would be to directly pass the required value from the parent page as a querystring value and you don't need the js function in the popup page. The querystring value you can access it in child pages's page load event and assign it directly to the textbox.
Your Parent js
function NewCustOpn() {
var ddlvalue = document.getElementById("<%=DropDownList1.ClientID%>").value;
var popup = window.open("Popup.aspx?dropdownval=" + ddlvalue, "Popup", "toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=0,width=520,height=350,left = 250,top = 50");
}
And from you child page's code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["dropdownval"])) {
TextBox1.Text = Request.QueryString["dropdownval"];
}
}

pop up does not go for post back in asp.net

I have button called sales and it have a JavaScript popup when I click on cancel it postback and the values in the form are inserted but when i click on ok it does not post back and the values in the form does not go in the database ( the JavaScript button is actually print call and when button is clicked it asks for print when print dialog box is open it does not post back and data is not inserted in the database)
here is the javascript code
function confirmAction(printable) {
var r = confirm("You want to Print Invoice?");
if (r == true) {
var printContents = document.getElementById(printable).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
__doPostBack();
}
else {
__doPostBack();
}
}
here is the code for button click
<asp:Button ID="btnaddsale" runat="server" Text="Sale" OnClick="btnaddsale_Click" OnClientClick="javascript:confirmAction('printable')"/>
Ok, couple of notes for you:
You want a postback in either case.
Your <asp:Button> will automatically do a postback either way, so you don't need to call __doPoskBack(); in this scenario.
Major issue here is that, if you want a postback, it will happen immediately when the function exits, effectively canceling out the print dialog too soon. To avoid this, we will use a JavaScript trick that will check if the document has focus, and only when it does (when user exits print dialog in the browser) will we return and allow the postback to occur.
To fix the issue,
First: Make the function return true; when user cancels, and wait for focus and then return true if the user wants to print:
function confirmAction(printable) {
var r = confirm("You want to Print Invoice?");
if (r == true) {
var printContents = document.getElementById(printable).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
// Check focus after user exits print dialog and then return true for the postback
var document_focus = false;
$(document).focus(function () { document_focus = true; });
setInterval(function () { if (document_focus === true) { return true; } }, 500);
}
else {
return true;
}
}
Then, change the JavaScript code to use the return statement in the OnClientClick event:
<asp:Button ID="btnaddsale" runat="server" Text="Sale"
OnClick="btnaddsale_Click"
OnClientClick="javascript:return confirmAction('printable')"/>
Update based on comments and your changed requirement:
Here's a snippet to make the script pop up after the postback. So you will insert values to database, and then add the print script / confirm dialog on page load using Page.ClientScript.RegisterStartupScript()
Note I don't recommend to embed the script in your C# code, so I'd suggest to take your confirmAction() function and place it (if not already) into a separate "yourScripts.js" file and then just call the function name when the page is loaded using jQuery. Here's an example:
In your master page or page header: This file should contain the confirmAction() function
<script type="text/javascript src="path/to/yourScriptsFile.js">
Then, in code-behind:
protected void Page_Load(object sender, EventArgs e)
{
// Only display script on PostBack, not initial page load
if (IsPostBack)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"confirmAction",
#"<script type=""Text/Javascript"">$(document).ready(function() { confirmAction('printable'); });</script>");
}
}
Also note, since you will NOT want a postback now, the confirmAction function should no longer return true; or use the trick code I posted above, and will just return false:
function confirmAction(printable) {
var r = confirm("You want to Print Invoice?");
if (r == true) {
var printContents = document.getElementById(printable).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
return false;
}

refresh base window from pop up window in asp.net

I am opening a popup window B from page A using:
this.Page.ClientScript.RegisterStartupScript(
this.GetType(),
"JavaScript",
"myWindow=window.open('B.aspx',
'WindowName',
'copyhistory=no,
width=800,
height=300,
top=150,
left=50,
resizable=1,
scrollbars=1');
myWindow.focus();",
true
);
I want to refresh page A whenever there is a change in popup window B.
There are many ways to do it:
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>
<script language="JavaScript">
<!--
function refreshParent() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow)
{
window.opener.progressWindow.close()
}
window.close();
}
//-->
</script>
Sources:
Open popup and refresh parent page on close popup
http://forums.devarticles.com/javascript-development-22/auto-refresh-parent-window-after-closing-popup-4864.html
Refresh parent window when the pop-up window is closed
Call refreshParent on user actions as :
function refreshParent() {
window.opener.location.href = window.opener.location.href;
}

get ID of clicked control

Using jQuery I'm trying to get the id of control, which I clicked (radiobutton). I read this question and tried almost everything from there:
alert($(this).get(0).id);
alert($(this).id);
alert($(this).attr('id'));
alert(this.id);
But I'm always getting: Undefined
I just don't understand what I'm doing wrong.
UPDATED:
Radiobuttons is generated dynamically in code behind by C#:
controlToReturn = new RadioButton
{
ID = controlId
};
((RadioButton)controlToReturn).Text = text;
((RadioButton)controlToReturn).Checked = Convert.ToBoolean(Convert.ToInt32(value));
((RadioButton)controlToReturn).GroupName = groupName;
((RadioButton)controlToReturn).CssClass = cssClass;
((RadioButton)controlToReturn).Attributes.Add("runat", "server");
((RadioButton)controlToReturn).Attributes.Add("onclick", "Show();");
and function in ASPX:
<script type="text/javascript" language="javascript">
function Show() {
if ($(this).cheked = true) {
console.log(this);
alert($(this).get(0).id);
alert($(this).id);
alert($(this).attr('id'));
alert(this.id);
}
}
</script>
I know radiobutton has id, I checked generated HTML.
Your problem is this has no context within your function and is in fact the window itself.
You would need to modify both the output html to provide context as an argument:
((RadioButton)controlToReturn).Attributes.Add("onclick", "Show(this);");
and change the function Show:
function Show(el) {
/* for jQuery use $(el) */
if(el.checked) {
alert(el.id);
}
}
C#:
((RadioButton)controlToReturn).Attributes.Add("onclick", "Show(this);");
JavaScript:
function Show(radio) {
if (radio.checked) {
alert(radio.id);
}
}
To attach a click-listener and alert the ID, your code would look something like this:
​$(function () {
$("input[type='radio']").on("click", function () {
alert(this.id);
});
});​
A working demo: http://jsfiddle.net/SSBnV/1/

Categories

Resources