单击“搜索按钮”后,我尝试创建动态按钮。但是我总是返回主页并且不进入该BtnBuy_ServerClick功能。GetPostBackControl() - 如果没有找到控件,则返回 null,否则返回控件。
见最后按钮的添加和创建。
我很高兴在这个问题上有任何帮助。
谢谢!
public partial class HomePageDesignaspx : System.Web.UI.Page
{
private static List<Button> buttonList;
protected void Page_Load(object sender, EventArgs e)
{
if (buttonList != null)
{
for (int i = 0; i < buttonList.Count(); i++)
{
buttonList[i].Click += new EventHandler(this.BtnBuy_ServerClick);
}
}
else
{
buttonList = new List<Button>();
}
Control c = GetPostBackControl(Page);
if (c != null && c.ID == "SearchBtn")
{
DataBase db = DataBase.Instance;
flights = db.GetFlights(companyValue, sourceValue, destinationValue, date, minPriceS, maxPriceS);
for (int i = 0; i < flights.Count; i++)
{
if (i % 3 == 0)
{
HtmlGenericControl drow = new HtmlGenericControl("div");
drow.ID = "drow" + i.ToString();
drow.Attributes.Add("class", "row");
this.flightTable.Controls.Add(drow);
}
HtmlGenericControl dCol = new HtmlGenericControl("div");
dCol.ID = "dCol" + i.ToString();
dCol.Attributes.Add("class", "col-sm-4");
this.flightTable.Controls.Add(dCol);
HtmlGenericControl dCard = new HtmlGenericControl("div");
dCard.ID = "dCard" + i.ToString();
dCard.Attributes.Add("class", "card-section");
dCol.Controls.Add(dCard);
HtmlGenericControl dCardImg = new HtmlGenericControl("div");
dCardImg.ID = "dCardImg" + i.ToString();
dCardImg.Attributes.Add("class", "card-section-image");
dCard.Controls.Add(dCardImg);
HtmlGenericControl CardImg = new HtmlGenericControl("img");
CardImg.ID = "CardImg" + i.ToString();
CardImg.Attributes.Add("src", "../Images/plane2.jpg");
dCardImg.Controls.Add(CardImg);
HtmlGenericControl dCardDesc = new HtmlGenericControl("div");
dCardDesc.ID = "dCardDesc" + i.ToString();
dCardDesc.Attributes.Add("class", "card-desc");
dCard.Controls.Add(dCardDesc);
HtmlGenericControl dCardTitle = new HtmlGenericControl("div");
dCardTitle.ID = "dCardTitle" + i.ToString();
dCardTitle.Attributes.Add("class", "title");
dCardDesc.Controls.Add(dCardTitle);
HtmlGenericControl H5Title = new HtmlGenericControl("h5");
H5Title.ID = "H53Title" + i.ToString();
H5Title.Attributes.Add("class", "card-desc");
H5Title.InnerText = "Num flight is " + flights[i].Id;
dCardTitle.Controls.Add(H5Title);
HtmlGenericControl dCardInfo = new HtmlGenericControl("div");
dCardInfo.ID = "dCardInfo" + i.ToString();
dCardInfo.Attributes.Add("class", "card-info");
dCardDesc.Controls.Add(dCardInfo);
HtmlGenericControl UlList = new HtmlGenericControl("ul");
UlList.ID = "UlList" + i.ToString();
UlList.Attributes.Add("class", "list-unstyle");
dCardInfo.Controls.Add(UlList);
HtmlGenericControl LiDate = new HtmlGenericControl("li");
LiDate.ID = "LiDate" + i.ToString();
UlList.Controls.Add(LiDate);
HtmlGenericControl iDate = new HtmlGenericControl("i");
iDate.ID = "iDate" + i.ToString();
iDate.Attributes.Add("class", "far fa-calendar-alt");
LiDate.Controls.Add(iDate);
HtmlGenericControl LabelDate = new HtmlGenericControl("label");
LabelDate.ID = "LabelDate" + i.ToString();
LabelDate.InnerText = "Date: " + flights[i].DateFlight;
LiDate.Controls.Add(LabelDate);
HtmlGenericControl LiPlace = new HtmlGenericControl("li");
LiPlace.ID = "LiPlace" + i.ToString();
UlList.Controls.Add(LiPlace);
HtmlGenericControl iPlace = new HtmlGenericControl("i");
iPlace.ID = "iPlace" + i.ToString();
iPlace.Attributes.Add("class", "fas fa-map-marker-alt");
LiPlace.Controls.Add(iPlace);
HtmlGenericControl LabelPlace = new HtmlGenericControl("label");
LabelPlace.ID = "LabelPlace" + i.ToString();
LabelPlace.InnerText = "Place: " + flights[i].Source + "->" + flights[i].Destination;
LiPlace.Controls.Add(LabelPlace);
HtmlGenericControl LiPrice = new HtmlGenericControl("li");
LiPrice.ID = "LiPrice" + i.ToString();
UlList.Controls.Add(LiPrice);
HtmlGenericControl iPrice = new HtmlGenericControl("i");
iPrice.ID = "iPrice" + i.ToString();
iPrice.Attributes.Add("class", "fas fa-hand-holding-usd");
LiPrice.Controls.Add(iPrice);
HtmlGenericControl LabelPrice = new HtmlGenericControl("label");
LabelPrice.ID = "LabelPrice" + i.ToString();
LabelPrice.InnerText = "Price: " + flights[i].Price;
LiPrice.Controls.Add(LabelPrice);
Button BtnBuy = new Button();
BtnBuy.ID = flights[i].Id;
BtnBuy.Attributes["class"] = "cart_btn btn btn-dark";
BtnBuy.Attributes.Add("type", "submit");
BtnBuy.Text = "Buy Now";
buttonList.Add(BtnBuy);
BtnBuy.Click += new EventHandler(this.BtnBuy_ServerClick);
UlList.Controls.Add(BtnBuy);
}
}
}
}
void BtnBuy_ServerClick(object sender, EventArgs e)
{
string buttonId = ((HtmlButton)sender).ID;
Session["IdFlight"] = buttonId;
Server.Transfer("~/WebPage/BuyFlight.aspx");
}
ASPX 标记在homePage.aspx:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<section class="card">
<div class="row" id="flightTable" runat="server">
<!--Place for tickets after the search-->
</div>
</section>