Solved

Viewstate of DataGrid CheckBox control...

Posted on 2006-10-25
10
599 Views
Last Modified: 2008-01-16
Hi,

I am programmatically adding a checkbox control to my datagrid column (.net 1.1).

What I need to do is capture whether or not it is being checked on postback.

So in an ItemDataBound event I have:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim chkSelected As CheckBox
            chkSelected = e.Item.FindControl("ChkSelection")

            If chkSelected.Checked Then
                            System.Diagnostics.Debug.WriteLne("Testing")
            End If
End If

 Problem is that the checkbox is always testing as being unchecked - I assume that this is because after postback the checkbox is recreated and it's viewstate has not been saved?!

Does anyone know of a workaround?

thanks

KS
0
Comment
Question by:saleek
  • 5
  • 3
  • 2
10 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17805595
If you are binding the DataGrid to a Datasource, and you aren't binding the CheckBox to a value, when then page posts back, that value isn't saved, unless you handle persisting the ViewState yourself.

Bob
0
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 17807533
save the things in to hidden variable.... then reassign it on load
0
 
LVL 11

Author Comment

by:saleek
ID: 17810419
TheLearnedOne ,

When you say bind value to checkbox, you mean that I should set it to true or false?

How can I persist the viewstate myself?

What would be the easiest way?

Roopesh,

I dont understand what you mean, can you provide more detail please.


thx,

KS
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17811042
No, I mean if you are binding to a DataTable, then you should have a boolean column that stores the value of the CheckBox, and bind the DataGrid column to that boolean column.

Bob
0
 
LVL 11

Author Comment

by:saleek
ID: 17812532
Ok, I will have to bind a column to the checkboxes.

When I check the boxes in the grid, a postback occurs - in which event can I check the state of checkboxes?

Is it possible to add an indexchanged event handler for each checkbox in the grid?

thank you,

KS
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Author Comment

by:saleek
ID: 17812696
Basically, when the state of any checkbox changes, I want to call a custom procedure.

thanks,

KS
0
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 17813112
if u need to post back the form during check box checked call a javascript
create a hidden variable ,
function inside that javascript
change the value of hidden variable.then submit the form
document.formname.submit();

during load check the value of hidden variable and call the function
0
 
LVL 11

Author Comment

by:saleek
ID: 17817797
Roopesh,

My page already postbacks when I tick a checkbox, but I want to be able to run a custom procedure on postback.

I cannot check the state of the checkbox in the ItemDataBound event as it only fires when the data changes.

regards,

KS
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17818499
You can add an event handler in the ItemCreated event handler.

Bob
0
 
LVL 11

Author Comment

by:saleek
ID: 17818977
Well I created an CheckChanged event in my custom template class and it works great.

The parameter I needed to pass to my custom procedure was the DataKeyField of the grid which I acquired like so:

Dim theGrid As DataGrid = CType(container.Page.FindControl("DataGrid1"), DataGrid)
Dim theID As Integer = CType(theGrid.DataKeys.Item(container.ItemIndex), Integer)

Dont know if thats the most efficient way to do it - but hey, it works!

Thanks.

LearnedOne, I give the points to you as you pointed out that I needed to bind a column from a datasource to my checkbox template column - that solved the initial state problem.

thanks all,

KS
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now