从Page_Load到One_Method,从One_Method到Another_Method
本文关键字:Method One Another Page Load | 更新日期: 2023-09-27 18:33:23
嘿,我用我的完整代码再问一个问题,因为我真的陷入了这一点。当页面加载时,面板上填充了从数据库中获取的书籍类别。此类别也是链接按钮。当我单击一个类别时,会在下面创建一个表格,其中包含该类别上的所有书籍。在该表中,第一个单元格填充了书籍的标题,该标题也是链接按钮。我只想在单击我时,该书的标题链接按钮以触发book_Details函数这样页面现在将只显示我选择的书。
取而代之的是,每当我单击书籍的标题链接按钮页面时,都会从 0 再次加载。
标记为:
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<div style="position: relative; top: 5%; left: 5%;">
<asp:Panel ID="MyPanel" runat="server"></asp:Panel>
</div>
<asp:MultiView ID="MultiView2" runat="server">
<asp:View ID="View1" runat="server">
<div style="overflow: auto; height: 400px;">
<asp:Table ID="ProductTBL" runat="server" BorderColor="Black" BorderStyle="Double" CellPadding="5" CellSpacing="5" BorderWidth="1px">
</asp:Table>
</div>
</asp:View>
<asp:View ID="View2" runat="server">
<asp:Table ID="detail_TBL" runat="server" BorderColor="Black" BorderStyle="Double" CellPadding="5" CellSpacing="5" BorderWidth="1px"></asp:Table>
</asp:View>
</asp:MultiView>
</asp:Content>
代码隐藏是:
protected void Page_Load(object sender, EventArgs e)
{
String conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" +
Server.MapPath("~/e-bookstoredb.accdb");
using (OleDbConnection connection = new OleDbConnection(conString))
{
connection.Open();
ыtring query = "SELECT * FROM category";
using (OleDbCommand cmd = new OleDbCommand(query, connection))
{
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string literal = (string)reader["Name"];
LinkButton lnk_button = new LinkButton();
lnk_button.Text = literal;
lnk_button.ID = "cat_B" + reader["ID"].ToString();
lnk_button.CommandArgument = reader["ID"].ToString();
lnk_button.CommandName = reader["ID"].ToString();
lnk_button.Command += new CommandEventHandler(books_Show);
MyPanel.Controls.Add(lnk_button);
MyPanel.Controls.Add(new LiteralControl("</br>"));
}
reader.Close();
}
connection.Close();
}
}
protected void books_Show(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
string cat = lnk.CommandArgument;
MultiView2.ActiveViewIndex = 0;
string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" + Server.MapPath("~/e-bookstoredb.accdb");
using (OleDbConnection con = new OleDbConnection(ConStr))
{
con.Open();
string query = "SELECT * FROM product WHERE category = @cat";
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
cmd.Parameters.AddWithValue("@cat", cat);
OleDbDataReader reader = cmd.ExecuteReader();
TableCell cell;
TableRow row = new TableRow();
TableCell titleCell = new TableCell();
titleCell.Text = "Τίτλος";
TableCell desCell = new TableCell();
desCell.Text = "Περιγραφή";
TableCell priceCell = new TableCell();
priceCell.Text = "Τιμή";
row.Cells.Add(titleCell);
row.Cells.Add(desCell);
row.Cells.Add(priceCell);
ProductTBL.Rows.Add(row);
LinkButton book_button;
while (reader.Read())
{
book_button = new LinkButton();
book_button.ID = "book" + reader["ID"].ToString();
book_button.Text = (string)reader["Title"];
book_button.CommandArgument = (string) reader["Title"];
book_button.CommandName = "cmd" + reader["ID"].ToString();
book_button.Command += new CommandEventHandler(book_Details);
row = new TableRow();
cell = new TableCell();
cell.Controls.Add(book_button);
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = (string)reader["Description"];
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = reader["price"].ToString()+"€";
row.Cells.Add(cell);
ProductTBL.Rows.Add(row);
}
reader.Close();
}
con.Close();
}
}
protected void book_Details(object sender, EventArgs e)
{
MultiView2.ActiveViewIndex = 1;
LinkButton lnk = sender as LinkButton;
String bookTitle = lnk.CommandArgument;
//...And then I just create a table to show only the book user selected
//...this table gets filled buy this query = " SELECT * FROM product WHERE Title=@bookTitle
}
您必须
使用if(!IsPostBack)
将代码包装在Page_Load
中,如下所示。这是因为根据页面生命周期 ASP.Net 首先触发Page_Load然后控制事件。
因此,每当您单击LinkButtons
时,服务器首先调用Page_Load,然后调用LinkButton
的单击事件。
因此,最好将Page_Load
代码包装在只想在此类Page_Load
执行一次的位置。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Your Page_Load logic goes here
}
}
根据MSDN,IsPostBack
的定义是
获取一个值,该值指示是否正在为首次加载或正在加载以响应回发。
如果正在加载页面以响应客户端回发,则为 true;否则,为假。
希望这有帮助!