动态控制事件 [英] dynamic control event

查看:87
本文介绍了动态控制事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (dt1.Rows.Count > 0)
{
  for (int i = 0; i < dt1.Rows.Count; i++)
  {
     Panel pnlTextBox= new Panel();
     pnlTextBox.ID = "pnlTextBox_" + i.ToString();
     pnlTextBox.CssClass = "tagpanel";
     TextBox txt = new TextBox();                //Textbox
     txt.ID = "txt_" + i.ToString();
     txt.CssClass = "txtb";
     txt.AutoPostBack = true;
     txt.EnableViewState = true;
     txt.Text = dt1.Rows[i][5].ToString();
        
     if (dt1.Rows[i][1].ToString() == "1")
     {
        txt.Enabled = false;
     }
   
     txt.MaxLength = 8;
     txt.AutoPostBack = true;

     Button Btn = new Button();                 //Save Button
     Btn.ID = "Btn_" + i.ToString();
     Btn.Text = "Save";
     Btn.CssClass = "btnCss";
                   
     Btn.Click += new System.EventHandler(this.Btn_test);

     Literal ltbr1 = new Literal();              //new line 
     ltbr1.ID = "ltbr1" + i.ToString();
     ltbr1.Text = "<br />";

     Label lbl = new Label();                  //tag name
     lbl.ID = "lbl_" + i.ToString();
     lbl.CssClass = "lblCss";
     lbl.Text = dt1.Rows[i][0].ToString();

     Label TagId = new Label();              //tag id
     TagId.ID = "Tag_" + i.ToString();
     TagId.Text = dt1.Rows[0][6].ToString();
     TagId.Visible = false;

     Label OldValue = new Label();             //tag value...used to store old value
     OldValue.ID = "old_" + i.ToString();
     OldValue.Text = dt1.Rows[i][3].ToString();
     OldValue.Visible = false;

     pnlTextBox.Controls.Add(txt);
     //    pnlTextBox.Controls.Add(img);
     pnlTextBox.Controls.Add(ltbr1);
     pnlTextBox.Controls.Add(lbl);
     pnlTextBox.Controls.Add(TagId);
     pnlTextBox.Controls.Add(OldValue);
     if (dt1.Rows[i][1].ToString() == "1")
     {
         pn1.Controls.Add(pnlTextBox);
         //    pn1.Controls.Add(ltbr);
     }
     else
     {
         pnlTextBox.Controls.Add(Btn);
         pn2.Controls.Add(pnlTextBox);
         //  pn2.Controls.Add(ltbr); 
      }                  
   }
}





这是我创建动态控制的代码我在页面pre_init状态调用了这个函数。按钮事件也正常工作。但我有一个问题。当我在文本框中写入新值时,动态创建会导致页面加载。为什么会发生这种情况?因此,当我更改文本框中的值并且我的焦点位于文本框中时,单击按钮事件不起作用。它将在页面加载后工作。为什么会这样?我应该去哪里?



this is my Code to create dynamic control.I have call this function at page pre_init state. Button event is also worked properly. But i have one problem.When I am write new value in text box which is dynamically created causes page load. Why this is happennig??? Because of this when i am changing the value in textbox and if my focus is in textbox then click event of button not works.It will work after page load. why this is happening?? where should i went wrong??

推荐答案

Anisuzzaman Sumon 是正确的。这是因为 AutoPostBack 属性。



删除它。这绝对不需要。
Yes Anisuzzaman Sumon is correct. It is happening due to the AutoPostBack Property.

Remove that. That is absolutely not needed.


你已经为文本框启用了自动回发,

确保当你添加动态控件时,AutoPostback值应为false





you have enabled the auto postback for text box,
Make sure when ever you have adding dynamic controls, AutoPostback values should be false


TextBox txt = new TextBox(); //Textbox
txt.ID = "txt_" + i.ToString();
txt.CssClass = "txtb";
txt.AutoPostBack = false;
txt.EnableViewState = true;


这篇关于动态控制事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆