• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

ASP.NET : Setting up a custom table with checkboxes

I found the GridView to be limiting for my needs and instead I want to use a table.

I am having issues with how checkboxes and tables are managed.

My issues are:

  o Adding multiple checkboxes with unique values, of which I may have disabled
     checkboxes depending on my data

     HTML Example: <input type="checkbox" value="1">

  o Adding rows vertically, this appears to be adding them horizontally
Dim lstCustomers As New CheckBox
Dim intID As Integer
Dim strName As String

Dim r As New TableRow()
Dim c As New TableCell()

' How to assign intID to lstCustomers checkbox?
intID = 1
strName = "John Smith"

c.Controls.Add(lstCustomers)
r.Cells.Add(c)
c.Controls.Add(New LiteralControl(strName))
r.Cells.Add(c)
tblCustomers.Rows.Add(r)

' How to assign intID to lstCustomers checkbox?
intID = 2
strName = "Bob Smith"

c.Controls.Add(lstCustomers)
r.Cells.Add(c)
c.Controls.Add(New LiteralControl(strName))
r.Cells.Add(c)
tblCustomers.Rows.Add(r)

Open in new window

<asp:Table ID="tblCustomers" BorderWidth="1" GridLines="Both" runat="server" />

Open in new window

TableControl.png
0
Officedoxs
Asked:
Officedoxs
  • 4
  • 4
1 Solution
 
RouchieCommented:
Could you please clarify what you are trying to achieve here?  e.g. the purpose of your page and what you are doing with the data?

The reason for asking is that the gridview is actually more than capable of achieving most tasks like this, but if there's a trick that you're missing that can make it easier for you, then we can identify it based on what you need to do.
0
 
OfficedoxsAuthor Commented:
I have checkboxes displaying or not displaying depending on related columns.  Some rows are completely disabled including disabled checkboxes and buttons.  From what I had seen, this was beyond what Gridview could readily manage without a lot of background code.  My thinking was that a table would be easier to manage, but perhaps not.

Here's an example of my design and run time.  It'll probably get a little more complicated than this when all is said and done, but this should cover off most of the issues.
TableDesigntime.png
TableRuntime.png
0
 
RouchieCommented:
Its actually very easy in the gridview, but yes, you do need code to make it work.  However, that's actually a good thing when you've done it because you have TONS more control over how things work.

I can show you some code to get it working, which you can tweak, but first can I ask about the functionality of the CHANGE button on each row?  What does that do when pressed?  The reason is that the Gridview can optionally show values, then switch them to editable fields when Change is pressed.  Or, it can just present editable values that can be saved when Change is pressed.

Just let me know...
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
OfficedoxsAuthor Commented:
Here a better overview of the functions.  The idea is to have my system contact people via phone or email depending on what the user selects.  There will be defaults which will automatically be checked.  The flow doesn't make much sense yet as I still have design to complete, but if I am able to work the programming side of it I can massage the flow later.

  o Auto-select all columns (1st column and Email), preferably in JavaScript so the
     page does not refresh.  I've seen code for this but it's appears a little more difficult
     in a Gridview.

  o I need to extract the underlying table ID details based on the 1st column checkbox.

  o Send allows customer to update/send via another page, a "Sending" page.  Send Selected
     will post updates all the items selected in the first column in this "Sending" page.

  o Edit will direct to another page with the table ID of the row.

  o The "Sending" page will need the ID numbers passed through as well.

  o Some columns will be "grayed out", I have used strike through for this example.
     Note the buttons and drop-downs are disabled for that line as well.
TableControl.png
0
 
RouchieCommented:
Okay let me work through your points one at a time...

  o Auto-select all columns (1st column and Email), preferably in JavaScript so the
     page does not refresh.  I've seen code for this but it's appears a little more difficult
     in a Gridview.

Why do you need to do this?  The remaining points seem to indicate that rows are edited one at a time.  I think perhaps the GridView should be in read-mode to begin with, then you switch to Edit mode to make changes to a row.  Does that sound better?

  o I need to extract the underlying table ID details based on the 1st column checkbox.

This is quite easy.  The GridView has a property called DataKeyNames that stores this value:
<asp:GridView runat="server" ID="myGV" DataKeyNames="MyIDColumnName" .......>

Open in new window

On postback, you can then read back that ID by doing something like this:
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles myGV.RowEditing
    Dim ID as Int32 = Convert.ToInt32(myGV.DataKeys(e.RowIndex).Value)
End Sub

Open in new window


  o Send allows customer to update/send via another page, a "Sending" page.  Send Selected
     will post updates all the items selected in the first column in this "Sending" page.

Okay so write the Send button in code like this:

<asp:Button id="btn_Send" runat="server" Text="Send" CommandName="Send" />

Open in new window


Then in code behind add the row number to the button's CommandArgument (so we can find it correctly when it's pressed):
	Protected Sub GvRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles myGV.RowDataBound
		Select Case e.Row.RowType
			Case DataControlRowType.DataRow
				Dim btn_Send As Button = CType(e.Row.FindControl("btn_Send"), Button)
				btn_Send.CommandArgument = "Send"
		End Select
	End Sub

Open in new window


Now when that button is pushed that action can be caught like this:

	Protected Sub SendPressed(sender As Object, e As CommandEventArgs) Handles myGV.RowCommand
		Select Case e.CommandName
			Case "Send"
				Dim correctRow As Integer = Convert.ToInt32(e.CommandArgument) ' reads back the row number saved above
				' example logic here (amend as needed!)
				Dim Name As String = CType(myGV.Rows(correctRow).FindControl("ltl_ID_Containing_Name_Value"), Literal).Text
				Session("Name") = Name
				Response.Redirect("ProcessingPage.aspx")
		End Select
	End Sub

Open in new window


  o Edit will direct to another page with the table ID of the row.

Exactly the same proceedure as above (get the row, read the ID value from DataKeyNames then transfer)

  o The "Sending" page will need the ID numbers passed through as well.

As above

  o Some columns will be "grayed out", I have used strike through for this example.
     Note the buttons and drop-downs are disabled for that line as well.

In the RowDataBound event of the gridview, declare the controls using the FindControl trick...
Dim ddl As DropDownList = CType(e.Row.FindControl("DropDownListID"), DropDownList)
If conditionIsMetToDisableControl Then
   ddl.Enabled = False
End If
' repeat for all controls that need to be disabled.

Open in new window

0
 
OfficedoxsAuthor Commented:
Thank you for the details.  I am reviewing your solution and will update you shortly.
0
 
RouchieCommented:
Take your time - there is a lot to take in (but I promise it's worth the effort...!)  :-)
0
 
OfficedoxsAuthor Commented:
Your solution was complete.  And while it looks like I can get everything in, it looks like my requirements and how I have things structured may be easier to do in a custom table.  Thank you for your insights.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now