I'm trying to set certain fields on a DetailsView control to ReadOnly based on roles. I'm successful on setting them to read-only, however the Update on the datasource fails because those fields are now Nothing when they are updated in the DB. It seems the
values are lost from the control after changing to read-only.
'// Do not show EDIT/DELETE for CSC members.
If My.User.CurrentPrincipal.IsInRole("csc") And dvSerial.CurrentMode.Edit Then
'// Disable the fields for READ ONLY viewers.
Dim c As System.Web.UI.WebControls.DataControlField
For Each c In dvSerial.Fields
If TypeOf c Is System.Web.UI.WebControls.BoundField Then
Select Case CType(c, System.Web.UI.WebControls.BoundField).DataField
'/ Update only these 2 fields to read-only.
Case "lftv_password", "lftv_ip"
CType(c, System.Web.UI.WebControls.BoundField).ReadOnly = True
End Select
End If
Next
end if
I get this exception when the update tries to update the database. I didn't change any values, the values show on the DetailsView as readonly which is what I need, but for some reason they lose their value upon updating the DB. Am I doing this wrong or is
this a bug? I'm running this code on both the Form_Load or anywhere else with the same error.
Exception information:
Exception type: System.Data.SqlClient.SqlException
Exception message: Cannot insert the value NULL into column 'lftv_password', table 'prod_dev.dbo.F_lftv_serials'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
Would any of these issues have anything to do with my problem? I'm wondering if its just a limitation or if there is no way to mark a detailsview field as read-only besides at design time ? I need to be able to mark a field as read-only just as if done with
the designer in the detailsview columns collection properties.
I take it that I just need to work around this until the bug is fixed, or is set as a design change and will stay this way.
The above code works to set fields to read-only, however I have a tough time getting the previous values from the DetailsView so I can repopulate the parameters. I'd rather not have to create another entire sqldatasource just for a single field difference,
so I'm trying to accomplish this without luck:
I'd like to somehow grab the value from the detailsview and populate the datsource update parameters with it so it passes it back to the db. The XYZ string should actually be grabbed somehow via the detailsview, but I don't see an easy way to reference values
in a detailsview without knowing its rowIndex, which in itself is almost impossible to get programmatically. I would never want to set detailsview1.rows(#).cells(1).text because who knows when the row number (#) is going to change ... I can't count on it
being the same number all the time. Why is there not a rows() using a name instead of just an index? I think I'm missing something ...
I tried using FindControl but when it's a boundfield I don't believe I can do this ... the control name is unknown because its auto generated.
It's not only readonly fields. I found you also have this problem if you choose not to display certain columns from your table in your DetailsView. I.e. if you have a table with Xyz column but you don't have this field in your DetailsView, the value for Xyz
will be null. I guess this anomaly isn't related to the TextBox/ViewState bug.
How did you do at finding a workaround for your problem?
Has anyone found a solution to this? It would seem that only displaying certain rows in a DetailsView would be a common scenario (based on role membership for example). It seems crazy that the values of the fields not being displated are lost when updating.
I'm using an ObjectDataSource with a custom business object (class Entity). I use a static method in a business layer to update the business object which takes a single parameter of type Entity. It seems that this is a common problem across all data source
objects - as you'd expect I suppose as it looks like its the DetailsView creating the problem.
I also ran into the issue where the problem occurs when a field is not shown. I had this specifically in the DataGrid.
For those still using Beta 2 (July CTP and up fix this issue):
Instead of having a bound field, use a template field. Apparently, when setting the ViewState, the Framework works correctly if there is a control embedded in a hidden or readonly column. So, put a label control in the column or row that you want to hide or
make read-only and databind to that label control.
While thats an so-so workaround if you are already manually generating your fields manually, its not a solution at all if you are autogenerating your fields. The ability to make changes to the autogenerated fields, such as setting them readonly or visible,
and not having the issue of data gone missing are to very important fixes that *MUST* be made.
I agree that it must be fixed. However, I believe it has been in July CTP and up, including the Release Candidate. I have not positively verified it, but will do so soon.
For me, it's a sad reality that no matter how well the autogenerated fields work in the new controls, I still have to define at least a few fields' controls myself, so that one more control doesn't matter much.
changeip
Member
30 Points
6 Posts
DetailsView - Programatically setting readonly without losing data
Aug 24, 2005 07:48 PM|LINK
I'm trying to set certain fields on a DetailsView control to ReadOnly based on roles. I'm successful on setting them to read-only, however the Update on the datasource fails because those fields are now Nothing when they are updated in the DB. It seems the values are lost from the control after changing to read-only.
'// Do not show EDIT/DELETE for CSC members.
If My.User.CurrentPrincipal.IsInRole("csc") And dvSerial.CurrentMode.Edit Then
'// Disable the fields for READ ONLY viewers.
Dim c As System.Web.UI.WebControls.DataControlField
For Each c In dvSerial.Fields
If TypeOf c Is System.Web.UI.WebControls.BoundField Then
Select Case CType(c, System.Web.UI.WebControls.BoundField).DataField
'/ Update only these 2 fields to read-only.
Case "lftv_password", "lftv_ip"
CType(c, System.Web.UI.WebControls.BoundField).ReadOnly = True
End Select
End If
Next
end if
I get this exception when the update tries to update the database. I didn't change any values, the values show on the DetailsView as readonly which is what I need, but for some reason they lose their value upon updating the DB. Am I doing this wrong or is this a bug? I'm running this code on both the Form_Load or anywhere else with the same error.
Exception information:
Exception type: System.Data.SqlClient.SqlException
Exception message: Cannot insert the value NULL into column 'lftv_password', table 'prod_dev.dbo.F_lftv_serials'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
changeip
Member
30 Points
6 Posts
Re: DetailsView - Programatically setting readonly without losing data
Aug 24, 2005 09:45 PM|LINK
http://forums.asp.net/927783/ShowPost.aspx (beta2 viewstate and readonly controls)
http://forums.asp.net/937160/ShowPost.aspx (ASP.NET 2.0 Beta2 ReadOnly TextBox loses ViewState after postback)
Thanks
changeip
Member
30 Points
6 Posts
Re: DetailsView - Programatically setting readonly without losing data
Aug 25, 2005 05:20 PM|LINK
I take it that I just need to work around this until the bug is fixed, or is set as a design change and will stay this way.
The above code works to set fields to read-only, however I have a tough time getting the previous values from the DetailsView so I can repopulate the parameters. I'd rather not have to create another entire sqldatasource just for a single field difference, so I'm trying to accomplish this without luck:
sdsSerialDetail.UpdateParameters(CType(c, System.Web.UI.WebControls.BoundField).DataField).DefaultValue = "XYZ"
CType(c, System.Web.UI.WebControls.BoundField).ReadOnly = True
I'd like to somehow grab the value from the detailsview and populate the datsource update parameters with it so it passes it back to the db. The XYZ string should actually be grabbed somehow via the detailsview, but I don't see an easy way to reference values in a detailsview without knowing its rowIndex, which in itself is almost impossible to get programmatically. I would never want to set detailsview1.rows(#).cells(1).text because who knows when the row number (#) is going to change ... I can't count on it being the same number all the time. Why is there not a rows() using a name instead of just an index? I think I'm missing something ...
I tried using FindControl but when it's a boundfield I don't believe I can do this ... the control name is unknown because its auto generated.
Thanks
mooz
Member
25 Points
5 Posts
Re: DetailsView - Programatically setting readonly without losing data
Sep 06, 2005 10:14 AM|LINK
How did you do at finding a workaround for your problem?
barwickl
Member
70 Points
14 Posts
Re: DetailsView - Programatically setting readonly without losing data
Sep 08, 2005 04:47 PM|LINK
Speedbird186
Member
200 Points
49 Posts
Re: DetailsView - Programatically setting readonly without losing data
Sep 16, 2005 11:17 PM|LINK
I also ran into the issue where the problem occurs when a field is not shown. I had this specifically in the DataGrid.
For those still using Beta 2 (July CTP and up fix this issue):
Instead of having a bound field, use a template field. Apparently, when setting the ViewState, the Framework works correctly if there is a control embedded in a hidden or readonly column. So, put a label control in the column or row that you want to hide or make read-only and databind to that label control.
HTH
SA.
thzero
Member
95 Points
19 Posts
Re: DetailsView - Programatically setting readonly without losing data
Sep 29, 2005 12:46 PM|LINK
Speedbird,
While thats an so-so workaround if you are already manually generating your fields manually, its not a solution at all if you are autogenerating your fields. The ability to make changes to the autogenerated fields, such as setting them readonly or visible, and not having the issue of data gone missing are to very important fixes that *MUST* be made.
Speedbird186
Member
200 Points
49 Posts
Re: DetailsView - Programatically setting readonly without losing data
Sep 29, 2005 10:57 PM|LINK
I agree that it must be fixed. However, I believe it has been in July CTP and up, including the Release Candidate. I have not positively verified it, but will do so soon.
For me, it's a sad reality that no matter how well the autogenerated fields work in the new controls, I still have to define at least a few fields' controls myself, so that one more control doesn't matter much.
thzero
Member
95 Points
19 Posts
Re: DetailsView - Programatically setting readonly without losing data
Sep 30, 2005 02:20 AM|LINK
Or how you can't get manipulate at run-time the autogenerated fields either, that needs to be fixed too.
uswebpro
Participant
1060 Points
219 Posts
Re: DetailsView - Programatically setting readonly without losing data
Oct 22, 2005 09:18 PM|LINK
- Aron