Checkbox values expect a Boolean value, so you'll have to set that when binding. Convert your logic then you should be fine.
So when updating I have to do formview.findcontrol("chkbox") and depending on if it's checked-set the objectdatsource updateparameter to Y or N. Is that what you mean? Is there no faster way? (I have about 30 checkboxes like that.
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim cb As CheckBox = CType(GridView1.Rows(e.RowIndex).FindControl("CheckBoxprop_View"), CheckBox)
If cb.Checked Then
e.NewValues("prop_view") = "Y"
Else
e.NewValues("prop_view") = "N"
End If
End Sub
pulsmartin
Member
108 Points
309 Posts
Checkbox databinding and Y/N values
Nov 27, 2012 07:57 PM|LINK
In my db I have a string field with either Y or N as value. I want to bind that to my checkbox (Y=checked)
I have the following
<asp:CheckBox ID="CheckBoxprop_view" runat="server" checked='<%# DataBinder.Eval(Container.DataItem, "prop_view") = "Y" %>' />
But how do I send the checked value back to the db?
mebinici
Participant
815 Points
256 Posts
Re: Checkbox databinding and Y/N values
Nov 27, 2012 08:26 PM|LINK
Checkbox values expect a Boolean value, so you'll have to set that when binding. Convert your logic then you should be fine.
Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
oned_gk
All-Star
31764 Points
6492 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 12:34 AM|LINK
<asp:CheckBox ID="CheckBoxprop_view" runat="server" checked='<%# Eval("prop_view") = "Y" ? True : False %>'pulsmartin
Member
108 Points
309 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 04:31 AM|LINK
Checkbox values expect a Boolean value, so you'll have to set that when binding. Convert your logic then you should be fine.
So when updating I have to do formview.findcontrol("chkbox") and depending on if it's checked-set the objectdatsource updateparameter to Y or N. Is that what you mean? Is there no faster way? (I have about 30 checkboxes like that.
this gives me a syntax error - not allowed to use ? at this place - I code using vb
oned_gk
All-Star
31764 Points
6492 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 06:06 AM|LINK
<asp:CheckBox ID="CheckBoxprop_view" runat="server" checked='<%# IIF(Eval("prop_view")="Y",True,False) %>'Or maybe wrong ==
<asp:CheckBox ID="CheckBoxprop_view" runat="server" checked='<%# Eval("prop_view") == "Y" ? True : False %>'pulsmartin
Member
108 Points
309 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 06:58 AM|LINK
But will this send the value back to database when updating? That's really my problem. To just "eval" my code in first post works.
oned_gk
All-Star
31764 Points
6492 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 07:11 AM|LINK
I think not, if you want two way binding you need change prop_view to bit datatype in ssql
then
checked='<%# Bind("prop_view") %>'pulsmartin
Member
108 Points
309 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 09:36 AM|LINK
well I can't do that. What other options do I have?
mebinici
Participant
815 Points
256 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 02:44 PM|LINK
Set/Get the values when data binding in your code behind.
Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
oned_gk
All-Star
31764 Points
6492 Posts
Re: Checkbox databinding and Y/N values
Nov 28, 2012 11:33 PM|LINK
VB
<asp:CheckBox ID="CheckBoxprop_view" runat="server" checked='<%# IIF(Eval("prop_view")="Y",True,False) %>'Then when updating
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim cb As CheckBox = CType(GridView1.Rows(e.RowIndex).FindControl("CheckBoxprop_View"), CheckBox) If cb.Checked Then e.NewValues("prop_view") = "Y" Else e.NewValues("prop_view") = "N" End If End Sub