By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

Class variable reverts to original value after event fires puzzler

P: n/a
Hello all,

I have a strange problem with a HtmlTable control, which is
dynamically created based on the results of a query.

Each row in the table has a LinkButton added to it. When I create the
LinkButton I also create a new Command event and set the command
argument to the primary key of the row in the database. I want every
link button I create to fire the same event, but I just need the
CommandArgument to be different:

private void buildHTMLTable()
foreach(DataRow row in drModList)
int intModID = Int32.Parse(drModList["mod_ID"].ToString());
// Sort out the link button
LinkButton lnkBtn = new LinkButton();
lnkBtn.Text = "Add detail";
lnkBtn.CommandArgument = intModID.ToString();
// Add the event
lnkBtn.Command += new CommandEventHandler(this.lnkBtn_Command);

I save the resulting generated table as a Session variable. Therefore
every time the page loads it checks to see if it already exists in the
Session object and if so, simply adds it to a placeholder.

When the user clicks on the button the event "lnkBtn_Command" fires as
expected, and the CommandArgument holds the correct primary key of the
row the user clicked, which is great.

I set the passed ID to a global variable "intSelectedModuleID" and
change the section the user is in by setting another global variable,
strCurrentSection, to a string "Edit Module".

private void lnkBtn_Command(object sender, CommandEventArgs e)
intSelectedModuleID = Int32.Parse(e.CommandArgument.ToString());
strCurrentSection = "Edit Module";

This is where the problem lies... it appears that as soon as the
program exits this function the variables intSelectedModuleID and
strCurrentSection revert back to the values they held before the
function was called. It seems like the
changes to the variables are being undone for some reason. I assume
I'm missing a step when adding the events to the page dynamically, as
I change these variables all the time using VS.NET generated events.

The variables in question both have get and set methods and are
declared outside any functions at the top of the page:

public int intSelectedModuleID
return (int) ViewState["PDDmoduleSelectedIndex"];
return 0;
ViewState["PDDmoduleSelectedIndex"] = value;

I really need to get this working before christmas so any help /
advice you could give me on this would be very much appreciated.

Oh one last thing.. for some reason I can't use the Response.Write
method in the "lnkBtn_Command" function. It crashes with the error
"Response is not available in this context". I hope that fixing one
problem will help the other!

Thank you in advance and have a very merry christmas.
Nov 15 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.