Popup window loading before itemcommand event firing

C

ChrisB

I'm attempting to open a new window from a LinkButton in a DataGrid.
I can set a session variable in the ItemCommand event for the
LinkButton like so:

// this is used to handle the ItemCommand event
private void itmCmd(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string itemGFNo = "";
if (e.CommandName == "EditTask")
{
if(e.CommandSource is LinkButton)
{
LinkButton lb = (LinkButton)e.CommandSource;
if (e.Item.FindControl("GFNoButton") != null)
{
itemGFNo = ((LinkButton).Item.FindControl"GFNoButton")).Text;
}
SetGFNoSession(itemGFNo); //set session var here
}
}
DataGridTaskList1.DataBind();
}

In the ItemDataBound event I can find the LinkButton and assign an
attribute like so:

private void DataGridTaskList1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.FindControl("EditTaskButton") != null)
{
((LinkButton) e.Item.FindControl
("EditTaskButton")).Attributes.Add("onclick",
"window.open('AddTaskPopup.aspx',null,'status= no, resizable= no,
scrollbars=no, toolbar=no,location=no,menubar=no ');");
}
}

The result is that on the AddTaskPopup page (in the page_load event) I
can get the session var and use it however I need to.

This works great the first time through, but each time thereafter the
session var is not set properly. Here's the sequence of events:

1) Page_Load event with datagrid fires
2) itemdatabound event fires (where I set the attribute to window.open
for AddTaskPopup)
3) I click on the LinkButton, the ItmCmd event fires setting the
session var
4) The page_load event for the popup fires, (and I set a label using
the session var I set in the ItmCmd event)
5) the popup AddTaskPopup opens up
6) I close the popup window using this code:
string strScript = "<script>";
strScript += "window.close();";
strScript += "</script>";
Response.Write(strScript);
7) The Page_Load event for the popup fires again
8) I'm returned to the original page, where I click another list
button
9) The page_load event for the original page fires
10) The ItmCmd event fires, changing the session var accordingly
11) The popup AddTaskPopup opens up again

The problem is, the session var I set in the ItmCmd event is not
updated becuase the PopUp loaded BEFORE the ItmCmd event, not AFTER!
So anything I do in the ItmCmd event has no effect on the Popup
because the Page_load event is called before my ItmCmd event ever
fires/

I think it may be a timing issue because it appeared to run correctly
a few times while debugging the code to write down the sequence of
events.

Anyone have an idea what's going on?

Thanks in advance,
Chris
 
M

Martin Dechev

Hi, Chris,

It will be way simpler if you pass the info in the query string instead of
doing a roundtrip to the server to store it in the session. Apart from the
round-trip, you can't be sure which code will be executed first - the
LinkButton OnItemCommand or the AddTaskPopup.aspx Page_Load.

Hope this helps
Martin
 
C

Chris Brannon

Martin,

Thanks for your reply. I was able to pass the value I needed with a
query string in the ItemDataBound event. I worry that if I wasn't able
to get the data I needed at the time the ItemDataBound event fired, I'd
still be in trouble.

Thanks again,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top