OnClick事件的回发问题

时间:2013-12-18 21:09:28

标签: c# asp.net

我有以下代码,但工作正常;当我单击删除按钮时,事件将触发,但更改不会显示在PostBack上。这显然与PostBack管理有关。有没有人有任何建议?

   namespace WebApplication1
    {
        public partial class questiondetails : System.Web.UI.Page
       {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadControls();
            }

        }

        void Edit_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton editbut = sender as ImageButton;
            string cName = editbut.ID;
            string rID = cName.Remove(0, 5);

            Response.Write(Request.Form["entity_" + rID]);
            Response.Write(Request.Form["dollar_" + rID]);

        }

        void Delete_Click(object sender, ImageClickEventArgs e)
        {
            var delbut = (ImageButton)sender;
            string cName = delbut.ID;
            string rID = cName.Remove(0, 7);
            string MyParams = "@RowID=" + rID;

            iservices.DataAccessWebService WS = new iservices.DataAccessWebService();

            WS.ExecuteDatasetStoredProcedure(ConfigurationManager.AppSettings["SecurityDataAccess"], "iweb_sp_custom_coi_Question_Delete", MyParams.ToString());
            LoadControls();
            Response.Write("dome");
      }


        protected void LoadControls()
        {

            //Construct paramters from Querystring
            StringBuilder MyParams = new StringBuilder();
            MyParams.Append("@Term=2013,@ProjectCode=a12345,@ID=123,@CategoryCode=CONSULTANCIES");

            //Count Answered questions
            iservices.DataAccessWebService WS = new iservices.DataAccessWebService();
            DataSet MyAnswers = WS.ExecuteDatasetStoredProcedure(ConfigurationManager.AppSettings["SecurityDataAccess"], "iweb_sp_custom_coi_SelectCurrentQuestion", MyParams.ToString());



            foreach (DataTable dt in MyAnswers.Tables)
            {
                int rc = 0;

                foreach (DataRow dr in dt.Rows)
                {

                    //Generate unique controls from DS
                    TextBox tbx = new TextBox();
                    tbx.ID = "entity_" + dr["rowID"].ToString();
                    tbx.Text = dr["EntityName"].ToString();
                    tbx.Width = 200;

                    DropDownList DollarRange = new DropDownList();
                    DollarRange.Items.Add("Select Dollar Amount");
                    DollarRange.Items.Add("Up to $1,000");
                    DollarRange.Items.Add("$1,001 - 5,000");
                    DollarRange.Items.Add("$5,001 - $10,000");
                    DollarRange.Items.Add("$10,001 - $50,000");
                    DollarRange.Items.Add("$50,001 - or more");
                    DollarRange.ID = "dollar_" + dr["rowID"].ToString();
                    DollarRange.SelectedValue = dr["DollarRange"].ToString();

                    ImageButton Delete = new ImageButton();
                    Delete.ImageUrl = "delete.png";
                    Delete.ID = "delete_" + dr["rowID"].ToString();
                    Delete.Click += Delete_Click;

                    ImageButton Edit = new ImageButton();
                    Edit.ImageUrl = "edit.png";
                    Edit.ID = "edit_" + dr["rowID"].ToString();
                    Edit.Click += Edit_Click;

                    //Append controls to placeholder
                    DataZone.Controls.Add(new LiteralControl((rc % 2 == 0) ? "<tr><td bgcolor='#f1f0e7'>" : "<tr><td>"));
                    DataZone.Controls.Add(tbx);
                    DataZone.Controls.Add(new LiteralControl((rc % 2 == 0) ? "</td><td bgcolor='#f1f0e7'>" : "</td><td>"));
                    DataZone.Controls.Add(DollarRange);
                    DataZone.Controls.Add(new LiteralControl((rc % 2 == 0) ? "</td><td bgcolor='#f1f0e7'>" : "</td><td>"));
                    DataZone.Controls.Add(Delete);
                    DataZone.Controls.Add(Edit);
                    DataZone.Controls.Add(new LiteralControl("</td></tr>"));
                    rc++;
                }



            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

代码的问题是,如果不是PostBack,请不要执行LoadControls。当您动态添加新控件时,它会在页面中注册,因此当调用该控件的回发时 - 页面将知道它应该调用哪种方法。

在发生PostBack的情况下,你不会调用LoadControls,因此Page不知道它应该调用什么方法,因为当页面搜索控件来处理当前的回发数据时,没有控件。

如果您每次都在不检查PageLoad中的IsPostBack的情况下进行LoadControls,那么Delete_Click方法将被称为罚款。但是可能会有两个数据请求 - 一个用于在PageLoad中控制填充,第二个用于删除和更新Delete_Click方法中的新数据。

您的代码还有另一个时刻 - 您正在向DataZone添加控件,我认为它是asp:PlaceHolder,但您不清除它,因此可以复制控件以进行第二次LoadControls调用。像往常一样,您需要在更新PlaceHolder之前清除它。