Showing posts with label window. Show all posts
Showing posts with label window. Show all posts

Wednesday, March 28, 2012

Ajax Update Panel & Opening a new browser window

Please excuse my terminolgy, I am not used to discussing in detail Embarrassed

I have a fairly complicated page, containing various asp controls. One of which is a dynamically populated datagrid. Rows on this datagrid can be clicked and then within the SelectedIndexChanged Event on the grid I am working out several values and passing them as query strings into a URL and am then using response.redirect to open the URL as a new page with the parameters available to it. This works fine.

Response.Redirect("'MyPage.aspx?Sdate=" & sdate & "&Edate=" & edate & "&AreaCode=" & AreaCode & "&Qsdate=" & Qsdate & "&Qedate=" & Qedate & "")

The problem is that the whole page is wrapped in an Ajax Update Panel, for various reasons, and I now need to open the new page (after clicking a row on the datagrid) in a new browser window.

The usual method I think would be -

Response.Write("<script>window.open 'MyPage.aspx?Sdate=" & sdate & "&Edate=" & edate & "&AreaCode=" & AreaCode & "&Qsdate=" & Qsdate & "&Qedate=" & Qedate& "', '_new','width=400, height=200');</script>")

BUT... I gather Response.Write can't be used with Ajax and when I have tryed it, I just aget a parsing error. Can anyone suggest how I can open a new browser window from the SelectedIndexChanged Event, whilst still keeping the datagrid within the Ajax update panel.

You can add a label, and make it initially invisible.// Label1.Visible =false;

Assign the Text property of that label, the java script you want to execute.

And on the SelectedIndexChanged event set the visibility of the Label to true and text to the javascript.

Label1.Text = "<script>window.open 'MyPage.aspx?Sdate=" & sdate &"&Edate=" & edate & "&AreaCode=" & AreaCode &"&Qsdate=" & Qsdate & "&Qedate=" & Qedate& "','_new','width=400, height=200');</script>";

Label1.Visible = true;

But, basically, you could do all of that on the client side. I see no reason why you make a post back, if you just append some values from the client side and concatenate them.


That still won't work with Ajax, I think that might be due to the way the event fires on the datagrid? It is making the lable visible and setting the text to that of the javascript when I debug, but once the page is loaded it just highlights the row selected and nothing else, the label is not even visible.

I don't think this method is right for what I am trying to achieve anyway, as there are variable values in my VB Code behind that need to be passed to the URL.Therefore how can I actually pass values held in vb.net variables into the javascript code?

i.e. - "<script>window.open 'MyPage.aspx?Sdate=" &sdate& "'

sdate is a variable that is used in my vb.net code that holds a date and I need that to be in the URL that is used in the javascript, same as all the other variables above - edate, areacode, etc.

Any other suggestions?


*Bump*


jaYKay:

Response.Write("<script>window.open 'MyPage.aspx?Sdate=" & sdate & "&Edate=" & edate & "&AreaCode=" & AreaCode & "&Qsdate=" & Qsdate & "&Qedate=" & Qedate& "', '_new','width=400, height=200');</script>")

Check what isRegisterStartupScript(Page, Type, String, String, Boolean) for a server side solution, or seeASP.NET AJAX Client Life-Cycle Events. if you need a client side solution.


Did you ever find a solution to this problem? I have the exact same situation and have spent the last 2 days trying to find a way to open a new browser window with dynamic values.

If you or anyone else has a solution to ths problem, could you please post it?

Thanks,

Ken


Hi, I've just coded a way of doing that..it's really ugly one, but this is what I can think of at this moment :)

Here is the web page code:

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
<!--

function PopUp()
{
var label = document.getElementById('Label1');
if(label!=null)
{
window.open(label.innerText);
}
}

//-->
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" visible="false" style="visibility:hidden" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
<script language="JavaScript" type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(EndRequest);

function EndRequest(sender, args) {
PopUp();
}
</script>
</body>
</html>

and the code behind (in c#):

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "http://www.asp.net";
Label1.Visible = true;
}
}

Hope this will help.

Cheers,

Yani


I was not able to get this variation to work, however on another thread I did get an answer that does work and is much cleaner.

Try this

http://forums.asp.net/p/1114996/1730089.aspx#1730089

Thank you very much for your reply.

Ken


I ended up cheating it really. I just used two panels on top of each other and made the first one and its contents disappear and the second one reappear, therefore giving the illusion that it was a new page, only problem now is that the information for the user is at the top of the second panel and the grid they clicked on is at the bottom of the first so they always have to scroll up to the top of the page each time. It works, if not a bit messy, I just need to set the focus to the top of the page, which is not that simple as the page is not refreshing as such and contains data that was parametised by the user.


Hi,

Have you looked at this thread http://forums.asp.net/t/1122462.aspx . I have posted a solution for similar problem in this thread. I hope this should solve your problem instead of doing it the above way. If you have any queries let us know.


Monday, March 26, 2012

AJAX timer wont trigger UpdatePanel in Window Mobile 5 device

My webform WORKS FINE when published to its localhost site and I bring it up in IE7.

I have a GridView and a textbox inside an updatepanel and an AJAX timer to trigger the update.

When the AJAX timer ticks, the textbox receives the current time and the Gridview updates from its SqlDataSource.

BUT ... when I try to execute it on 1) the Pocket PC Windows Mobile 5 Emulator OR 2) on a real PPC with WM5, it just sits there static after the initial load. Manual refreshes work but the timer appears to do nothing.

I would appreciate any ideas, especially any methods to help debug the client behavior to see if that timer is indeed doing anything.

<asp:ScriptManagerID="ScrMgr1"runat="server"EnablePartialRendering="true"/><asp:TimerID="Timer1"runat="server"OnTick="Timer1_Tick1"Interval="5000"></asp:Timer><asp:UpdatePanelID="UpdatePanel1"runat="server"UpdateMode="Conditional">
<Triggers> <asp:AsyncPostBackTriggerControlID="Timer1"EventName="Tick"/></Triggers><ContentTemplate> <asp:GridViewID="GridView1"runat="server"DataSourceID="SqlDataSource1"></asp:GridView> <asp:LabelID="Label1"runat="server"Height="82px"Text="Label"Width="306px"></asp:Label> <asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NautilusDBConnectionString %>"
SelectCommand="SELECT [Source], [Loads] FROM [Loads]" ProviderName="<%$ ConnectionStrings:NautilusDBConnectionString.ProviderName %>"></asp:SqlDataSource></ContentTemplate></asp:UpdatePanel>

=====================================

Code Behind

protectedvoid Timer1_Tick1(object sender,EventArgs e)

{

GridView1.DataBind(); //needed, or no data update

Label1.Text ="UpdatePanel1 refreshed at: " +DateTime.Now.ToLongTimeString();

UpdatePanel1.Update();

}

You need to setChildrenAsTriggers="false"when you want to callasp:UpdatePanel.Update()inthe timer Tick event handler.It should work fine.
Try to take a look at this document for details when calling asp:UpdatePanel.Update()method -http://ajax.asp.net/docs/mref/M_System_Web_UI_UpdatePanel_Update.aspx.
Wish this can help you.
Thanks Jasson that worked. But you didnt say why it worked in IE7 but not in Mobile 5. I made some further changes in the project
and never could get it working again even with my new knowledge from the documentation.
NOW I have the same problem using a timeras a child. It works in IE7 but a breakpoint in the timer handler
is never reached by either the PDA emulator or a real PDA ... that is in the case that I connect
to the running Visual Studio project. Same behaviour when I publish the project to IIS and connect there.
Why does it work in IE7 and not in IEMobile?
 
protectedvoid Timer1_Tick(object sender,EventArgs e)

{

GridView1.DataBind();

Label1.Text = System.

DateTime.Now.ToString();

}

==========================================================

<%

@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title>

</

head>

<

body><formid="form1"runat="server">
<asp:ScriptManagerID="ScriptManager1"runat="server"/><br/><asp:UpdatePanelID="UpdatePanel1"runat="server"ChildrenAsTriggers="true">
<ContentTemplate>
<asp:TimerID="Timer1"runat="server"Interval="5000"OnTick="Timer1_Tick"></asp:Timer>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NautilusDBConnectionString %>"SelectCommand="SELECT [Source], [Loads] FROM [Loads] ORDER BY [IDnum]"CacheDuration="1"></asp:SqlDataSource>

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataSourceID="SqlDataSource1"Style="z-index: 100; left: 270px; position: absolute; top: 39px"></asp:GridView>

<asp:LabelID="Label1"runat="server"Height="19px"Style="z-index: 102; left: 18px;

position: absolute; top: 186px"

Text="Label"Width="198px"></asp:Label><br/></ContentTemplate></asp:UpdatePanel></form>

</

body>

</

html>