Tuesday, March 13, 2012

Strange problem

Ok, I have this ASP.NET application I am making.

the main code is the updateTable(), this function populates a Table web control with rows of data, the last cell being a button, this button is attached an attribute to identify the row. A click event is attached to this button. The click event of this button fires the updateTable() again, using the attached attribute as the argument (sort of like a file structure, and the button says, enter this directory so it passes the directory name of that row, and reloads the table with the dir/files of that directory.

Now here is the problem, if I have my initial set root directory and updateTable(root dir) in the page_load, the click event fires correctly, but of course the updateTable gets called twice (which works as the click event is after the first one), but when you try to drill down a second time, it fouls it up because the path gets reset to the begining, and the new path doesn't exist there.

What I am trying to do is this.

Page_Load()

If (!Page.PostBack)
updateTable(Default Path)

Then have my button click event do

updateTable(myButton.Attributes["newDir"].ToString();

But with out the updateTable event firing on Page_Load, the click event does not get handled.

Hopefully this makes sense, if need more info, let me know.I been playing around with this problem for a bit tonight. And I made some changes.

I made the button click event simple set Session["currentDir"] and changed the Page_Load to call updateTable(Session["currentDir"].ToString(); and in the !Page.PostBack clause, I have it setting Session["currentDir"] to the application parameter setting for "rootDir" I have.

Now everything works, but... You got to click twice on all the buttons and I can't seem to figure out how to avoid that. The problem appears to be the click event doesn't fire until after the page is reloaded so the first time it has the last directory set, and changes the session variable, but will only update the table when you click it yet again.
It's a timing issue. You have to consider the fact that events are called after Load and before PreRender. Thus, if you make changes to the data from an event, you need to rebind the controls that depend on this data after you've made the change. Otherwise, the change will only be visible on the next postback (hence the "double click").
By the way, it's not a very good idea to use Session in this context.
Does this help?
How do you rebind the control, you mean attach the button handler to it again?

I tried doing it so that button event called the updateTable() function, but that didn't work unless the event fired in the beginning of page load, so I changed it to a session variable see if that helped. It make the thing work, but didn't solve the problem as now I have to click twice.
No, WhateverControl.DataBind();

The structure of your page should more or less be:

Init: create controls if necessary.
Load: read datasources and bind controls if not ispostback
Events: update data sources according to the event and rebind relevant controls.

0 comments:

Post a Comment