似乎无法使OnClick事件触发

时间:2014-08-14 17:15:24

标签: c# html asp.net onclick click

试图弄清楚为什么这段代码似乎忽略了我的删除按钮上的Click事件。我已经尝试了OnClientClickClick,但两者同时只是为了看看是否会采取行动。由于这显然令人困惑,为了清楚我说“删除按钮”时我的意思,我的问题出现在这一行:btn.Click += btn_Click;

btn_Click永远不会触发,尝试使用调试器逐步执行它,它甚至从未实现过这一点。有人可以告诉我哪里出错了,可能会提出一些如何解决的建议吗?

下面我已经包含了我的所有代码用于此应用程序。

HTML

<asp:PlaceHolder ID="searchPool" runat="server"></asp:PlaceHolder>

<asp:Label ID="lblSearchItem" runat="server">Item</asp:Label>
<asp:TextBox id="txtSearchItem" runat="server"></asp:TextBox>

<asp:Label ID="lblDescription" runat="server">Description</asp:Label>
<asp:TextBox id="txtDescription" runat="server"></asp:TextBox>

<asp:Label ID="lblPath" runat="server">Path</asp:Label>
<asp:TextBox id="txtPath" runat="server"></asp:TextBox>
<asp:Button ID="btnNewSearchItem" runat="server" OnClick="btnNewSearchItem_Click"/>

C

        public class SearchObject
        {
            private string _item;
            private string _description;
            private string _path;

            public SearchObject(string item, string description, string path)
            {
                Item = item;
                Description = description;
                Path = path;
            }

            public string Item {get; set;}
            public string Description {get; set;}
            public string Path {get; set;}
        }
        public static List<SearchObject> searchObjects = new List<SearchObject>();

        private void DisplayItems()
        {
            for (int i = 0; i < searchObjects.Count(); i++)
            {
                Panel pnl = new Panel();
                pnl.ID = string.Format("panel{0}", i);
                TextBox txtItem = new TextBox();
                txtItem.ID = string.Format("panel{0}", i);
                txtItem.Text = searchObjects[i].Item;
                TextBox txtDescription = new TextBox();
                txtDescription.ID = string.Format("txtItem{0}", i);
                txtDescription.Text = searchObjects[i].Description;
                TextBox txtPath = new TextBox();
                txtPath.ID = string.Format("txtPath{0}", i);
                txtPath.Text = searchObjects[i].Path;
                Button btn = new Button();
                btn.ID = string.Format("button{0}", i);
                btn.Click += btn_Click;
                btn.Attributes["RemoveIndex"] = i.ToString();

                pnl.Controls.Add(txtItem);
                pnl.Controls.Add(txtDescription);
                pnl.Controls.Add(txtPath);
                pnl.Controls.Add(btn);

                searchPool.Controls.Add(pnl);
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int index = Convert.ToInt32(btn.Attributes["RemoveIndex"]);
            searchObjects.RemoveAt(index);
        }

        protected void btnNewSearchItem_Click(object sender, EventArgs e)
        {
            SearchObject oSearch = new SearchObject(txtSearchItem.Text, txtDescription.Text, txtPath.Text);
            searchObjects.Add(oSearch);
            DisplayItems();
        }

2 个答案:

答案 0 :(得分:-1)

在.aspx页面中,查看OnClick。引号之间的任何内容都需要是关联的aspx.cs页面中方法的名称。所以对此:

你的方法应该是:

protected void btnNewSearchItem_Click(object sender,EventArgs e) { ...在这里插入代码...... }

答案 1 :(得分:-2)

您的按钮方法必须受到保护void btnNewSearchItem_Click(object obj,EventArg e)