Help with form submit buttons and code behind

mjcs100

Newcomer
Joined
Feb 19, 2006
Messages
7
Hi I was wandering if someone could help me with a problem I'm having with my code behind for my submit buttons. What I did was create an html form in Dreamweaver and I created the code behind in Visual Studio 2005 to work with it. I have created a couple of applications in the past like this to work with a couple of other webpages I have but without the submit buttons being used this way and they work fine. Now I have 3 different html pages with the same thing, 2 forms and a submit button with the text "Download" for each one but has a different "ID". All the buttons are calling the same code behind. In the code behind, I want to have it distinguish which button was clicked by the "ID". I created a dummy in Visual Studio on the .aspx page with the buttons and it works but when I use the html page for it, and delete the info on the .aspx page, it won't post back to the html. I tried a couple of different ways to get the page_load event to recognize the html form, but been having no luck. Here is one of the codes I am trying to use, and I was wandering if someone could help me figure out what I am doing wrong.

The html page's button's names and the buttons in visual studio are the exact same.
C#:
protected void Page_Load(object sender, EventArgs e)
    {
            btn1.Click += new System.EventHandler(this.Download);
            btn2.Click += new System.EventHandler(this.Download);
            btn3.Click += new System.EventHandler(this.Download);
            btn4.Click += new System.EventHandler(this.Download);
            btn5.Click += new System.EventHandler(this.Download);
            btn6.Click += new System.EventHandler(this.Download);
            btn7.Click += new System.EventHandler(this.Download);
            btn8.Click += new System.EventHandler(this.Download);
    }

    protected void Download(object sender, System.EventArgs e)
    {
        switch (((Button)sender).OnClientClick)
        {
            case "btn1":
                //code for btn1.
		//code to updateDataBase
                break;
            case "btn2":
                //code for btn2.
 		//code to updateDataBase
                break;
            case "btn3":
                //code for btn3
		//code to updateDataBase
                break;
            case "btn4":
                //code for btn4
		//code to updateDataBase
                break;
            case "btn5":
                //code for btn5
		//code to updateDataBase
                break;
            case "btn6":
                //code for btn6
		//code to updateDataBase
                break;
            case "btn7":
                //code for btn7
		//code to updateDataBase
                break;
            case "btn8":
                //code for btn8
		//code to updateDataBase
                break;
        }

        lblStatus.Text = dlID.ToString();
    }
    private void UpdateDataBase(object sender)
    {
        try
        {
            dbConn.ConnectionString = //code to access mssql database
            dbConn.Open();
            dbCommand.Connection = dbConn;
            dbCommand.CommandText = "SELECT * FROM table WHERE column='" + btnClicked + "'";
            dbReader = dbCommand.ExecuteReader();

            if (dbReader.Read())
            {
                if (btnClicked == dbReader["column"].ToString())
                {
                    dlID = Convert.ToInt32(dbReader["column"]);

                    dlID++;

                    dbConn.Close();

                    try
                    {
                        dbConn.ConnectionString = //code to access mssql database
                        dbConn.Open();
                        dbCommand.Connection = dbConn;
                        dbCommand.CommandText = "UPDATE table SET acolumn= '" + dlID + "' WHERE acolumn = '" + btnClicked + " '";
                        dbReader = dbCommand.ExecuteReader();

                        lblStatus.Text = dlID.ToString();
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
            }

        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }

        finally
        {
            dbConn.Close();
        }
    }
____________________________________________________________

This is the second way I tried it and no luck with this one.
C#:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        }
        else
        {
            lblStatus.Text = dlID.ToString();
        }
    }

protected void btn1_Click(object sender, EventArgs e)
    {
        code for btn1
    }
protected void btn2_Click(object sender, EventArgs e)
    {
        code for btn2
    }
protected void btn3_Click(object sender, EventArgs e)
    {
        code for btn3
    }
protected void btn4_Click(object sender, EventArgs e)
    {
        code for btn4
    }
protected void btn5_Click(object sender, EventArgs e)
    {
        code for btn5
    }
protected void btn6_Click(object sender, EventArgs e)
    {
        code for btn6
    }
protected void btn7_Click(object sender, EventArgs e)
    {
        code for btn7
    }
protected void btn8_Click(object sender, EventArgs e)
    {
        code for btn8
    }

private void UpdateDataBase(object sender)
    {
        try
        {
            dbConn.ConnectionString = //code to access mssql database
            dbConn.Open();
            dbCommand.Connection = dbConn;
            dbCommand.CommandText = "SELECT * FROM table WHERE column='" + btnClicked + "'";
            dbReader = dbCommand.ExecuteReader();

            if (dbReader.Read())
            {
                if (btnClicked == dbReader["column"].ToString())
                {
                    dlID = Convert.ToInt32(dbReader["column"]);

                    dlID++;

                    dbConn.Close();

                    try
                    {
                        dbConn.ConnectionString = //code to access mssql database
                        dbConn.Open();
                        dbCommand.Connection = dbConn;
                        dbCommand.CommandText = "UPDATE table SET acolumn= '" + dlID + "' WHERE acolumn = '" + btnClicked + " '";
                        dbReader = dbCommand.ExecuteReader();

                        lblStatus.Text = dlID.ToString();
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
            }

        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }

        finally
        {
            dbConn.Close();
        }
    }
______________________________________________________

This is my last way I have tried, but it keeps telling me "The event 'System.Web.UI.WebControls.Button.Click' can only appear on the left hand side of += or -=".
C#:
protected void Page_Load(object sender, EventArgs e)
    {
        if (btn1.Click = true)
        {
            code for btn1
        }
        else if (btn2.Click = true)
        {
            code for btn2
        }
        else if (btn3.Click = true)
        {
            code for btn3
        }
        else if (btn4.Click = true)
        {
            code for btn4
        }
        else if (btn5.Click = true)
        {
            code for btn5
        }
        else if (btn6.Click = true)
        {
            code for btn6
        }
        else if (btn7.Click = true)
        {
            code for btn7
        }
        else if (btn8.Click = true)
        {
            code for btn8
        }
   }

private void UpdateDataBase(object sender)
    {
        try
        {
            dbConn.ConnectionString = //code to access mssql database
            dbConn.Open();
            dbCommand.Connection = dbConn;
            dbCommand.CommandText = "SELECT * FROM table WHERE column='" + btnClicked + "'";
            dbReader = dbCommand.ExecuteReader();

            if (dbReader.Read())
            {
                if (btnClicked == dbReader["column"].ToString())
                {
                    dlID = Convert.ToInt32(dbReader["column"]);

                    dlID++;

                    dbConn.Close();

                    try
                    {
                        dbConn.ConnectionString = //code to access mssql database
                        dbConn.Open();
                        dbCommand.Connection = dbConn;
                        dbCommand.CommandText = "UPDATE table SET acolumn= '" + dlID + "' WHERE acolumn = '" + btnClicked + " '";
                        dbReader = dbCommand.ExecuteReader();

                        lblStatus.Text = dlID.ToString();
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
            }

        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }

        finally
        {
            dbConn.Close();
        }
    }
I hope someone will be able to help me. Thanks
 
Last edited by a moderator:
Hi,
I can only give you my code I use.
It's intention is dynamic addition of buttons and place them into a Placeholder.

Code:
Dim LinkB11 As New LinkButton
Dim oCnt As Integer = 1
LinkB11.Text = oCnt
LinkB11.ID = "LinkB" & oCnt
LinkB11.Attributes.Add("href", "javascript:__doPostBack('LinkB11','" & Trim(drSELECT1(1)) & "/" & Trim(drSELECT1(0)) & "')")

PlaceHolder1.Controls.Add(LinkB11)
oCnt += 1

And then in Page_Load event this code:
Code:
If Microsoft.VisualBasic.Left(Request.Form("__EVENTTARGET"), 5) = "LinkB" Then
LinkB11_Click(Request.Params.Get("__EVENTARGUMENT"))
End If

The main Click Sub is this:
Code:
Private Sub LinkB11_Click(ByVal mFf As String)
Response.Redirect("http://localhost/retailforms/subweb/" & Trim(mFf) & ".aspx")
End Sub

Don't know how this would act if you had premade HTML Buttons on the page. But perhaps it can help you catch the Button Click event.
 
Back
Top