简体   繁体   English

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

[英]Dynamic Buttons are not triggering in aspx.cs page

I have recently started making a mock webstore for an assignment and have unfortunately hit a road bump over a button. 我最近开始制作一个虚拟的网上商店来做作业,但不幸的是,在按钮上遇到了麻烦。 I have tried MULTIPLE ways to implement a button that would trigger a function via my server. 我尝试了多种方法来实现一个按钮,该按钮将通过我的服务器触发功能。 However, I cannot get the button to trigger via the server side no matter how I implement a button. 但是,无论我如何实现按钮,都无法通过服务器端触发按钮。 Below is my code for my aspx.cs page. 以下是我的aspx.cs页面的代码。 Note the AddPanel() function is where I am trying to add each panel to my webpage with the corresponding css. 请注意,AddPanel()函数是我尝试将每个面板与相应的CSS添加到我的网页的地方。 If yall need the css as well I'm more than happy to provide it. 如果你们也需要CSS,那么我很乐意提供它。 Any help is appreciated. 任何帮助表示赞赏。 I also kept all the comment's of variations I have tried to get my button to work. 我还保留了所有尝试使按钮起作用的变化的注释。

Homepage.aspx.cs 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
    }
}

If you create the button by c# code behind, You can not add this by html Codes! 如果使用c#代码创建按钮,则无法通过html代码添加按钮! To adding button created to the panel you need to add this: 要将创建的按钮添加到面板,您需要添加以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM