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

Global variable lost in Change event Handler?

P: n/a
BACKGROUND
----------
I have an ASP.NET application having two panels. In one panel, an XML
document, transformed using xsl, is displayed. In the other panel are some
controls that allow a user to change the xml. For example, each "l_item"
element appears as a row in a table labelled with a "label"; so if I have

<l_item id="1">
<label>blah</label>
...
</l_item>

it appears on the web page as

-------------
BLAH:
...
-------------

In the other panel there is a textbox control, where the user can type a new
label and click a button, causing the xml document to change and thus the
display to change.

If a "l_item" has been changed from the original xml, I want the attribute
for that item to change. Using the above example if the user changed the
label to "blahblah" I want the xml to look like this:

<l_item id="-1">
<label>blahblah</label>
...
</l_item>

(When the user is finished, the document is sent to a database procedure
that only bothers to update records where the id is a negative number.)

CODING
------
Without going into too much detail, I have methods that change the xml
document in response to the control values and a XmlNodeChangedEventHandler
that changes the "l_item".

...//global variables
Protected selected As String
Protected selectednode As XmlNode
...

(In Page_Load)
...
A selected = Request.Form("selectedrow")
B selectednode =
xmlegend.DocumentElement.SelectSingleNode("//l_item/label='" & selected &
"']")
...

(In the label text box method)
...
C If selectednode Is Nothing Then Exit Sub
Try
D selectednode.Item("label").InnerText = label_txt.Text
Catch ex As Exception
E emessage = ex.Message
...

(In the node change handler)
If (args.Action = XmlNodeChangedAction.Change And args.Node.LocalName
<> "id") Then
F coi = selectednode.Attributes.Item(0).InnerText
If Not (coi.StartsWith("-")) Then
selectednode.Attributes.Item(0).InnerText = "-" & coi
End If
End If

PROBLEM
-------
Line F throws a "Object reference not set to an instance of an object"
exception. When debugging using VS, the debugger shows that when the page
reloads after changing the label text box, "selected" is set to the correct
value at line A and that "selectednode" becomes a valid XMLElement at line B.
At lines C and D, the debugger shows these values are still there. Flow
branches to the node change handler, coming to line F as it should. But the
debugger shows that at line F both "selected" and "selectednode" are
"Nothing". Flow goes back to the exception handler, line E, which shows
"selected" and "selectednode" are the same legitimate values they were before.
So somehow, the node change handler doesn't see the global variable values.
How can I get this to work?

Many thanks.
Nov 12 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.