Return value of ModalDialog is always undefined - asp.net

For some reason, the value returned by the Modal Dialog box is always "undefined".
My Main page (aspx):
<%# Page Title="Home Page" Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<html>
<head></head>
<body>
<script type="text/javascript">
function openWindows () {
var getval;
getval = window.showModalDialog('../WebSite/popups/uploader.htm');
document.getElementById("Input").value = getval;
}
</script>
<input id="Input" runat="server" />
<input type="button" id="Button1" runat="server" onclick="openWindows()" value="Choose Image"/>
</form>
</body>
</html>
So in this case, the value of getval is always "undefined"
My Dialog Box (HTML) Code:
<html>
<head>
<script type="text/javascript">
function ReturnValues() {
var objfile = document.getElementById("fileup").value
document.getElementById("TxtInput").value = objfile
var vReturnValue = document.getElementById("TxtInput").value;
window.ReturnValue = vReturnValue;
window.close();
}
</script>
</head>
<body>
<form id="Formuploader" method="post" runat="server">
<input id="TxtInput" type="hidden" runat="server" /><br />
<button id="btnSaveImage" runat="server" onclick="ReturnValues()">Save Image</button>
</form>
</body>
</html>
Here, ReturnValue does have the required value. But as soon as the ModalDialog closes, the getval variable in the main window becomes undefined.
Any help is greatly appreciated.
Thanks!

when you return value, do it this way:
window.returnValue = vReturnValue;
use lowercase returnValue, not ReturnValue.
also, your modal dialog is not closing. to fix that, change your button to a link.
<a href="#" id="btnSaveImage" runat="server" target="_self" onclick="ReturnValue()">
Save Image</a>

Try this:
window.opener.document.getElementById("Input").value = getval;

Related

Set or get html tags value or innerhtml in razor view

how to set or get html elements inner text or html controls value in Razor syntax.
i know how we can do this in aspx file using Runat="server" attribute like this
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><%Response.Write(Page.Title); %></title>
<script runat="server">
protected void Button1_ServerClick(object sender, EventArgs e)
{
p1.InnerText = "Hello " + textbox1.Value; //get textbox value and set in html p tag
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="textbox1" runat="server" /><br />
<input type="button" id="btn" runat="server" value="Click Me" onserverclick="Button1_ServerClick" />
<p id="p1" runat="server">see result here</p>
</div>
</form>
</body>
</html>
Basically something like this...
View (SomeAction.cshtml):
...
<p>ViewBag.SomeValue</p>
...
Controller:
...
[HttpGet]
public ActionResult SomeAction()
{
ViewBag.SomeValue = "Hello";
return View();
}
...
However you should learn much about approach which is used to build applications using ASP.MVC. It's completely different than ASP.NET WebForms. WebForms technology uses events while ASP.MVC uses Model View Controller. You have to change your mindset.

changing HTML tag properties using inline server tags

I have the following submit button:
<form id="form1" runat="server">
<input type="submit" id="submit" value="Submit" runat="server"/>
</form>
How do I change e.g. the background colour of the button itself by using inline server tags after the button has been clicked?
I've tried using the attribute style="" in the submit buttons declaration with inline server tags <%%>, but I just get a Parser error message saying "Server tags cannot contain <% ... %> constructs."
Expected code to be used to verify button click:
<%
if (Request["submit"] != null)
{
// CODE HERE
}
%>
<form id="form1" runat="server">
<%
if (Request["submit"] != null)
{ %>
<input type="submit" id="submit" value="Submit" runat="server" style="background:#ffffff"/>
<% }
else{ %>
<input type="submit" id="submit" value="Submit" runat="server" style="background:#aaaaaa"/>
<%}%>
</form>
or
<form id="form1" runat="server">
<% string color="";
if (Request["submit"] != null)
{ %>
color="background:#aaaaaa";
<% }%>
<input type="submit" id="submit" value="Submit" style="<%=color%>"/>
</form>
If you use the second one you the submit button can not have runat="server" attribute
its not proper way to mix the server side code with the HTML markups..
use code behind for such things..
try the following..
if(Request["submit"]==null)
submit.Style.Add("background", "red");
else
submit.Style.Add("background", "white");

Why is this leaking memory in ie 8?

edit 2 -
To recreate - (I hope)
Create an empty asp.net project, add a default.aspx, and an about.aspx
use this code for the default.aspx file:
http://pastebin.com/4Ri1BufM
and this for about.aspx
http://pastebin.com/Ni9dZL7c
Build, and run sieve against it. Click the button, then the link. Sieve should now list 1 memory leak.
This simple layout puts a server button inside a panel. If you click it, and then the "Leave" link, sIEve reports 1 memory leak:
http://localhost:2605/Default.aspx (1 reference)
<INPUT value="partial postback" type=submit name=ctl01 >
Aspx source:
<%# Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="LeakTestProject._Default" %>
<html>
<head>
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button Text="partial postback" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
<a href="SomeOtherPage.aspx" >leave</a>
</body>
</html>
I've think I've tracked down a memory leak in my application to this source.
Would anyone be able to explain why this is happening, and what to do to fix it?
(Note - there is no server side code running)
edit-
Here is the generated html:
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="Default.aspx" id="Form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTUzNzM3OTMxMmRk27mKixZYS2MtHiuw7SZF6+1A/CkWRnnarcN2a05yCJs=" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<script src="/WebResource.axd?d=_lxNoU5aYdEC_WWnUhGTN0VgFQnwFJ1fkZBkPhUGNITY1Tt87f4CGSPd9dhq2TD9I8lv5ljTGi956KpbHSLq3ebld0t7nKLON7Hc04CCAwA1&t=634525784292792916" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=0VE3fsyoY2cTFnat6m0YjE4OCCjVV0jmjSC6wigMBEOHyKW8kewBrhtJnBFIn73VSWcGW45QXwelC5m-1iZoornC8qWpZ-kXP-Y38e49qW1TbssOgo419ZWtVHSVmIpSg9CvXOQzUBClAjuHORyX5Q2&t=ffffffffbd2983fc" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');
//]]>
</script>
<script src="/ScriptResource.axd?d=DBlWOXkmp0ycsSicGEZhACmLuhW1VF8BppK-Uj8ijyjhFxx7MTG8NW0gLl3IJ6TyEo2rAMfLCQ9rO_bk2wDCcpyT3Df6CN-TLMAA575gUNAjTSzC6U1koagV2FYRmu4TNsAgoEjhkN6fJX3lENf7Bw2&t=ffffffffbd2983fc" type="text/javascript"></script>
<div class="aspNetHidden">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLZvOXNBgKiwImNCx8fSMXEwq+V+5yrnoaTcidPwoJw3ebm6DJQ1D3fVVAL" />
</div>
<script type="text/javascript">
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('ScriptManager1', 'Form1', ['tUpdatePanel1','UpdatePanel1'], [], [], 90, '');
//]]>
</script>
<div id="UpdatePanel1">
<input type="submit" name="ctl01" value="partial postback" />
</div>
</form>
<a href="About.aspx" >leave</a>
</body>
</html>
According to http://www.codeproject.com/KB/ajax/jqmemleak.aspx there is a memory leak that sounds a lot like what you are experiencing.
See "Iteration 2 - Memory Leak"
Using a tool called 'SIEve' he states 'You can see that every time I click refresh, it creates a new set of input elements without destroying the previous ones. '
His fix was a JQuery plugin that calls into the Update panel cleanup hooks. You could potentially try this?
(function($) {
$.fn.Disposable = function(cln) {
return this.each(function() {
var el = this;
if (!el.dispose) {
el.dispose = cleanup; // will be called by
// Microsoft for cleanup
$(window).bind("unload", cleanup);
}
function cleanup() {
if (!el)
return;
$(el).unbind();
$(window).unbind("unload", cleanup);
el.dispose = null;
el = null;
};
});
};
})(jQuery);
function add() {
$get('res').value =
parseInt($get('i1').value) + parseInt($get('i2').value);
}
Sys.Application.add_load(function() {
$('.num').change(add).Disposable();
});
could you please try this change:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script>
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(my_init);
function my_init() {
document.getElementById("UpdatePanel1").innerHTML = "";
}
</script>

Javascript popup

I am trying to open this modal DIV from code behind but I am getting error message. Please let me know what's wrong with this.
Here is .aspx code.
<div id="modal" runat="server" style="display:none;">
<asp:Button ID="btnClose" runat="server" Text="Close" onClick="Popup.hide('modal')" CausesValidation="False"/>
</div>
Code behind
if (!Page.ClientScript.IsStartupScriptRegistered("MyScript"))
{
string confirmbox = "Popup.showModal('modal');return false;";
Page.ClientScript.RegisterClientScriptBlock(GetType(), "MyScript", confirmbox, true);
}
For the error mentioned by you as
"Too many characters in character literal when loading the page"
You have to use OnClientClick instead of using onClick as it is a ASP.NET Server Control and not a normal HTML5 control.
For the code you have asked for:
Code for opening modal on page load
ASPX Code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function hidePopup()
{
document.getElementById('modal').close();
}
</script>
</head>
<body onload="showPopup();">
<form id="form1" runat="server" >
<dialog id="modal">
<asp:Button ID="btnClose" runat="server" Text="Close" OnClientClick="hidePopup();return false;"/>
</dialog>
</form>
</body>
</html>
Code Behind .CS Code
if (!Page.ClientScript.IsStartupScriptRegistered("MyScript"))
{
string confirmbox = "function showPopup(){document.getElementById('modal').showModal();}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", confirmbox, true);
}
Tested on Chrome Version:39.0.2171.99 m , it works perfectly.
With this modal you can also exit the popup using "Esc" key,instead of Close button also.

jQuery: function not working when clicking button inside <form runat="server">

With jQuery, in (document).ready I assigned a click function to all buttons of my asp.net (aspx) page.
When I click a button outside , the function works properly.
When clicking a button INSIDE the form, it doesn't work. Why?
Here my default.aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jQuery._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 type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("button").click(function () {
$("input").before("ciao");
});
});
</script>
</head>
<body>
<button id="Button1">Button BEFORE Form</button>
<button id="Button2">Another BEFORE Form</button>
<form id="form1" runat="server">
<button id="btStart">Button in Form</button>
<div>
<input type="text" value="I like number 1" />
<input type="text" value="Smile to number 2" />
</div>
</form>
</body>
</html>
I'm using Visual Studio 2010. I tried also with jQuery 1.4.2, same problem.
Thanks for letting me know, cheers.
Since you are clicking a button in a <form>, it triggers a submit action. Try adding
return false;
after
$("input").before("ciao");
to prevent the default action related to that click event.
It should work but the button is just submitting and refreshing so you are not noticing it. Since your using asp.net you could do it by just using an asp:Button like:
<asp:Button ID="yourButton"
OnClientClick="$('input').before('ciao'); return false;" Text="Test" />

Resources