?
Solved

Viewstate of DataGrid CheckBox control...

Posted on 2006-10-25
10
Medium Priority
?
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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