Solved

Viewstate of DataGrid CheckBox control...

Posted on 2006-10-25
10
602 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

21 Experts available now in Live!

Get 1:1 Help Now