2014年7月13日 星期日

[RESOLVED] Checkbox numeric value 0/-1


In my web site I have a bunch of checkboxes but unfortunately, ASP evaluates true as 1. The database I'm working with requires true to evaluate to -1.


How can I convert checkbox.checked to -1 and bind that? So far in my aspx source code I have:


asp:Checkbox ID="AFCanInstCheckbox" runat="server" OnCheckedChanged="CheckboxValue"

How can I bind -1/0 to the checkbox to be submitted to the database instead of 0/1? I'm guessing I can either do code-behind or some function in the bind statement but I'm a bit stuck. Does anyone have suggestions?


 



Hi,


Its just a maths!




jwang592


ASP evaluates true as 1.


Yes its true. While checked it return 1 and unchecked it return 0.


Dim intToDB as integer
intToDB = cint(chk.selected) * (-1) ' Now you get -1 Store it in DB

// Same way get the value from DB

dim intFromDB as integer
intFromDB = -1 * (-1) // which gives 1 Bind it to check box

Hope this helps!


thanks!





If you have bit field, you can pass it by boolean type parameter. pass true / false value. Or Direct Checked ='<% # bind("fieldname") %>'

Thanks for the answer! I realized the same solution just after I posted my original question.


I was hoping for a more elegant answer but this works as well


沒有留言:

張貼留言