2017-04-12 27 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"; 

あなたは、サーバーコントロールを使用する必要があり、このようなトリガ、サーバー側のクリックイベント - 私はFormAuthenticationを使用していますa very simple ASP.NET Web Form Web Application at GitHubを持って

<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"; 
} 

。自由にクローンしてテストしてください。

+0

ありがとう、しかしこれは大丈夫です。これは教師からのものであり、私はこれをこのように使う必要があります。私の本当の問題はEventhadlerです。 – Any

関連する問題