I'am developing with c# and asp.net.
I have some pages with update panels. In this update panels there is a button that redirects to a new window. This is done by calling the window.open with the scriptmanager in code behind. Everything works fine until I don't use https. If I use tunnelling with a router to have a secure line till the router and then use a proxy to access my webpages, the window.open called from the buttons that are in an update panel open a new register card, but there I get the error : page not found. In the url I can see that the router did not put the proxy+IP before the path. That does not happen if I remove the update panel.With Firebug I could see that with the update panel I get a POST and in the resonse the contentType is text/plain. Without the update panel there is a GET and the response contentType is text/html. So what can I do to run this without removing the update panels?
This works fine until I don't use https over my proxy:
protected void btnPrint_Click(object sender, EventArgs e)
{
url = "~/Gui/Report/ReportViewer.aspx?ReportName=CustomerReport";
Page page = (Page)HttpContext.Current.Handler;
if (page == null) {
Redirect(url);
}
url = page.ResolveUrl(url);
string script = #"window.open(""{0}"");";
script = String.Format(script, url);
ScriptManager.RegisterStartupScript(page,
typeof(Page),
"Redirect",
script,
true);
}
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<dx:ASPxButton runat="server" ID="btnPrint"
Text="print" OnClick="btnPrint_Click">
</dx:ASPxButton>
</ContentTemplate>
</asp:UpdatePanel>
Got it....
the information that the scriptlanguage is javascript is lost if I am using the proxy (strange...?!?!).
If I build the script tags on my own with adding the attribut "language='javascript'" then everything works fine.
Can anybody tell me why this information is lost?
Here the new code:
protected void btnPrint_Click(object sender, EventArgs e)
{
url = "~/Gui/Report/ReportViewer.aspx?ReportName=CustomerReport";
Page page = (Page)HttpContext.Current.Handler;
url = page.ResolveUrl(url);
string script = "window.open('" + url + "');";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append(script);
sb.Append("</script>");
ScriptManager.RegisterStartupScript(page,
typeof(Page),
"Redirect",
script,
false);
}
Related
I'm trying to use a Button to open a link in a new tab in ASP.NET. I'm trying the following but it isn't working:
<asp:Button ID="ReportButton" runat="server" CssClass="button" Font-Size="XX-Large" ForeColor="White" Text="Report" OnClick="ReportButton_Click" OnClientClick="form1.target='_blank';" />
In the code, ReportButton_Click is defined as follows:
protected void SkidPackReportButton_Click(object sender, EventArgs e)
{
GoToPage(LocationSkidPackReportPage);
}
and GoToPage is defined as follows:
bool GoToPage(string page)
{
try
{
Response.Redirect(page);
return true;
}
catch (Exception)
{
StatusLabel.Text = "There was an error finding the page.";
return false;
}
}
Don't do server-side Response.Redirect, just do a client-side window.open. E.g.
void GoToPage(string page) {
ScriptManager.RegisterStartUpScript(this, this.GetType(), "newPage", String.Format("window.open({0});", page), True);
}
Or better yet - avoid postback altogether. You can assign clientClick to your button like:
ReportButton.OnClientClick = String.Format("window.open({0});return false;", LocationSkidPackReportPage);
This way new page will be opened on client without need to go back to the server.
Make LocationSkidPackReportPage a public property in code behind and then replace your button by:
Report
OR, if you need to fill this var in code behind:
// Response.Redirect(page); -> Replace this by:
string script = String.Format("window.open('{0}', '_blank');", LocationSkidPackReportPage);
ScriptManager.RegisterStartUpScript(this, this.GetType(), "reportResultPage", script, True);
this work for me
Page.ClientScript.RegisterStartupScript(
this.GetType(), "OpenWindow", "window.open('../_Reportes/ReporteGeneral.aspx','_newtab');", true);
Please look at 1st image.
In this page i use LinkButton. ( Like... "sr001","sr003", and all )
.aspx page
<asp:LinkButton ID="lnkbtn" runat="server" onclick="lnkbtn_Click"
ValidationGroup='<%# Eval("pid") %>'><%#Eval("productcode") %></asp:LinkButton>
.cs page
protected void lnkbtn_Click(object sender, EventArgs e)
{
int id;
id = Convert.ToInt32(((LinkButton)sender).ValidationGroup.ToString());
string abc = "http://development.in/prod-more-info.aspx?pid=" + id;
Response.Write("<script>window.open('" + abc.ToString() + "','_blank');</script>");
}
Now, When I clik on this link button process work successfully.
BUT..............
My design is disturb by this process, look at 2nd image..
How can I fix this problem?
PLEASE HELP ME....
Doing a raw Response.Write is not advisable since you are literally just appending content to the response stream.
If you want to ensure your script shows up in the proper place and executes you should use ClientScriptManager.RegisterStartupScript instead.
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(this.GetType(), "MoreInfoPopup"))
{
int id;
id = Convert.ToInt32(((LinkButton)sender).ValidationGroup.ToString());
string abc = "http://development.in/prod-more-info.aspx?pid=" + id;
string script = String.Format("<script>window.open('{0}','_blank');</script>",abc);
cs.RegisterStartupScript(this.GetType(), "MoreInfoPopup", script);
}
Are you just wanting to reload the page once the button is clicked? If so, do a response redirect to Request.Url.AbsolutePath.
The code:
Response.Redirect(System.Web.HttpContext.Current.Request.Url.AbsolutePath);
How to open a radwindow on the click event of a Imagebutton within a user control?
Moreover i have used the same code in aspx page and it works fine.
car.ascx
code behind car.ascx.cs
protected void btnCarLogo_Click(object sender, ImageClickEventArgs e)
{
carurl="https://www.google.co.in/"
ScriptManager.RegisterStartupScript(this, this.GetType(), "popCarWindow", "window.radopen('" + carurl + "', 'CarDetails');", true);
}
It has VisibleOnPageLoad property. If you set it to true, window will be visible after postback.
Examples:
Show window
myRadWindow.VisibleOnPageLoad = true;
Hide window
myRadWindow.VisibleOnPageLoad = false;
Take a look here: http://www.telerik.com/community/forums/aspnet-ajax/window/opening-radwindow-from-the-server.aspx and see that the parameters are Page and not this (i.e. UserCOntrol).
Here is on working with JS functio nnames in user controls: http://www.telerik.com/support/kb/aspnet-ajax/general/using-dynamic-unique-names-for-javascript-functions.aspx
And, if you are going to have more than one manager on the page: http://www.telerik.com/help/aspnet-ajax/radwindow-troubleshooting-wrong-window-opened.html.
That way probably you get errors stating that the window is null
Try it like this:
Code behind:
string script = "<script language='javascript' type='text/javascript'>Sys.Application.add_load(ShowWindow);</script>";
ClientScript.RegisterStartupScript(this.GetType(), "showWindow", script);
Then on your aspx:
<script type="text/javascript">
function ShowWindow()
{
var oWnd = window.radopen('https://www.google.co.in/', 'window1');
}
</script>
I'm trying to simulate a button click in my program in order to get the response from the server. The problem is that the web page "onclick" activate a script in that page
The html look like this:
<script language=javascript>
function frmsubmit(id)
{
document.all("HistoryData1_hiddenID").value=id;
document.all("Form1").submit();
}
</script>
<input type="button" id="btnGo" value="Go" Class="RegularButton" onclick="frmsubmit('0')" >
I used this code that i have seen:
WebBrowser wb = new WebBrowser();
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
wb.Document.GetElementById("btnGo").InvokeMember("click");
Nothing happens. In the real web page I get an html with data that I need. Any ideas?
you can use jquery like
$("#btnGo").click();
in your .cs you will use it like
StringBuilder sb = new StringBuilder();
sb.AppendLine("$(document).ready(function() {");
sb.AppendLine("$('#btnGo').click();");
sb.AppendLine(" });");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", sb.ToString(), true);
thats it, of course you have to add jquery libraries to html page
Try calling doPostBack
__doPostBack("btnGo', '<event argument here>');
http://wiki.asp.net/page.aspx/1082/dopostback-function/
Use jQuery trigger() function (or triggerHandler, dependingo on your purpose).
I.e.
$('#<%=btnGo.ClientID%>').trigger('click');
I have a Image Button declared as,
<div>
<asp:ImageButton ID="btnDoWork" runat="server" ImageUrl="/_LAYOUTS/1033/IMAGES/row.png" ValidationGroup="Page" />
</div>
<div>
<asp:RequiredFieldValidator runat="server" ID="reqName" ControlToValidate="txtEmail" ValidationGroup="Page" ErrorMessage="enter a email" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$" ControlToValidate="txtEmail" ValidationGroup="Page" ErrorMessage="enter a email" />
</div>
within a update panel,
now in code behind I am doing something like this,
btnDoWork = (ImageButton)this.control.FindControl("btnDoWork"); //this code is in childcontrols method
btnDoWork.Click += new ImageClickEventHandler(btnDoWork_Click);
then
protected void btnDoWork_Click(object sender, ImageClickEventArgs e)
{
//Process a bit of code and at end,
this.Page.Unload += new EventHandler(Page_Unload_MessageBox);
and then in button click event,
public static void Page_Unload_Page_Unload_MessageBox(object sender, EventArgs e)
{
System.Globalization.CultureInfo _culture = Thread.CurrentThread.CurrentUICulture;
StringBuilder sb = new StringBuilder();
sb.Append("<script language=\"javascript\">");
sb.Append("$('body').append(\"<div id='M'><span id='text'>" +
SPUtility.GetLocalizedString("$Resources:abc", "def", (uint)_culture.LCID) +
"</span><br/><div id='BB' onclick='return BB();'><a href='' onclick='return BB();'>" +
SPUtility.GetLocalizedString("$Resources:OK", "def", (uint)_culture.LCID) +
"</a></div></div>\");");
sb.Append("function BB() { $('#M').remove(); $('#E').remove(); return false; }");
sb.Append("function dM(){ var browser = navigator.appName; if (browser == 'Netscape') { $('#M').css({ 'top': '5%' }, 500); } }");
sb.Append("</script>");
// Write the JavaScript to the end of the response stream.
HttpContext.Current.Response.Write(sb.ToString());
Now if I put email address I get error while when it tries to Response.Write I think, I wonder what alternative is there, e.g. can I use triggers in update panel or any other event or something..
here's the error I am getting now,
Note: I changed all variable names so don't get confused if something doesn't match
The message is very clear, you can not add this command HttpContext.Current.Response.Write on update panel, and that because can not know how to handle it, because the update panel is return a struct that is used by the javascript to redraw some part of the page.
The solution is to add a literal control inside the UpdatePanel, in the place you wish to add the extra html code, and write that control the render as:
txtLiteralID.Text = sb.ToString();
How ever, here you have a diferent situation than the normal, you won to render and run a script.
The main problem is how to trigger the script to run. The only way is to use the UpdatePanel handler that is this standard code:
<script type="text/javascript">
// if you use jQuery, you can load them when dom is read.
$(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
});
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
// after update occur on UpdatePanel run the code.
UnloadMsgBox();
}
</script>
Now on the EndRequest you need to call your script, where it may all read exist in your code as:
function UnloadMsgBox()
{
// render your code of the javascript.
$('body').append(\"<div id='M'><span id='text'></span><br/><div id='BB' onclick='return BB();'><a href='' onclick='return BB();'></a></div></div>\");
function BB() { $('#M').remove(); $('#E').remove(); return false; }"
function dM(){ var browser = navigator.appName; if (browser == 'Netscape') { $('#M').css({ 'top': '5%' }, 500); } }"
}
and not need to render it on UpdatePanel.
To summarize:
On the update panel you can not use the Response.Write to render something but a literal control, that renders inside him.
On the update panel you can not render javascript code and expect to run, to run a javascript code you need to use the EndRequest handler that comes with the UpdatePanel.
MS Ajax calls perform full page rendering, calculate the diff from the original, send the diff to the client, and magically merge the diff in the browser.
If you just send javascript as response, it's something the framework does not expect and it throws the message.
See a previous answer on how to invoke javascript from an UpdatePanel.