Solved

Viewstate of DataGrid CheckBox control...

Posted on 2006-10-25
10
611 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

705 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