我是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)
在ASP.NET Web窗体中,如果您将hnd字符串作为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。请随意克隆并测试它。