I have a very simple Grid with a template field containing a radio button:
The grid displays images of a vehicle selected from a database on the key field IDVehicle where tbl_Images.IDVehicle = tbl_Vehicles.IDVehicle
There may be many images for the vehicle so I need to be able to allow the user to select one image as the 'default' or main image to use on reports. To do this I thought it would be simple to add a radio button to the grid so that when the user selectes the radio button the database would be updated so that the DefaultImage field (SQLDataType = BIT) would be set to True for the selected radio button and all of the other buttons with the same IDVehicle would be set to False. Something like:
Of course this does not work. I am not sure if it is because I am attempting this using the Grid ItemDataBound argument and not one associated with the radio button.
Any help much appreciated.
<
telerik:RadGrid
ID
=
"rg_VehicleImages"
runat
=
"server"
AllowAutomaticDeletes
=
"True"
AutoGenerateDeleteColumn
=
"True"
CssClass
=
"radgrid"
DataSourceID
=
"sds_ImagesVehicle"
GridLines
=
"None"
Width
=
"220px"
>
<
mastertableview
autogeneratecolumns
=
"False"
datakeynames
=
"IDImages"
datasourceid
=
"sds_ImagesVehicle"
>
<
rowindicatorcolumn
>
<
HeaderStyle
Width
=
"20px"
/>
</
rowindicatorcolumn
>
<
expandcollapsecolumn
>
<
HeaderStyle
Width
=
"20px"
/>
</
expandcollapsecolumn
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"IDimages"
DefaultInsertValue
=
""
HeaderText
=
""
ItemStyle-ForeColor
=
"White"
ItemStyle-Width
=
"2px"
SortExpression
=
"True"
Visible
=
"True"
>
<
HeaderStyle
Width
=
"2px"
/>
<
ItemStyle
ForeColor
=
"White"
Width
=
"2px"
/>
</
telerik:GridBoundColumn
>
<
telerik:GridImageColumn
AlternateText
=
"Thumbnail"
DataImageUrlFields
=
"FilePath, ThumbnailName"
DataImageUrlFormatString
=
"{0}/{1}"
DataType
=
"System.String"
FooterText
=
"ImageColumn footer"
HeaderText
=
""
ImageAlign
=
"Middle"
UniqueName
=
"vehicleimage"
>
<
HeaderStyle
Width
=
"75px"
/>
</
telerik:GridImageColumn
>
<
telerik:GridBoundColumn
DataField
=
"ThumbnailName"
DefaultInsertValue
=
""
HeaderText
=
"ThumbnailName"
SortExpression
=
"ThumbnailName"
UniqueName
=
"ThumbnailName"
Visible
=
"False"
>
</
telerik:GridBoundColumn
>
<
telerik:GridTemplateColumn
DefaultInsertValue
=
""
UniqueName
=
"rbt_VehicleImage"
>
<
ItemTemplate
>
<
asp:RadioButton
ID
=
"rbt_VehicleImage"
GroupName
=
"MyGroup"
onclick
=
"MyClick(this,event)"
runat
=
"server"
/>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
mastertableview
>
<
clientsettings
>
<
clientevents
onrowdblclick
=
"RowClick"
/>
</
clientsettings
>
</
telerik:RadGrid
>
The grid displays images of a vehicle selected from a database on the key field IDVehicle where tbl_Images.IDVehicle = tbl_Vehicles.IDVehicle
There may be many images for the vehicle so I need to be able to allow the user to select one image as the 'default' or main image to use on reports. To do this I thought it would be simple to add a radio button to the grid so that when the user selectes the radio button the database would be updated so that the DefaultImage field (SQLDataType = BIT) would be set to True for the selected radio button and all of the other buttons with the same IDVehicle would be set to False. Something like:
Protected
Sub
rg_VehicleImages_ItemDataBound(
ByVal
sender
As
Object
,
ByVal
e
As
Telerik.Web.UI.GridItemEventArgs)
Handles
rg_VehicleImages.ItemDataBound
If
TypeOf
e.Item
Is
GridDataItem
Then
Dim
item
As
GridDataItem =
DirectCast
(e.Item, GridDataItem)
Dim
RadioBtn
As
RadioButton =
DirectCast
(item.FindControl(
"rbt_VehicleImage"
), RadioButton)
If
RadioBtn.Checked =
False
Then
Dim
sql
As
String
Dim
strConnString
As
[
String
] = System.Configuration.ConfigurationManager.ConnectionStrings(
"CF_SQL_Connection"
).ConnectionString()
sql =
"UPDATE CF_Images SET DefaultImage = @Value WHERE IDImages = @IDImages"
Dim
connection
As
New
SqlConnection(strConnString)
Dim
command
As
New
SqlCommand(sql, connection)
Dim
IDImages
As
Int32 =
DirectCast
(item.GetDataKeyValue(
"IDImages"
), Int32)
command.Parameters.Add(
"@IDImages"
, SqlDbType.Int).Value = IDImages
command.Parameters.Add(
"@Value"
, SqlDbType.Int).Value =
"0"
command.Connection.Open()
command.ExecuteNonQuery()
command.Connection.Close()
Else
Dim
sql
As
String
Dim
strConnString
As
[
String
] = System.Configuration.ConfigurationManager.ConnectionStrings(
"CF_SQL_Connection"
).ConnectionString()
sql =
"UPDATE CF_Images SET DefaultImage = @Value WHERE IDVehicles = @IDVehicles AND IDImages = @IDImages"
Dim
connection
As
New
SqlConnection(strConnString)
Dim
command
As
New
SqlCommand(sql, connection)
Dim
IDImages
As
Int32 =
DirectCast
(item.GetDataKeyValue(
"IDImages"
), Int32)
Dim
IDVehicles
As
String
= Request.QueryString(
"IDVehicles"
)
command.Parameters.Add(
"@IDVehicles"
, SqlDbType.Int).Value = IDVehicles
command.Parameters.Add(
"@IDImages"
, SqlDbType.Int).Value = IDImages
command.Parameters.Add(
"@Value"
, SqlDbType.Int).Value =
"1"
command.Connection.Open()
command.ExecuteNonQuery()
command.Connection.Close()
End
If
End
If
End
Sub
Of course this does not work. I am not sure if it is because I am attempting this using the Grid ItemDataBound argument and not one associated with the radio button.
Any help much appreciated.