[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET : Setting up a custom table with checkboxes

Posted on 2011-05-02
8
Medium Priority
?
417 Views
Last Modified: 2012-05-11
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
Comment
Question by:Officedoxs
  • 4
  • 4
8 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 35510608
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
 

Author Comment

by:Officedoxs
ID: 35511231
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
 
LVL 25

Expert Comment

by:Rouchie
ID: 35511711
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Officedoxs
ID: 35517324
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
 
LVL 25

Accepted Solution

by:
Rouchie earned 2000 total points
ID: 35681745
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
 

Author Comment

by:Officedoxs
ID: 35695703
Thank you for the details.  I am reviewing your solution and will update you shortly.
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35696064
Take your time - there is a lot to take in (but I promise it's worth the effort...!)  :-)
0
 

Author Closing Comment

by:Officedoxs
ID: 37122133
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 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