Solved

Viewstate of DataGrid CheckBox control...

Posted on 2006-10-25
10
609 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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