Asp.net按钮单击事件无效

时间:2015-02-18 12:53:49

标签: asp.net button onclick event-handling click

我有一个以编程方式填充的表。在构建表时,也会在表中创建按钮。但我似乎找不到让我的按钮点击事件工作的方法。它似乎并没有激发这一事件。

有什么想法吗?我是asp.net的新手。

以下是代码:

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

namespace FrogPointCMS
{
    public partial class Beacons : System.Web.UI.Page
    {
        string merchantName;
        string myMerchantID;
        ParseObject merchantObject;

        protected void Page_Load(object sender, EventArgs e)
        {

            merchantName = Request.QueryString["user"];
            myMerchantID = Request.QueryString["merchantID"];

           // merchantObject = (ParseObject)Session["Merchant"]; // this works to retrieve objects from another screen

            merchantObject = Global.merchantObjectStatic;

            if (merchantObject != null)
            {
                getBeacons();
            }

        }



        public async void getBeacons()
        {

             MyParseQueries myQuery = new MyParseQueries();

             var myBeacons = await myQuery.getMyBeacons(); // get all the beacons

            foreach (ParseObject beacon in myBeacons)
            {
                string aliasName = "";
                string offerType = "N/A";
                string offerTitle = "";
                string offerDescription = "";

                 var merchantOb = beacon.Get<ParseObject>("merchantObjectId");
                 var merchantID = merchantOb.ObjectId;

                 if (merchantID == myMerchantID)
                 {
                     if (beacon.ContainsKey("contentObjectID"))
                     {
                         ParseObject content = beacon.Get<ParseObject>("contentObjectID"); // get the content object from parse.
                         offerType = content.Get<string>("offerType");
                         offerTitle = content.Get<string>("offerTitle");
                         offerDescription = content.Get<string>("offerDescription");
                     }

                     aliasName = beacon.Get<string>("aliasName");

//HERE IS THE PROBLEM AREA WITH THE BUTTON CREATION AND CLICK EVENT

                    Button assignNameBtn = new Button();
                     assignNameBtn = new Button();
                     assignNameBtn.ID = "assignName";
                     assignNameBtn.Text = "Assign Name";
                     assignNameBtn.Click += new System.EventHandler(this.assignNameBtn_Click);
                    // assignNameBtn.OnClientClick = "buttonClickTest2()"; //this works to trigger the java in the html page
                    // assignNameBtn.OnClientClick = "assignNameBtn_Click()";

                     Button assignActionBtn = new Button();
                     assignActionBtn.ID = "assignAction";
                     assignActionBtn.Text = "Assign Action";

                     var tr = new HtmlTableRow();
                     var checkBox = new HtmlTableCell();
                     var tableCellName = new HtmlTableCell();
                     var tableCellButton1 = new HtmlTableCell();
                     var tableCellButton2 = new HtmlTableCell();
                     var tableCellAction = new HtmlTableCell();

                     checkBox.InnerHtml = "<input  type=\"checkbox\"/>";
                     tableCellName.InnerText = aliasName;
                     tableCellButton1.Controls.Add(assignNameBtn);
                     tableCellButton2.Controls.Add(assignActionBtn);
                     tableCellAction.InnerText = offerType + " - " + offerTitle + " - " + offerDescription;

                     tr.Cells.Add(checkBox);
                     tr.Cells.Add(tableCellName);
                     tr.Cells.Add(tableCellButton1);
                     tr.Cells.Add(tableCellAction);
                     tr.Cells.Add(tableCellButton2);

                     beaconTable.Rows.Add(tr);
                 }

            }

        }

        void assignNameBtn_Click(object sender, EventArgs e)
        {

            var btn = (Button)sender; // testing 

            HtmlTableRow row = (HtmlTableRow)btn.NamingContainer; //testing

           // System.Diagnostics.Debug.WriteLine("Clicked");
           // assignNameBtn.OnClientClick = "buttonClickTest2()";
            Response.Write("It worked");

        }
    }
}

有问题的表在.aspx文件中以这样的方式开头

<form id="mainform" runat="server">
                <table border="0" cellpadding="0" cellspacing="0" id="beaconTable" runat="server" >
                <tr>
                    <th class="table-header-check"><a id="toggle-all" ></a> </th>
                    <th class="table-header-repeat line-left minwidth-1"><a href="">Beacon Name</a> </th>
                    <th class="table-header-repeat line-left minwidth-1" style="width: 113px"></a></th>
                    <th class="table-header-repeat line-left"><a href="">Assigned Action</a></th>
                    <th class="table-header-repeat line-left"></a></th>

                </tr>

1 个答案:

答案 0 :(得分:0)

您应该使用不带括号

assignNameBtn.Click += assignNameBtn_Click;