2017-04-12 84 views
-1

我是ASP.NET C#的新手,我必须为学校做些什么。 我必须做电子商店。简单的事情,但我已经用桌子做。我正在生成表,但最后一个单元取决于用户登录。如果您未登录,则只有文本。当你在最后一个单元格中有一个名为“index”的文本框和一个名为“koupit”的按钮。当我点击按钮时,它会将我引导到页面,SQL代码在哪里。该代码意味着,我可以购买我选择的物品。一切都很好。我只有事件处理程序有问题。它想要一些回报,但我不知道它想要什么。带参数的动态按钮和事件处理程序

有我的代码:

namespace e_shop 
{ 
    public partial class index1 : System.Web.UI.Page 
    { 
     string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["eshop"].ToString(); 

     string id_item; 

     // label, where I will write, that there is some problem    
      private string error = ""; 
     private void Page_Load(object sender, EventArgs e) 
     { 
      { 
       //connect to DB 
       //connection string 

       //sql connect 
       SqlConnection sqlConnection = new SqlConnection(connectionString); 
       //buttons 
       if (Request.Form["puj_prihl"] != null) 
       { 
        //someone click on log in 
        string login = Request.Form["puj_login"]; 
        string password = Request.Form["puj_password"]; 
        //delete white-spaces 
        login = login.Trim(); 
        password = password.Trim(); 
        //controll on null 
        if (login.Equals("") || password.Equals("")) 
        { 
         error = "You have to write something"; 
        } 
        else 
        { 

         //try to log in 
         if (Function.userLogin(login, password, sqlConnection)) 
         { 
          //you are logged in 
         } 
         else 
         { 
          //something is bad 
          error = "Something is bad"; 
         } 
        } 
       } 
       if (Request.Form["puj_odhl"] != null) 
       { 
        //someone click on logout button 
        if (!Function.userLogout()) 
        { 
         error = "Something is bad"; 
        } 
       } 
       //div-body 
       //get everything about items 
       SqlCommand sqlCommand = new SqlCommand("SELECT * FROM [items]", sqlConnection); 
       //Open connection 
       sqlConnection.Open(); 
       //read everything 
       SqlDataReader dataReader = sqlCommand.ExecuteReader(); 
       //read rows 
       while (dataReader.Read()) 
       { 
        //new row 
        TableRow tr = new TableRow(); 
        //I need id id 
        id_item = dataReader["ID_item"].ToString(); 
        //next cells 
        TableCell tc_name = new TableCell(); 
        tc_name.Text = dataReader["name_item"].ToString(); 
        TableCell tc_popis = new TableCell(); 
        tc_popis.Text = dataReader["popis_item"].ToString(); 
        TableCell tc_category = new TableCell(); 
        tc_category.Text = dataReader["category_item"].ToString(); 
        TableCell tc_price = new TableCell(); 
        tc_price.Text = dataReader["price_item"].ToString(); 
        //cell for click 
        TableCell tc_click = new TableCell(); 
        if (!Funkce.isLoggedIn()) 
        { 
         //user isnt logged 
         tc_click.Text = "You have to log in"; 
        } 
        else 
        { 

         Button buy = new Button(); 
         buy.Text = "Buy"; 

         TextBox index = new TextBox(); 

         tc_click.Controls.Add(buy); 
         tc_click.Controls.Add(index); 
         buy.Click+= buy_Click(index, id_item); 

         } 



        //cells to row 
        tr.Cells.Add(tc_name); 
        tr.Cells.Add(tc_popis); 
        tr.Cells.Add(tc_category); 
        tr.Cells.Add(tc_price); 
        tr.Cells.Add(tc_click); 

        //row to table 
        table_eshop.Rows.Add(tr); 
       } 
       //close reader 
       dataReader.Close(); 
       //close connection 
       sqlConnection.Close(); 
       //div-login 
       literal_login.Text = "<form action=\"\" method=\"post\">\n"; 
       if (Funkce.isLoggedIn()) 
       { 

        //if we are logged in we need logout form 
        literal_login.Text += "<input type=\"submit\" name=\"puj_odhl\" value=\"Logout\" />\n"; 
        label_who.Text = "<p>Logged user: " + Function.getUserLogin(Session[Session.SessionID].ToString(), sqlConnection) + " </p>\n"; 
        my_cart.Visible = true; 
       } 
       else 
       { 
        //else we need to logg in 
        literal_login.Text += "Login: <input type=\"text\" name=\"puj_login\" /><br />\n"; 
        literal_login.Text += "Password: <input type=\"password\" name=\"puj_password\" /><br />\n"; 
        literal_login.Text += "<input type=\"submit\" name=\"puj_prihl\" value=\"Logged in\" />\n"; 

       } 
       literal_login.Text += "</form>\n"; 
       if (!chyba.Equals("")) 
       { 
        label_error.Text = "<p class=\"error\"> " + error + " </p>"; 
       } 

        } 
     } 
       //here is problem 
        private EventHandler buy_Click(TextBox index, string id_item) 
     { 

      string quantity = index.Text; 
     Response.Redirect("buy_it.aspx?id=" + id_item + "&quantity=" + quantity); 

     } 
      } 
    } 
+0

我强烈建议您编辑注释并将它们翻译为英文以便更好地理解 –

+0

如果出现错误,错误是什么以及错误发生在哪里,则完全不清楚。请明确说明。 – Seano666

+0

我希望现在它更好,一切都是英语和评论 – Any

回答

0

在ASP.NET Web窗体,按钮控制将不能,如果你来咬为html字符串来触发服务器端单击事件。

//else we need to logg in 
literal_login.Text += "Login: <input type=\"text\" name=\"puj_login\" /><br />\n"; 
literal_login.Text += "Password: <input type=\"password\" name=\"puj_password\" /><br />\n"; 
literal_login.Text += "<input type=\"submit\" name=\"puj_prihl\" value=\"Logged in\" />\n"; 

您需要使用服务器控件,并触发服务器端单击事件这样的 -

<asp:TextBox runat="server" ID="UsernameTextBox" /> 
<asp:TextBox runat="server" ID="PasswordTextBox" /> 
<asp:Button runat="server" ID="SubmitButton" Text="Submit" 
    OnClick="SubmitButton_Click" /> 

// Code Behind 
protected void SubmitButton_Click(object sender, EventArgs e) 
{ 
    string username = UsernameTextBox.Text, 
     password = PasswordTextBox.Text; 
    bool rememberMe = RememberMeCheckBox.Checked; 

    // Retrieve username and hashed password from database, and validate them 
    if (username.Equals("johndoe", StringComparison.InvariantCultureIgnoreCase) && 
     password.Equals("123456", StringComparison.InvariantCultureIgnoreCase)) 
    { 
     FormsAuthentication.RedirectFromLoginPage(username, rememberMe); 
    } 
    MessageLabel.Text = "Invalid username or password"; 
} 

我有a very simple ASP.NET Web Form Web Application at GitHub它采用FormAuthentication。请随意克隆和测试它。

+0

谢谢,但这是好的。这是来自老师,我必须像这样使用它。我真正的问题是使用Eventhadler。 – Any

相关问题