mjcs100 Posted September 18, 2007 Posted September 18, 2007 (edited) 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. 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. 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 -=". 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 Edited September 18, 2007 by PlausiblyDamp Quote
IxiRancid Posted September 28, 2007 Posted September 28, 2007 Hi, I can only give you my code I use. It's intention is dynamic addition of buttons and place them into a Placeholder. 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: If Microsoft.VisualBasic.Left(Request.Form("__EVENTTARGET"), 5) = "LinkB" Then LinkB11_Click(Request.Params.Get("__EVENTARGUMENT")) End If The main Click Sub is this: 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.