Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Saving values in a disabled checkbox
Message
De
04/09/2013 17:20:39
 
 
À
04/09/2013 14:07:21
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01582105
Message ID:
01582206
Vues:
28
>>>>I have a formview inside a formview. FV2 is on the ItemTemplate of FV1. FV2 contains 6 checkboxes. Based on user roles, users are allowed to edit various combinations of the 6 checkboxes. For example user1 may edit Ck1 and Ck2 but not the others. User2 may edit only Ck5, etc. The ckeckboxes are enabled or disabled based on the user's roles. My problem is that if a checkbox is not enabled, the value of 0 is stored anyway. Thus the user wipes whatever data was stored for the checkboxes he was not allowed to edit. Is there a setting somewhere that will preserve a previous true even if the checkbox is disabled?
>>>>
>>>>I had thought I could reenable the checkboxes before the save but since FV2 is inside of FV1 its methods are not readily available.
>>>>
>>>>Thanks
>>>
>>>Try creating a hidden field for any disabled checkbox with the original value.
>>
>>I would still need to intercept the update command of FV2 and write the update code myself. Do you know how I could do that since FV2.Updating is not available in the selection DDLs?
>
>I don't usually use formviews, so I'm afraid I don't know how that would work.
OK - Let's do this a little more basic. I removed the checkboxes from the formview and just placed them on the page. They are not bound to anything. They are populated with this routine.
Private Sub Page_PreRenderComplete(sender As Object, e As System.EventArgs) Handles Me.PreRenderComplete
					
			Dim Basecase_ck As CheckBox = Basecase_CheckBox
			Dim Scenario1_ck As CheckBox = Scenario1_CheckBox
			Dim Scenario2_ck As CheckBox = Scenario2_CheckBox
			Dim Scenario3_ck As CheckBox = Scenario3_CheckBox
			Dim Scenario4_ck As CheckBox = Scenario4_CheckBox
			Dim Scenario5_ck As CheckBox = Scenario5_CheckBox
			Dim Scenario6_ck As CheckBox = Scenario6_CheckBox

			If Not User.IsInRole("CanEditBasecase") Then
				Basecase_ck.Enabled = False
			End If
			If Not User.IsInRole("CanEditScenario1") Then
				Scenario1_ck.Enabled = False
			End If
			If Not User.IsInRole("CanEditScenario2") Then
				Scenario2_ck.Enabled = False
			End If
			If Not User.IsInRole("CanEditScenario3") Then
				Scenario3_ck.Enabled = False
			End If
			If Not User.IsInRole("CanEditScenario4") Then
				Scenario4_ck.Enabled = False
			End If
			If Not User.IsInRole("CanEditScenario5") Then
				Scenario5_ck.Enabled = False
			End If
			If Not User.IsInRole("CanEditScenario6") Then
				Scenario6_ck.Enabled = False
			End If

			Dim oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlRTPConnectionString").ConnectionString)
			Dim oCommand As New SqlClient.SqlCommand("SELECT rtp_id, [basecase], scenario1, scenario2, scenario3, scenario4, scenario5, scenario6 FROM rtp_projects WHERE rtp_id = @rtp_id", oConn)
			oConn.Open()
			oCommand.Parameters.AddWithValue("@rtp_id", FormView1.SelectedValue)
			Dim oReader As SqlDataReader = oCommand.ExecuteReader
			oReader.Read()
			Basecase_ck.Checked = oReader("Basecase")
			Scenario1_ck.Checked = oReader("Scenario1")
			Scenario2_ck.Checked = oReader("Scenario2")
			Scenario3_ck.Checked = oReader("Scenario3")
			Scenario4_ck.Checked = oReader("Scenario4")
			Scenario5_ck.Checked = oReader("Scenario5")
			Scenario6_ck.Checked = oReader("Scenario6")

			oReader.Close()
			oCommand.Dispose()
			oConn.Close()
			End Sub
and updated with this:
	Public Sub SaveCheckBoxes(sender As Object, e As EventArgs)
		Dim oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlRTPConnectionString").ConnectionString)
		Dim oCommand As New SqlClient.SqlCommand("", oConn)
		oConn.Open()

		Select Case sender.ID.ToString
			Case "Basecase_CheckBox"
				oCommand.CommandText = "UPDATE [rtp_projects] SET [basecase] = @basecase WHERE rtp_id = @rtp_id"
				oCommand.Parameters.AddWithValue("@basecase", sender.Checked)
				oCommand.Parameters.AddWithValue("@RTP_ID", FormView1.SelectedValue)
			Case "Scenario1_CheckBox"
				oCommand.CommandText = "UPDATE [rtp_projects] SET [Scenario1] = @Scenario1 WHERE rtp_id = @rtp_id"
				oCommand.Parameters.AddWithValue("@Scenario1", sender.Checked)
				oCommand.Parameters.AddWithValue("@RTP_ID", FormView1.SelectedValue)
			Case "Scenario2_CheckBox"
				oCommand.CommandText = "UPDATE [rtp_projects] SET [Scenario2] = @Scenario2 WHERE rtp_id = @rtp_id"
				oCommand.Parameters.AddWithValue("@Scenario2", sender.Checked)
				oCommand.Parameters.AddWithValue("@RTP_ID", FormView1.SelectedValue)
			Case "Scenario3_CheckBox"
				oCommand.CommandText = "UPDATE [rtp_projects] SET [Scenario3] = @Scenario3 WHERE rtp_id = @rtp_id"
				oCommand.Parameters.AddWithValue("@Scenario3", sender.Checked)
				oCommand.Parameters.AddWithValue("@RTP_ID", FormView1.SelectedValue)
			Case "Scenario4_CheckBox"
				oCommand.CommandText = "UPDATE [rtp_projects] SET [Scenario4] = @Scenario4 WHERE rtp_id = @rtp_id"
				oCommand.Parameters.AddWithValue("@Scenario4", sender.Checked)
				oCommand.Parameters.AddWithValue("@RTP_ID", FormView1.SelectedValue)
			Case "Scenario5_CheckBox"
				oCommand.CommandText = "UPDATE [rtp_projects] SET [Scenario5] = @Scenario5 WHERE rtp_id = @rtp_id"
				oCommand.Parameters.AddWithValue("@Scenario5", sender.Checked)
				oCommand.Parameters.AddWithValue("@RTP_ID", FormView1.SelectedValue)
			Case "Scenario6_CheckBox"
				oCommand.CommandText = "UPDATE [rtp_projects] SET [Scenario6] = @Scenario6 WHERE rtp_id = @rtp_id"
				oCommand.Parameters.AddWithValue("@Scenario6", sender.Checked)
				oCommand.Parameters.AddWithValue("@RTP_ID", FormView1.SelectedValue)

		End Select

		oCommand.ExecuteNonQuery()
		oCommand.Dispose()
		oConn.Close()
		'MsgBox("Updated")

	End Sub
Each checkbox looks like this:
<asp:CheckBox ID="Basecase_CheckBox" runat="server" Enabled="true" Text="Basecase" OnCheckedChanged="SaveCheckBoxes" />
This all works fine if all of the checkboxes are enabled. However, if a couple of them are disabled, as soon as I edit one of the remaining ones the status of the disabled ones goes to UnChecked and the table is updated to unchecked. I can't figure this out because there is NO binding and No call to the disabled checkboxes or their fields. I suspect it has something to do with viewstate but can't figure it out.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform