This is a migrated thread and some comments may be shown as answers.

Programatically change of event hanlders and autopostbacks

1 Answer 17 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 23 Jul 2012, 11:09 AM
Hi

I have a grid with a variety of controls (asp:CheckBox, asp:TextBox, etc).  I want to programatically decide if I want to hook OnXXXXChanged hanlders and autopostback.  Could you provide advice on how to best proceed?

I currently have all controls in the grid set to autopostback AND with thier respective change event handler.  All event handlers are used tio show that the grid is currently being editted.  Once the grid is being editted it stays in edit until the save is performed.  Thus subsequent autopostbacks are a frustration as they serve no additional purpose.

Kind regards
Mark

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 23 Jul 2012, 12:20 PM
Hello,

You can access the controls and attach their respective event handlers in ItemCreated event. Here is the sample code.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
     GridDataItem item = (GridDataItem)e.Item;
     TextBox txt = (TextBox)item.FindControl("TextBox1");
     CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
     txt.AutoPostBack = true;
     chk.AutoPostBack = true;
     txt.TextChanged += new EventHandler(txt_TextChanged);
     chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
  }
}
void chk_CheckedChanged(object sender, EventArgs e)
 {//attaching events dynamically
 }
void txt_TextChanged(object sender, EventArgs e)
{
}

Thanks,
Shinu.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or