GridView TemplateField's DataBinding event not raised on postback

  • Thread starter Thread starter cpn008
  • Start date Start date
C

cpn008

I have a dynamically created gridview with a templatefield column. It
contains one button. The code is below. Basically what happens is that
the InstantiateIn() method fires EVERY time, the button_DataBinding()
method is never fired on Postback (it fires fine on initial page
load). As a result, I am unable to get the dataValue and do the
necessary binding on Postback. This is Bad Juju!

Any direction you can point me in would be greatly appreciated.
Thanks!



1 public void InstantiateIn( Control container )
2 {
3 ..
4
5 Button button = new Button();
6 button.Text = "Edit";
7
8 button.DataBinding += new EventHandler
( button_DataBinding );
9
10 container.Controls.Add(button);
11 }
12
13
14 private void button_DataBinding( object sender, EventArgs e )
15 {
16 Button button = (Button)sender;
17 GridViewRow row = (GridViewRow)button.NamingContainer;
18
19 object dataValue = DataBinder.Eval(row.DataItem, "ID");
20 ..
21 }
22
 
How do you databind on postbacks?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin




I have a dynamically created gridview with a templatefield column. It
contains one button. The code is below. Basically what happens is that
the InstantiateIn() method fires EVERY time, the button_DataBinding()
method is never fired on Postback (it fires fine on initial page
load). As a result, I am unable to get the dataValue and do the
necessary binding on Postback. This is Bad Juju!
Any direction you can point me in would be greatly appreciated.
Thanks!
1    public void InstantiateIn( Control container )
2    {
3            ..
4
5            Button button = new Button();
6            button.Text = "Edit";
7
8            button.DataBinding += new EventHandler
( button_DataBinding );
9
10           container.Controls.Add(button);
11   }
12
13
14   private void button_DataBinding( object sender, EventArgs e )
15   {
16       Button button = (Button)sender;
17       GridViewRow row = (GridViewRow)button.NamingContainer;
18
19       object dataValue = DataBinder.Eval(row.DataItem, "ID");
20       ..
21   }
22- Hide quoted text -

- Show quoted text -

Hi,

Currently I do not databind the gridview on postback, only on the
initial load.

I did try to databind on postback (either in the OnInit or OnLoad
event), and the above DataBinding event did get raised. But the
problem is when the button gets clicked, nothing happens, the
gridview's RowCommand event won't fire. If I don't databind on
postback, the RowCommand event will fire but not the DataBinding
event, so I cannot get and pass the neccessary info from the gridview
row.

Thanks.
 
Ok, so you know that you need to databind in order to get the DataBinding
event working. Do it in OnLoad event. And set CommandName property for the
button to get command events.


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


How do you databind on postbacks?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin




I have a dynamically created gridview with a templatefield column. It
contains one button. The code is below. Basically what happens is that
the InstantiateIn() method fires EVERY time, the button_DataBinding()
method is never fired on Postback (it fires fine on initial page
load). As a result, I am unable to get the dataValue and do the
necessary binding on Postback. This is Bad Juju!
Any direction you can point me in would be greatly appreciated.
Thanks!
1 public void InstantiateIn( Control container )
2 {
3 ..
4
5 Button button = new Button();
6 button.Text = "Edit";
7
8 button.DataBinding += new EventHandler
( button_DataBinding );
9
10 container.Controls.Add(button);
11 }
12
13
14 private void button_DataBinding( object sender, EventArgs e )
15 {
16 Button button = (Button)sender;
17 GridViewRow row = (GridViewRow)button.NamingContainer;
18
19 object dataValue = DataBinder.Eval(row.DataItem, "ID");
20 ..
21 }
22- Hide quoted text -

- Show quoted text -

Hi,

Currently I do not databind the gridview on postback, only on the
initial load.

I did try to databind on postback (either in the OnInit or OnLoad
event), and the above DataBinding event did get raised. But the
problem is when the button gets clicked, nothing happens, the
gridview's RowCommand event won't fire. If I don't databind on
postback, the RowCommand event will fire but not the DataBinding
event, so I cannot get and pass the neccessary info from the gridview
row.

Thanks.
 
Ok, so you know that you need to databind in order to get the DataBinding
event working. Do it in OnLoad event. And set CommandName property for the
button to get command events.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin


How do you databind on postbacks?
- Show quoted text -

Hi,

Currently I do not databind the gridview on postback, only on the
initial load.

I did try to databind on postback (either in the OnInit or OnLoad
event), and the above DataBinding event did get raised. But the
problem is when the button gets clicked, nothing happens, the
gridview's RowCommand event won't fire. If I don't databind on
postback, the RowCommand event will fire but not the DataBinding
event, so I cannot get and pass the neccessary info from the gridview
row.

Thanks.- Hide quoted text -

- Show quoted text -

Thanks much for the help!
 
Back
Top