嘿,我用完整的代碼提出了一個更多的問題,因爲我在這一點上真的被困住了。 當頁面加載面板時填充了從數據庫中取得的書籍類別。這個類別也是鏈接按鈕。當我點擊一個類別時,將在下面創建一個表格,其中包含該類別的所有書籍。 在該表格中,第一個單元格充滿了書籤標題,它也是Linkbutton。 我只是想當我點擊那本書的標題linkbutton來觸發book_Details函數 ,以便頁面現在只顯示我選擇的書。從Page_Load到One_Method和從One_Method到Another_Method
取而代之的是,每當我再次點擊從0
的標記books'title LinkButton的頁面加載是:
<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 [email protected]
}
非常感謝男士!但是,當第二種方法(book_details)被解僱時,我想通過正確的參數來顯示正確的書。如何在page_load方法中使用該參數。就像我在books_show方法中做的一樣? – Kostis 2014-12-07 22:35:37
爲什麼在Page_Load事件中需要它?如果(Session [「method」] ==「showbookdetails」) { {book_detail(sender,e);}我可以說在例如page_load – Sam 2014-12-07 22:40:21
。 } 但我怎麼能跟我爭論? – Kostis 2014-12-07 22:50:18