Showing posts with label code-behind. Show all posts
Showing posts with label code-behind. Show all posts

Monday, March 26, 2012

UpdatePanel & Opening a new window when returning from code-behind

Hi there,

Here's the OnClick event code for a button inside an UpdatePanel:

protected void btnReport_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "viewRepo", "<script>window.open('RepoViewer.aspx','viewReport')</script>");
}

As you can see, after returning from code-behind I want to open a new browser window with a given web form. Well, this works correctly without an UpdatePanel, or when EnablePartialRendering is set to false.

If EnablePartialRendering is set to true, a new window does not open.

Any workarounds?

-Benton

I'm having the same issue.

Rob


Hi Benton,

I'd the same problem, but I solve it, use've just to remove the tag scrip from your code (belive, it works!!!)

I used before this:

Sub wOpen(ByVal fWindow As String, ByVal fNameWin As String, ByVal fWidth As Integer, ByVal fHeight As Integer, ByVal fMenu As Boolean, ByVal fResizable As Boolean, ByVal fScroll As Boolean)
Dim strJ As String
strJ = "<script language='text/javascript'>"
strJ &= "window.open('" & fWindow & "', '" & fNameWin & "', "
strJ &= "'width=" & fWidth & ", height=" & fHeight & ", menubar="
If fMenu = True Then
strJ &= "yes"
Else
strJ &= "no"
End If
strJ &= ", resizable="
If fResizable = True Then
strJ &= "yes"
Else
strJ &= "no"
End If
strJ &= ", scrollbars="
If fScroll = True Then
strJ &= "yes"
Else
strJ &= "no"
End If
strJ &= "')<"
strJ &= "/script>"
'Page.RegisterClientScriptBlock("wOpen", strJ)
'Me.ClientScript.RegisterStartupScript(Me.Page.GetType(), "wOpen", strJ, True)
Me.ClientScript.RegisterStartupScript(Me.GetType(), "wOpen", strJ, True)
End Sub

And now I'm using:
Sub xOpen(ByVal fWindow As String, ByVal fNameWin As String, ByVal fWidth As Integer, ByVal fHeight As Integer, ByVal fMenu As Boolean, ByVal fResizable As Boolean, ByVal fScroll As Boolean)
Dim strJ As String
strJ = "window.open('" & fWindow & "', '" & fNameWin & "', "
strJ &= "'width=" & fWidth & ", height=" & fHeight & ", menubar="
If fMenu = True Then
strJ &= "yes"
Else
strJ &= "no"
End If
strJ &= ", resizable="
If fResizable = True Then
strJ &= "yes"
Else
strJ &= "no"
End If
strJ &= ", scrollbars="
If fScroll = True Then
strJ &= "yes"
Else
strJ &= "no"
End If
strJ &= "')"
Me.ClientScript.RegisterStartupScript(Me.GetType(), "xOpen", strJ, True)
End Sub

Note that I just remove the javascript tag, and it works very well.

jb.alessandro:

Hi Benton,

I'd the same problem, but I solve it, use've just to remove the tag scrip from your code (belive, it works!!!)


Note that I just remove the javascript tag, and it works very well.

Hi JB,

I am having the same problem. I hope there is a way to fix it with my case. Below is my script. Thanks

Response.Write(

"<script>window.open('../MailCenter/mailMain.aspx?tomemid=" +Convert.ToInt32(ViewState["MemID"]) +"','_blank');</script>");

How is it possible to remove the tags in this case? I tried but it didn't work.

blumonde

Saturday, March 24, 2012

UpdatePanel and Exceptions

Hi

I have an UpdatePanel using a trigger from the click event on a button. Everything seems to works ok, the click event hits the code-behind. My question is, how do you tell the page that an exception occured in the code-behind?

Many thanks

Craig

Hi,

you can use the ErrorTemplate provided by the ScriptManager control:

<ErrorTemplate>
<div>
There was an error processing your action. <br />
<span id="errorMessageLabel"></span>
<hr />
<button type="button" id="okButton">OK</button>
</div>
</ErrorTemplate>

This way, all the controls on the page will be disabled and the Exception message will be displayed in a nice overlay. You can customize the appearance of the ErrorTemplate but notice that the ids in red are "hard-coded-ids". The first is a label that displays the error message, the second is a button that re-enables the page.

Moreover, you can catch the PageError event of the ScriptManager control:

<atlas:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering="true"
OnPageError="ScriptManager1_PageError">

and, in codebehind, declare an event handler in which you can do custom processing of the error message:

protected void ScriptManager1_PageError(object sender, PageErrorEventArgs e)
{
e.ErrorMessage = "Unable to complete the request because: "
+ "<strong>"
+ e.Error.Message
+ "</strong>";
}

The PageErrorEventArgs instance exposes two properties:ErrorMessage that can be set to the error message that the client will receive;Error that returns a reference to the Exception instance.

Hi Garbin

My script manager is in a MasterPage:

<atlas:ScriptManagerID="scriptmanager"runat="Server"EnablePartialRendering="true"/>


How can I access the<ErrorTemplate> or the PageError event from the MasterPage in my aspx page which uses:

<atlas:ScriptManagerProxyID="scriptManager"runat="server"EnableScriptComponents="false">

I have a user control which renders a nice messagebox, so to be consistant I'd want to use that.

Thanks for your help.

Craig


Hi,

you cannot add an ErrorTemplate to the ScriptManagerProxy, but only to the ScriptManager control. But to preserve consistency it seems to me a good idea to have an ErrorTemplate in a MasterPage.

You can also drop in the ErrorTemplate a UserControl, just ensure that the ScriptManager is located inside the <form />. One problem however is that, as you know, the UserControl modifies the ClientID of its child controls and the ScriptManager relies on two hard-coded ids ("errorMessageLabel" and "okButton") to work as expected. One solution would be to assign these ids to non-server controls.