简体   繁体   中英

Checkbox.checked in gridview on postback

I have a gridview with checkbox in Itemtemplate. What i need to do is check the condition:

(checkbox.checked==true) 

if its true take the rows to another gridview. i binded the data of the gridview in

if(!PostBack)condition.

Everything was working fine until my employer said he doesnt wants the gridview to be bound in the if(!postback) condition but if i take that condition out checkbox.checked value is false always!

From the details of your question, I'm not sure if your boss wants the grid unbound or doesn't want the page bloated with the grid's ViewState, but in my experience, it's usually the latter. However, if your boss simply doesn't want the grid bound in the "if(!Postback)" condition and doesn't mind if it's bound or uses ViewState, then your boss doesn't understand web development using C# and server-side controls.

But... to answer your question, the reason the checkboxes are all unchecked is because you're rebinding the grid AFTER the ViewState has been applied. ViewState contains all the data needed in a postback condition to set the state of the checkboxes during postbacks and applies that data prior to the OnLoad (or Page_Load) event. By rebinding the grid, you are essentially wiping out anything that was posted back in ViewState and applied. To overcome this, turn ViewState off for the grid and apply the state of the checkboxes yourself AFTER you rebind the grid using the FORM collection.

If you want to do away with ViewState, here are some tips in this article. http://www.codeproject.com/KB/viewstate/DataGridViewState.aspx

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