Menu

#13 CheckBoxTester doesn’t post the page back to server

open
nobody
None
5
2004-09-13
2004-09-13
ve1
No

Summary:
CheckBoxTester doesn’t post the page to server when
the Checked property is set to True.

Description:

I have a web page that had a checkbox on the asp.net
page which had its AutoPostBack property set to true.
And there was some code in the Checkbox_Changed
event handler that redirected page to some other web
page. If I was navigating the page manually, I would
just click on the checkbox and make it checked and that
posted the page back to the server. However, when I
set the Checked=true using CheckBoxTester, it did not
post the page back to the server.

Resolution:

In spite of my attempts to make this work by trying out
various things, this just didn’t work. However, when I
ran the NUnitASP unit tests which are part of the source
code, I realized that there was a test for
CheckBoxTester. When I ran the test, it passed without
a problem. This got me curious as to why the stuff was
working in the unit test that came with NUnitASP and
not in my code.

After doing careful comparison, I realized that the unit
test had a LinkButton on the web page that was testing
CheckBoxTester control. And the LinkButton.Click()
method was getting called to actually POST the form
back to server.

So after a few trial and errors I came up with following
solution:

I Added a new method for CheckBoxTester named Click
which will allow me to fire the click event as if it were a
button or a LinkButton.

And here is the simple piece of code that I had to put in
[partly taken from LinkButton source code].

/// <summary>
/// Click the button.
/// </summary>
public void Click()
{
if (!Enabled)
{
throw new
ControlDisabledException(this);
}
PostBack(Tag.Attribute
("onclick"));
}

The most important part of the code above is the
Tag.Attribute(“onclick”).

If you are facing the same problem as I did, please
modify the CheckBoxTester code by adding the above
function and explicitly call the Click method of the
CheckBoxTester control. Make sure that your
AutoPostBack is set to True.

I love the NUnitASP framework. It worked flawlessly
even with a bunch of custom controls and third party
mask controls that were used on the web site I was
working on.

Hope this helps.

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.