Ajax asp.net

Solo disponible en BuenasTareas
  • Páginas : 4 (798 palabras )
  • Descarga(s) : 0
  • Publicado : 12 de noviembre de 2011
Leer documento completo
Vista previa del texto
Adding Controls dynamically to UpdatePanel control and Setting Triggers Dynamically

Sometimes, we will require to create UpdatePanel dynamically and load the controls dynamically to it. The belowcode will help us to do that.

protected void Page_Load(object sender, EventArgs e)
{
UpdatePanel up1 = new UpdatePanel();

TextBox txtName = new TextBox();Label lbl = new Label();
Button btn = new Button();
lbl.ID = "lblName";
btn.ID = "btnSave";
btn.Text = "Save";
btn.Click += newEventHandler(btn_Click);
up1.ContentTemplateContainer.Controls.Add(lbl);
up1.ContentTemplateContainer.Controls.Add(txtName);
up1.ContentTemplateContainer.Controls.Add(btn);form1.Controls.Add(up1);
}

Setting Triggers Dynamically
When we set the ChildrenAsTriggers to false and UpdateMode to Conditional we need to define triggers explicitly for the UpdatePanelcontrols. The below code will help us in doing dynamically.

protected void Page_Load(object sender, EventArgs e)
{
UpdatePanel up1 = new UpdatePanel();
up1.ChildrenAsTriggers =false;
up1.UpdateMode = UpdatePanelUpdateMode.Conditional;

TextBox txtName = new TextBox();
Label lbl = new Label();
Button btn = new Button();lbl.ID = "lblName";
btn.ID = "btnSave";
btn.Text = "Save";
btn.Click += new EventHandler(btn_Click);
up1.ContentTemplateContainer.Controls.Add(lbl);up1.ContentTemplateContainer.Controls.Add(txtName);
up1.ContentTemplateContainer.Controls.Add(btn);
AsyncPostBackTrigger trig = new AsyncPostBackTrigger();trig.ControlID = btn.ID;
trig.EventName = "Click";
up1.Triggers.Add(trig);
form1.Controls.Add(up1);
}
protected void btn_Click(object sender, EventArgs...
tracking img