C# 动态按钮未在aspx.cs页面中触发

C# 动态按钮未在aspx.cs页面中触发,c#,asp.net,C#,Asp.net,我最近开始为一项作业制作一个模拟网络商店,不幸的是,我在路上碰到了一个按钮。我尝试了多种方法来实现一个按钮,通过我的服务器触发一个功能。但是,无论我如何实现按钮,我都无法通过服务器端触发按钮。下面是我的aspx.cs页面代码。注意AddPanel()函数是我试图用相应的css将每个面板添加到我的网页中的函数。如果你也需要css,我很乐意提供。感谢您的帮助。我还保留了所有我试图让我的按钮工作的变体的评论 Homepage.aspx.cs using System; using System.Col

我最近开始为一项作业制作一个模拟网络商店,不幸的是,我在路上碰到了一个按钮。我尝试了多种方法来实现一个按钮,通过我的服务器触发一个功能。但是,无论我如何实现按钮,我都无法通过服务器端触发按钮。下面是我的aspx.cs页面代码。注意AddPanel()函数是我试图用相应的css将每个面板添加到我的网页中的函数。如果你也需要css,我很乐意提供。感谢您的帮助。我还保留了所有我试图让我的按钮工作的变体的评论

Homepage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Homepage : System.Web.UI.Page
{
private List<Item> theItems;
protected void Page_Load(object sender, EventArgs e)
{
    if(theItems == null)
    {
        theItems = new List<Item>();
    }
    Item it = new Item("img/demo/1.jpg", "img/demo/thumb1.jpg", "Hard Drive 500GB", "Nothing but the highest quality of hard drives.", 600.50, "#1", 2);
    theItems.Add(it);
    it = new Item("img/demo/2.jpg", "img/demo/thumb2.jpg", "External Blu Ray", "You like blu ray? We can play Blu ray.", 200.00, "#2", 8);
    theItems.Add(it);
    it = new Item("img/demo/3.jpg", "img/demo/thumb3.jpg", "Fancy Phone", "Wanna call someone? You need a phone to do it!", 499.99, "#3", 2);
    theItems.Add(it);
    it = new Item("img/demo/4.jpg", "img/demo/thumb4.jpg", "Throwable Frisbee", "Who needs a laptop when you have a frisbee!", 2000.99, "#4", 2);
    theItems.Add(it);
    it = new Item("img/demo/5.jpg", "img/demo/thumb5.jpg", "Test Div", "This is a test div yo lets hope it works", 5.01, "#5", 5);
    theItems.Add(it);
    AddPanel();
}

//public void AddPanel(string imgSrc, string thmbSrc, string label, string description, string href, double price)
public void AddPanel()
{
    int count = 1;
    Button btn = new Button();
    foreach(Item item in theItems)
    {

        string btnID = "button" + count;
        btn.ID = btnID;
        btn.Text = "Add To Cart";
        btn.Click += new EventHandler(addToCart);

        string pane = @"<div><img src=""" + item.ImgSrc + @""" alt=""""><h5>" + item.Label + "</h5><p>" + item.Description + "</p>";
        pane += @"<p style=""text-align: right; margin-right: 16px""><a href = ""#"" class=""button"">Add to Cart</a></p></div>";
        //pane += @"<p style=""text-align: right; margin-right: 16px"">""</p></div>";
        //pane += @"<p style=""text-align: right; margin-right: 16px"">< asp:Button ID = """+btnID+@""" runat = ""server"" Text = ""Add To Cart"" /></p></div>";
        //pane += @"<p style=""text-align: right; margin-right: 16px""><input type=""button"" id="""+ btnID + @""" runat=""server"" text=""Add To Cart"" onServerClick=""addToCart()""></button></p></div>";
            //pane += @"<p style=""text-align: right; margin-right: 16px""><a id="""+btnID+ @""" href = """" class=""button"" runat=""server"" onServerClick=""addToCart"">Add to Cart</a></p></div>";
            this.panes.InnerHtml += pane;
            string paneNav = @"<li><a href=""" + item.Href + @"""><img src=""" + item.ThmbNailSrc + @""" alt=""""> <strong>" + item.Label + "</strong>" + "$" + item.Price + "</a></li>";
            this.innerPaneNav.InnerHtml += paneNav;
            count++;
        }

    }

    public void addToCart(object sender, EventArgs e)
    {
        string s;
    s = "It worked!"; // this is where I have a breakpoint to see if the     code was triggered
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类主页:System.Web.UI.Page
{
私人名单;
受保护的无效页面加载(对象发送方、事件参数e)
{
if(items==null)
{
items=新列表();
}
项目it=新项目(“img/demo/1.jpg”、“img/demo/thumb1.jpg”、“硬盘500GB”、“只有最高质量的硬盘。”,600.50,“#1”,2);
添加(它);
它=新项目(“img/demo/2.jpg”,“img/demo/thumb2.jpg”,“外部蓝光”,“你喜欢蓝光吗?我们可以播放蓝光。”,200.00,“#2”,8);
添加(它);
它=新项目(“img/demo/3.jpg”、“img/demo/thumb3.jpg”、“花式电话”、“想给某人打电话吗?你需要一部电话才能打电话!”,499.99,“#3”,2);
添加(它);
它=新项目(“img/demo/4.jpg”、“img/demo/thumb4.jpg”、“可扔掉的飞盘”、“当你有飞盘时谁需要笔记本电脑!”,2000.99,“4”,2);
添加(它);
它=新项目(“img/demo/5.jpg”、“img/demo/thumb5.jpg”、“Test Div”、“这是一个测试Div,希望它能工作”,5.01,“#5”,5);
添加(它);
AddPanel();
}
//public void AddPanel(字符串imgSrc、字符串thmbSrc、字符串标签、字符串描述、字符串href、双倍价格)
公共面板()
{
整数计数=1;
按钮btn=新按钮();
foreach(EMS中的项目)
{
字符串btnID=“按钮”+计数;
btn.ID=btnID;
btn.Text=“添加到购物车”;
btn.Click+=neweventhandler(addToCart);
字符串窗格=@“+item.Label+”“+item.Description+”

”; 窗格+=@“

”; //窗格+=@“

”; //窗格+=@“

”; //窗格+=@“

”; //窗格+=@“

”; this.panes.InnerHtml+=窗格; 字符串paneNav=@“
  • ”; this.innerPaneNav.InnerHtml+=paneNav; 计数++; } } public void addToCart(对象发送方,事件参数e) { 字符串s; s=“成功!”;//这里有一个断点,可以查看代码是否被触发 } }
    如果您使用c#代码隐藏创建按钮,则无法使用html代码添加此按钮!要将创建的按钮添加到面板,您需要添加以下内容:

    btn.Click += new EventHandler(addToCart); // your code
    pane.Controls.Add(btn); //adding created button to server side control (pane)
    

    是的,我也尝试过,但当我将按钮添加到控件时,我发现一个错误,“System.Web.HttpException”类型的异常发生在System.Web.dll中,但未在用户代码中处理。其他信息:无法获取窗格的内部内容,因为内容不是文字。“窗格是什么?”?它不在您的代码中。错误表示无法获取窗格的内部内容。因此,我认为问题在于这行:
    this.panes.InnerHtml+=pane
    您可以轻松地使用带有属性
    runat=“server”
    div
    元素,并向其添加html文本或c#控件。