[英]Dynamically adding controls and eventhandlers (asp.net c#)
我是计算机基础一年级的学生,正在为uni的一个项目设计一个基本的电子商务网站,但遇到了一个问题,我不得不将头撞在桌子上
所以..
我根据会话变量(类型字典-关键字是产品ID,值是数量)中的内容,在购物篮页面的Page_Load方法中使用asp.net控件动态创建购物篮。
我可以从浏览页面添加项目,该页面根据用户放入数据库的products表中的内容进行了动态添加的控件,效果很好。
我可以在篮子中填入所有详细信息,但是正在用“-”和“ +”按钮来更改篮子的数量。
我可以分配事件处理程序来运行一个类中的方法,该类一次添加(或删除)1个项目,但是我面临的问题是,如果将代码放在Page_Load中,该函数可以工作,但在事件处理程序触发前呈现控件(因此,当字典更新时,它不会显示新值-您必须刷新或添加另一个值,然后始终落后1)
如果我将代码放在PreRender中,则事件处理程序不会触发。
这是我在ASP.NET中的第一个项目,因此,如果我用自己的方法弄错了树,请放轻松。
任何想法或正确方向的推动都将不胜感激
提前谢谢了
编辑添加更多细节
//creating my button
Button tdQtyDownButton = new Button();
tdQtyDownButton.ID = "qtyDown"+ row["prod_id"]; tdQtyDownButton.Text = "-";
tdQtyDownButton.Click += delegate (object sender1, EventArgs e1) {
ShoppingBasket.AddItem((int)row["prod_id"]); };
tdQtyDown.Controls.Add(tdQtyDownButton);
//in seperate ShoppingBasket class file
public static void AddItem(int prod_id)
{
if (HttpContext.Current.Session["basket"] != null)
{
if(!((Dictionary<int, int>)HttpContext.Current.Session["basket"]).ContainsKey(prod_id))
{
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).Add(prod_id, 1);
}
else
{
int currentQty = 0;
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).TryGetValue(prod_id, out currentQty);
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).Remove(prod_id);
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).Add(prod_id, currentQty + 1);
}
}
else
{
CreateBasket();
AddItem( prod_id);
}
}
正如我所说的,这是可行的-我认为这只是一个生命周期问题,可能需要一种全新的方法
对于Windows窗体应用程序
public partial class Form1 : Form
{
int i = 1;
int j = 1;
int rbCount = 0;
public Form1()
{
InitializeComponent();
}
private void buttonAddRadio_Click(object sender, EventArgs e)
{
RadioButton rb = new RadioButton();
rb.Name = "Radio Button" + i.ToString();
rb.Text = "Radio Button" + i;
rb.Left = 8;
rb.Top = 15 + (rbCount * 27);
rb.AutoSize = true;
rb.Click += new EventHandler(radio_click);
groupBox1.Controls.Add(rb);
i++;
rbCount++;
}
void radio_click(object sender, EventArgs e)
{
MessageBox.Show(((RadioButton)sender).Text);
}
private void buttonAddCheck_Click(object sender, EventArgs e)
{
CheckBox cb = new CheckBox();
cb.Name = "CheckBox" + j.ToString();
cb.Text = "CheckBox" + j;
cb.Left = 8;
cb.Top = 15 + (rbCount * 27);
cb.AutoSize = true;
cb.Click += new EventHandler(checkbox_checked);
groupBox1.Controls.Add(cb);
j++;
rbCount++;
}
void checkbox_checked(object sender, EventArgs e)
{
MessageBox.Show(((CheckBox)sender).Text);
}
}
对于ASP.NET Web应用程序
string[] myArray = new string[] { "Alex", "Bob", "John", "Srinivas", "Zamal", "Rahul" }
foreach (string item in myArray)
{
HyperLink myHyp = new HyperLink();
myHyp.Text = Suspect;
myHyp.NavigateUrl = "User Details.aspx?name=" + HttpUtility.UrlEncode(item));
myPanel.Controls.Add(new LiteralControl("<ul>"));
myPanel.Controls.Add(new LiteralControl("<li>"));
myPanel.Controls.Add(hpSuspect);
myPanel.Controls.Add(new LiteralControl("</li>"));
myPanel.Controls.Add(new LiteralControl("</ul>"));
}
NB LiteralControl用于添加常规HTML控件。
假设您要动态添加一个Button
并处理它的click事件:
Button button1 = new Button();
button1.Text = "dynamic button";
button1.Left = 10; button1.Top = 10;
textBox1.Click += new EventHandler(btn_click);
this.Controls.Add(button1);
private void btn_click()
{
}
更新:从您的评论看来,您似乎想刷新页面而不重新加载页面...为此,最简单的方法是SignalR或可以使用UpdatePanel控件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.