Asp.net按钮点击事件不起作用



我有一个用程序填充的表。按钮也是在表构建时在表中创建的。但我似乎找不到让我的按钮点击事件工作的方法。它似乎并没有引发这场活动。

有什么想法吗?我是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>

您应该使用不带括号的

assignNameBtn.Click += assignNameBtn_Click;

相关内容

  • 没有找到相关文章