Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2003, 07:34 PM
Authorized User
 
Join Date: Aug 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default saving checkbox state in template column

Hi,

I'm stuck with something, and I would really appreciate any help.

Here is what I'm trying to do: I have a datagrid bound to a dataset with 4 columns. I'm using auto generate columns. I create a 5th template column with checkbox in each row. When user pushes an "update" button, I need to be able to find out which rows were selected using checkboxes, and then build by own dataset to do an update to the database.

I add the template column in following events: (1)"Search" button which gets info from the database and displays it on the datagrid.
(2)datagrid page index change event

Here is the problem: When I push the "update" button, the template column is not present anymore (Each row returns "Nothing" when I do try to find the control. If I readd the template column in page_load or "update" button event, I lose the info w/regard to which checkboxes were checked.

I would really appreciate any help or suggestions.

Thanks,
Ash


 
Old August 25th, 2003, 12:55 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you elaborate on your situation. Something isn't clear to me.
Quote:
quote:I add the template column in following events: (1)"Search" button which gets info from the database and displays it on the datagrid.
(2)datagrid page index change event
So you are creating the checkbox column on the fly and not in the HTML for the datagrid? Perhaps a clearer question should be: Is the checkbox column always [u]supposed</u> to be there?

Under what circumstances are you binding the data? You should only need to do this once during the lifespan of the page (aside from when data is updated and you really need to re-read the data).

Perhaps the problem you are having is due to rebinding of the data. At some point, the grid is bound, thus doing the autogenerate columns bit, but the datagrid event in which you are attempting to pull the checkbox value happens before the dynamic addition of your checkbox column. Normally, if my memory serves me correctly, you only should bind the grid once during the life of that page (again, apart from when updates are made). After you have bound the grid you can make your dynamic additions of columns and such. Then when posted back the grid gets reconstructed from viewstate and the dynamically added checkbox column should be there. You can then access it, do your updates, then go thru the re-binding process if necessary.

Hope that isn't too confusing.
 
Old August 25th, 2003, 03:59 PM
Authorized User
 
Join Date: Aug 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. Let me try to be more specific this time.
I have a button that pulls some data from the database (based on what user chooses on the page) and displays the data in a datagrid. Here is the code:

    ds = dservice.create(errstring, "object name", "SP Name", plist)
    'persist dataset to session
    Session.Item("DS") = ds

    dgLMCAssign.AutoGenerateColumns = True
    AddCheckBoxCol("templateCol")
    dgLMCAssign.DataSource = dsLMCAssignment.Tables(0)
    dgLMCAssign.DataBind()

Here is AddCheckBoxCol definition:

Public Sub AddCheckBoxCol(ByVal strID As String)
    Dim sCheckBoxID As String = strID
    Dim tcol As New TemplateColumn()
    With tcol
      .HeaderText = "Reassign"
      .ItemTemplate = New MyItemTemplate("stringCheckBoxID")
    End With
    dg.Columns.Add(tcol)
  End Sub

Here is MyItemTemplate definition:
Public Class MyItemTemplate
  Implements ITemplate

  Private colID As String

  Sub New(ByVal str As String)
    colID = str
  End Sub


  Public Overridable Overloads Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
    Dim oCheckBox As CheckBox = New CheckBox()
    oCheckBox.EnableViewState = True

    'oCheckBox.AutoPostBack = True
    oCheckBox.ID = colID
    AddHandler oCheckBox.DataBinding, AddressOf BindCheckBox
    AddHandler oCheckBox.CheckedChanged, AddressOf CheckBoxCheckChangedHandler
    'oCheckBox.Checked = False
    container.Controls.Add(oCheckBox)
  End Sub

  Public Sub BindCheckBox(ByVal sender As Object, ByVal e As EventArgs)
    Dim oCheckBox As CheckBox = CType(sender, CheckBox)
    'oCheckBox.AutoPostBack = True
    Dim container As DataGridItem = CType(oCheckBox.NamingContainer, DataGridItem)
    oCheckBox.EnableViewState = True
    'oCheckBox.Checked = False

  End Sub

  Public Sub CheckBoxCheckChangedHandler(ByVal sender As Object, ByVal e As EventArgs)


  End Sub
End Class

In the definition of myItemTemplate class, I was experimenting with viewstate and events, but couldn't get it to work.

Here is the fleshed out pageindex change event:
Private Sub dgLMCAssign_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles dgLMCAssign.PageIndexChanged

    dsLMCAssignment = Session.Item("LMCAssignment")

    AddCheckBoxCol("LMCtemplateCol")
    dgLMCAssign.CurrentPageIndex() = e.NewPageIndex()
    dgLMCAssign.DataSource = dsLMCAssignment
    dgLMCAssign.DataBind()

  End Sub

Now in my update button event, I want to see which checkboxes were clicked, but they are not visible anymore(visually, and scope wise)

Lemme know if you need more details. I appreciate your time.
And yes, I would like the checkbox column to be always present

Thanks,
Ash
 
Old August 25th, 2003, 05:05 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Wow! You are doing way more than I would attempt. :D

Here's what I would do...

Leave your datagrid as is for the autogenerated columns. But then add the <columns> node with a single <templatecolumn> with an asp:checkbox in it. To bind data to it, you can have the datagrid event "ItemDataBound" set the checked status of the checkbox to the value of some piece of data in that datagriditem. Usually this is another column in the grid that is hidden. So in dgLMCAssign_ItemDataBound() you have something like this:
Code:
CType(e.Item.Cells(0).Controls(0), CheckBox).Checked = _
CType(e.Item.Cells(1).Text, Boolean)
You'll have to change the Cells(x) index value to match your column numbers but you get the idea. When you press the update button on your form you should be able to for each thru the data grid items
Code:
For Each objItem In dgLMCAssign.Items
and pull the checkbox checked status from each using similar code. You will most likely have the data you need right in the datagrid to do whatever update you need to do so you can just pull it from the grid and execute your updates.

Because the code for adding the checkbox column is in the aspx, the column will always be there and will be there on postback. All you need to do is set the checkbox and retrieve its status on the appropriate events.

HTH,
Peter
 
Old August 27th, 2003, 02:56 PM
Authorized User
 
Join Date: Aug 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter.

I had changed my design to work around the problem, but I will try to use your approach in the next page. Appreciate the help!!
 
Old September 5th, 2005, 02:21 AM
Registered User
 
Join Date: Sep 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rujuta
Default

I am facing same problem , Please help me ... i am unable to get how many checkboxes are clicked . no even is fired when checkbox is clicked and when i set autopostback=true , i get completely fresh datagrid .
I am having datagrid which compose of 2 Itemplates and if i don't bind datagrid on postback then all boundcolumns and itemplates are invisible , so i am using sessions to add datagrid columns again but if i check checkbox , i don't get event nor can i realised how many cehckboxes are clicked which i click on commandbutton.

Please help me ASAP .

Thanks in Advance,
Rujuta

Rujuta
 
Old September 5th, 2005, 11:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I guess u r doing it in [u]Postback </u>too!
dont bind ur checkbox(es) in postback.

HTH

Always:),
Hovik Melkomian.
 
Old September 28th, 2005, 01:18 PM
Registered User
 
Join Date: Sep 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey...
I have seen your post on this site.
I am having same problem. What I have done is a bit different design. I dont know whether its good or not...
I have dragged a datagrid on page. And I am having template columns. 1 for label and other 3 are for checkboxes. These number of columns, 4, is not pre defined. Its coming on database condition.
When I am trying to click the Save button, it says that there is no Row at all in Datagrid although its having 31 rows....
I can fetch the number of rows in Javascript but not in code behind when I am clickin on Save button.
Can you help me out, please.?



Quote:
quote:Originally posted by texasraven
 Thanks Peter.

I had changed my design to work around the problem, but I will try to use your approach in the next page. Appreciate the help!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving control state to persistent medium nmahesh567 .NET Framework 2.0 0 June 17th, 2007 02:28 PM
rebinding checkbox checks in a template column texasraven ASP.NET 1.x and 2.0 Application Design 4 October 22nd, 2006 06:16 PM
Checkbox state cq_ted ASP.NET 1.x and 2.0 Application Design 0 May 9th, 2006 12:56 AM
State of CheckBox in ItemTemplate of DataGrid bpgadhia Classic ASP Professional 1 August 19th, 2004 01:26 AM
Saving State psingh ASP.NET 1.x and 2.0 Application Design 0 July 8th, 2003 01:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.