简体   繁体   中英

How to make RowCreated event not to fire when it is not postback

I have a gridview with dynamic controls in RowCreated event. In the gridview columns there are server controls .

For ex I have a LinkButton control, when I click the button it is again creating all the controls on the page in the RowCreated event. Which is eating out so much time.

How can I prevent from that event being called and have those controls

My code :

                        Button btn = new Button();
                        btn.ID = "button1";
                        btn.Text = "Submit";
                        btn.Click += btn_Click;
                        e.Row.Cells[0].Controls.Add(btn);

A simple way would be to add if (!IsPostBack)... to your RowCreated event.

Unfortunately, dynamic controls must be recreated on postback. Retaining values can sometimes get tricky, but if you work with the Page Life Cycle and ViewState correctly, you should be OK.

There are other areas where you can look for optimizations, particularly caching your datasource .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM