Button Click Event Data Not available in time

  • Thread starter Thread starter Michael Johnson Jr.
  • Start date Start date
M

Michael Johnson Jr.

I am trying to handle a button click event, which updates a web control
table with data. The button is dynamically created in the table itself.

When I call updateTable() in the Page_Load the new data from the button is
not available at this time as to allow the table to properly update.
So basically, I need to call UpdateTable() twice, once from page load and
once from button. This causes problems with my application, and I was
trying to figure out how I can make this work.

Basically I want to do this.

loadTable()
read data from source
create rows in table
as I create rows, create controls in each row based on data
wire click events to these controls

button click event
update current location var
call loadTable to display new data based on this location var
 
Michael said:
I am trying to handle a button click event, which updates a web control
table with data. The button is dynamically created in the table itself.

When I call updateTable() in the Page_Load the new data from the button is
not available at this time as to allow the table to properly update.

You _have_ to re-create the dynamically created controls in Page_Load or
earlier stage in the same order you created them before and with the
same identifiers as they had before. If you then also wire the events
again, you'll see that after Page_Load, the event will be emitted
by the button as expected.
So basically, I need to call UpdateTable() twice, once from page load and
once from button. This causes problems with my application, and I was
trying to figure out how I can make this work.

What problems does it cause ? After Page_Load the state must have
been restored, which for most controls happens automatically,
but if you create controls dynamically, you have to restore them yourself.

Best regards,

Eric
 
Well if I run the method in the button click event, the method runs twice,
which fouls up my code trying to drill down in the table.

If I just set a member variable in the button click event, i end up needing
to click the button twice to make it work.
 
Hello Michael,
Well if I run the method in the button click event, the method runs twice,
which fouls up my code trying to drill down in the table.

Do you mean that the method contains some piece of code that drills down
in the table and you don't want that piece to run both from Page_Load
and Button_Click ? Then I'd say split up the method in two methods.
Otherwise, could you perhaps explain in more detail what the problem is ?
If I just set a member variable in the button click event, i end up
needing to click the button twice to make it work.

I don't understand what you mean here.

Best regards,

Eric
 
Have a table1 webcontrol, and a method updateTable() that runs from
Page_Load event. updateTable() does a Directory.GetDirectories() and
Directory.GetFiles() to get a list of files/directories, and it populates
the table with this information. It also makes some buttons for each row.
For directories, it makes a "Enter Directory" button, for files it makes
"Delete, Download, and what not". I have created a standard event handler
for the button "Enter Directories" (Havent' messed with rest of the buttons
till I solve this problem". Now the problem comes in when the user clicks
the button (which passes the rows directory in the button.Attributes
collection to the click event) I want to update the table. I have tried
calling updateTable() with the new directory as the arguement and I have
tried using a variable which I set in the click event. The problem is the
click event fires after the page_load event, but if I do not populate the
table in the page_load event, the event won't fire. But since the event
hasn't fired, I don't know what to populate the table with. I have tried
calling the method in page_load then recalling it in the button click, the
problem with this is it gives me trouble when I try to go further than one
directory deep. I have tried setting a variable in the click event for the
new directory, but since the page_load event fires before it it causes me to
need to click twice to see the changes. I tried placing the function in the
OnPreRender and Render methods, since they are after the Page_load but I
seemed to have problems with it not showing anything.

I really can't think of any solution to this problem, but yet it sounds so
easy of a requirement.
 
Hello Michael,

Sorry for not replying today, I'll try to reply tomorrow.
It's almost midnight now here in the Netherlands.
How does a sleepy smiley look ? Something like E-) ?

Best regards,

Eric
 
Hello Michael,
till I solve this problem". Now the problem comes in when the user clicks
the button (which passes the rows directory in the button.Attributes
collection to the click event) I want to update the table. I have tried

I think a better idea for associating a row with a file/directory would
be to have a property that maintains an array in ViewState, which contains
the current list of files/directories. You can use this array to "redraw"
the table at Page_Load and it also makes the HTML smaller than when you're
using button.Attributes, especially if you have multiple buttons in a row.

public string[] FsEntries
{
get
{
if(ViewState["FsEntries"] == null)
ViewState["FsEntries"] = new string[];

return((string[])(ViewState["FsEntries"]));
}
set
{
ViewState["FsEntries"] = value;
}
}

I think using this array will solve the problem.
It's easy to "redraw" the table at Page_Load, so that the
event from the button will be properly emitted and handled.

In the code that creates the table, I would put some code
that assigns the ID of the button such that it always ends
with the row number, so that you get button IDs like :
cmdEnterDirectory0, cmdEnterDirectory1, etc.

Then from the Button_Click, you can easily find out in which
row the button was that was clicked and, by looking at the array,
what directory you wanted to enter. Then you can get the new
items and store them in the array and redraw the table based
on the new contents.

Best regards,

Eric
 
Back
Top