August 2004 - Posts

Execute javascript and perform postback from the same ASP.NET button

Another common question:

How can we execute both client script function and server side event from the same ASP.NET Button server control?

Button server control rendered as <input type=“submit”> html element. It submits the form and executes server side Click event handler on each click, mousedown or “Enter” pressed on form. Sometimes button controls render javascript in onlick event to perform validation tasks (if validator controls were droped on page). Knowing this, we don't want to interfere with validation or forms submit. The simplest way to execute client script before submit, is to attach scriptto mousedown event. You have to initiate button click after script execution to continue with submit:

<script language=“javascript“>
function doSomething()
{
   // Do some thing usefull ...
   // Continue with submit
   this.click();
}
</script>
<asp:button onmousedown=“doSomething()“ id=“Button1” Text="Button" runat="server"></asp:button>

This button will execute client side doSomething function and proceed to server to fire Button1_Click event.

Adding controls to ASP.NET page at runtime

We add control dynamically, attach event handler to it's event and nothing happened. What is wrong?

It is common question asked again and again. Adding controls to ASP.NET WebForm at runtime is a simple task:
private void Page_Load(object sender, System.EventArgs e)
{
    Button newButton = new Button();
    newButton.Text = "New Button";
    newButton.ID = "newButton";
    newButton.Click += new System.EventHandler(this.newButton_Click);

    WebUserControl1 newUserControl = (WebUserControl1)LoadControl("WebUserControl1.ascx");
    newUserControl.ID = "newUserControl";

    this.PlaceHolder.Add(newUserControl);
    this.PlaceHolder.Add(newButton);
}

Problem

Now let's raise a bet little. What if we want to add new control as a result of some user action (button click for example)? We are moving the same code from Page_Load to Button_Click event and… Here troubles begin. Our controls will show as expected, but only first time. Any postback will reset page to its original state. Dynamically created controls will be unable to fire any event. What is happening here? The answer is simple. Page class is stateless. It is destroyed after rendering. Page recreates child controls collection, based on tags in aspx file, then postback data can be handled and event handlers attached (in OnInit event). Controls we just created dynamically do not exist in aspx file; page has no knowledge about them.

Solution

Solution is also simple. We have to recreate controls on load stage of page lifecycle. After it's done, each control can handle his postback data, retrieve saved viewstate, raise events etc.

Now code skeleton will look like this:

private void Page_Load(object sender, System.EventArgs e)
{
    RecreatePersistedControls();
}
private void Button_Click(object sender, System.EventArgs e)
{
    CreateControl("newControl");
    PersistControl("newControl");
}
private void RecreatePersistedControls()
{
    // Call CreateControl for each persisted control
}
private void CreateControl(string id)
{
    // Create controll with specified id,
    // add it to controls collection, attach event handlers
}
private void PersistControl(string id)
{
    // Persist specified id (in session for example)
}

See fully functional example at Code Project

'Stop Debugging' option in VS.NET does not stops code execution

Behavior
There are cases, when program execution continues after 'Stop debugging' option (menu or toolbar) selected in Visual Studio .NET. 

Steps to reproduce behavior
1. Create default ASP.NET VB application
2. Add WebForm
3. Add Button to WebForm
4. Add following code to WebForm (you will, probably, have to update connection string):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                    Handles Button1.Click
   Dim cn As New SqlClient.SqlConnection("server=.;uid=sa;pwd=;initial catalog=Northwind;")
   cn.Open()
   Dim cmd As New SqlClient.SqlCommand("update categories set categoryname=categoryname+'1' where categoryid=1", cn)
   cmd.ExecuteNonQuery()
End Sub
5. Set breakpoint on first line of  Button1_Click and run the program.
6. Hit 'Stop Debugging' button immediately after debugger reach breakpoint.
7. Inspect your database. In most cases it will be updated!

Explanation
Turns out that VS.NET debugger does not suppose to halt managed process execution when you 'Stop Debugging'. It is just detaches from process. With unmanaged code, this requires killing process.

Solution
If you will check "Unmanaged code debugging' option in Project>Properties>Configuration Properties>Debugging, execution will stop immediately, when 'Stop Debugging' selected.