簡體   English   中英

動態創建的Button的事件處理程序

[英]Event handler for dynamically created Button

我正在嘗試將WinForms項目轉換為ASP.Net項目。 目前,我正在解決一個基本問題。 在用戶在GridView選擇一行后,我需要動態創建一個Button在頁面上顯示它。 在將Button添加到頁面之前,請設置一個Click事件處理程序。 問題是,此事件處理程序永遠不會觸發。 我試圖在觸發GridViewSelectedIndexChanged事件時動態創建Button ,並將Button創建為實例成員,並在類的OnInit方法中設置事件處理程序。 兩者都不起作用。 這是我第一次嘗試的代碼:

protected void dgvReports_SelectedIndexChanged(object sender, EventArgs e)
        {    

            if (this.dgvReports.SelectedIndex >= 0)
            {
                Report rpt = (Report)bs.Current;
                Control parameterCaption = this.divParameters.Controls[0];
                Button btnAccept = new Button() { Text = "Get results" };
                bool newLine = false;
                this.divDescription.Visible = true;
                this.divParameters.Visible = true;

                this.divParameters.Controls.Clear();
                this.divParameters.Controls.Add(parameterCaption);

                this.txtDescription.Text = rpt.Description;

                btnAccept.Click += new EventHandler(btnAccept_Click);

                foreach (ReportParameter parameter in rpt.Parameters)
                {
                    if (parameter.Visible)
                    {
                        this.divParameters.Controls.Add(new Label() { Text = parameter.Description, Width = 150, CssClass = "parameter" });
                        this.divParameters.Controls.Add(new TextBox() { Text = parameter.DefaultValue, Width = 300, ID = parameter.Name });

                        if (newLine)
                        {
                            this.divParameters.Controls.Add(new LiteralControl("<br />"));
                        }

                        newLine = !newLine;
                    }
                }

                this.divParameters.Controls.Add(new LiteralControl("<br /> <div style='text-align:center'>"));
                this.divParameters.Controls.Add(btnAccept);
                this.divParameters.Controls.Add(new LiteralControl("</div>"));
            }
        }

        void btnAccept_Click(object sender, EventArgs e)
        {
            Report rpt = (Report)bs.Current;
            SqlConnection con = new SqlConnection(global::System.Configuration.ConfigurationManager.ConnectionStrings["DP2ConnectionString"].ConnectionString);
            SqlCommand com = new SqlCommand();            
            DataTable dataTable = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(com);

            com.Connection = con;
            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = rpt.DbProcedure;
            dataTable.Locale = CultureInfo.CurrentCulture;

            foreach (Control control in this.divParameters.Controls)
            {
                if (control is TextBox)
                {
                    TextBox txt = control as TextBox;
                    com.Parameters.AddWithValue(txt.ID, txt.Text);
                }
            }

            foreach (ReportParameter parameter in rpt.Parameters)
            {
                if (!parameter.Visible)
                {
                    com.Parameters.AddWithValue(parameter.Name, parameter.DefaultValue);
                }
            }

            sda.Fill(dataTable);
        }

asp.net中的動態控件從未如此簡單。 您的事件處理程序可能未包含在“查看狀態”中,因此不會持久存在於回發中,就像他們單擊有問題的按鈕時一樣。 必須在每次頁面加載時重新制作該按鈕,然后再附加該事件處理程序-如果可能的話,我會為自己省去麻煩,然后嘗試顯示和隱藏該按鈕。

嘗試更改btnAccept.Click += new EventHandler(btnAccept_Click); btnAccept.Click += new RoutedEventHandler(btnAccept_Click);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM