无法在动态创建的asp.net按钮中触发事件

时间:2016-06-23 21:18:23

标签: c# asp.net

我是asp.net的初学者。我现在正在动态创建asp.net按钮。但我必须编写代码,在我的Page_load旁边创建按钮,按钮点击事件(myEvent)不要发生。如何修复我的代码?我希望我的按钮在点击时触发操作。感谢您的注意。

这是我的cs文件。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadDropdownListHere();
            prepare_ddl();
            load_view("bathroom");


        }


    }
        private void prepare_ddl()
    {
        string str = "data source=.;initial catalog=gusto;integrated security=true";
        SqlConnection con = new SqlConnection(str);

        DataTable ddl_table = new DataTable();
        List<String> category = new List<String>();

        System.Data.SqlClient.SqlCommand cmd = null;
        cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select * from category";

        //getting data start
        try
        {
            con.Open();

            SqlDataAdapter da = null;
            using (da = new SqlDataAdapter(cmd))
            {
                da.Fill(ddl_table);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Dispose();
            cmd = null;
            con.Close();
        }

        foreach (DataRow row in ddl_table.Rows)
        {
            category.Add(row["name"].ToString());
         }
        //getting data end

        ddl.DataSource = category;
        ddl.DataBind();
    }
    private void load_view(String type)
    {

        DataTable item_table = new DataTable();

        string str = "data source=.;initial catalog=gusto;integrated security=true";
        SqlConnection con = new SqlConnection(str);
        System.Data.SqlClient.SqlCommand cmd = null;
        cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select * from item where category='"+type+"'";

//SqlParameter param = new SqlParameter("@category",type);
  //      cmd.Parameters.Add(param);

        try
        {
            con.Open();

            SqlDataAdapter da = null;
            using (da = new SqlDataAdapter(cmd))
            {
                da.Fill(item_table);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Dispose();
            cmd = null;
            con.Close();
        }

        foreach (DataRow row in item_table.Rows)
        {


  string inner_content = "<div class='mid'>" + row["name"].ToString() + "</div><center><img height='220' width='220' src='" + row["pic"].ToString() + "'></center><div>Category:" + type + "</div><div>Price:" + row["price"].ToString() + "</div>";

            HtmlGenericControl control = new HtmlGenericControl("div");
                control.InnerHtml = inner_content;
                ItemView.Controls.Add(control);
                Button b = new Button();
                b.Text = "Add to cart";
                b.ID = "myID";
                b.Click += new System.EventHandler(this.myEvent);
                ItemView.Controls.Add(b);


        }



    }

    protected void myEvent(object sender, EventArgs e)
    {
        test.Text = "My Event";
   }


    protected void ddl_filter(object sender, EventArgs e)
    {

        load_view(ddl.SelectedItem.Text);
    }

}

0 个答案:

没有答案