ASP.NET checkbox value store in SQL Server - asp.net

I using FormView in my application. I found that no value property in asp checkbox is available! Following code for my check box (it displays True/False values from SQL Server).
So I just need to be able to add True/False or "on" value from checkbox to database when I saves form.
Thanks for help.
<asp:CheckBox runat="server"
CssClass="checkbox"
Text="Medicine"
ID="lic_board_medicine_chk"
TextAlign="Right"
Checked='<%# Eval("lic_board_medicine_chk").ToString() == "True" ? true : false%>'/>

try changing Eval to BIND
<asp:CheckBox runat="server"
CssClass="checkbox"
Text="Medicine"
ID="lic_board_medicine_chk"
TextAlign="Right"
Checked='<%# Bind("lic_board_medicine_chk").ToString() == "True" ? "checked" : "" %>'/>

Related

Using Conditional Statement in ASP.NET Control Attribute

I have a DropDownList on my page and I would like the DataTextField value to be based on a condition (language). I'm using Model Binding and this DropDown is nested in a bound FormView.
This is what I'm trying to do:
<asp:DropDownList ID="DropDownList1" ItemType="BLL.HelperClasses.ItemForList"
DataValueField="id" DataTextField="<%#: (this.IsEnglish) ? "en" : "fr" %>" SelectMethod="DropDownList1_GetData"
SelectedValue="<%#: Item.Claim.CurrencyID %>" runat="server"></asp:DropDownList>
.NET is complaining that my DataTextField "server tag is not well formed". IsEnglish is a boolean property in my base page.
Anyone know how to do this without using the code-behind?
I think you have to have "Eval" inserted so for it to actually compare them don't you?
<asp:label id="Label1" runat="server" text="<%# Eval("StReply") == true ? "yes" : "no" %>" xmlns:asp="#unknown">
</asp:label>
Code was used from http://www.codeproject.com/Questions/653888/using-If-statement-in-Bind-Eval
Problem was with the surrounding double-quotes. Changed them to single-quotes and everything works perfectly.
Final solution:
<asp:DropDownList ID="DropDownList1" ItemType="BLL.HelperClasses.ItemForList"
DataValueField="id" DataTextField='<%#: (this.IsEnglish) ? "en" : "fr" %>' SelectMethod="DropDownList1_GetData"
SelectedValue="<%#: Item.Claim.CurrencyID %>" runat="server"></asp:DropDownList>

Using iif function in checkbox

I am working in a project where I need to display data from database in a gridview. The gridview contains varchar fields could be whether YES or NO. So I want to display rather than the text, checkbox. When its YES check box will be checked, when its NO check box will be unchecked.
My asp.net
<asp:CheckBox ID="chkCheck" runat="server" Checked='<%#"IIF(Eval("Check")="YES",true,false)%>' Width="80px" Enabled="false"/>
I am getting this error:
Character constant must contain exactly one character.
Is there any alternative option would solve my issue?
You can simply use this (for VB.NET)
Checked='<%# Eval("Check")="YES" %>'
For null values try
Checked='<%# Convert.ToString(Eval("Check"))="Yes" %>'
I'm not sure about your error, but do you need the IIF? I'm no expert in ASP, I'm relatively new to it, but why can't you just do Checked='<%# Eval("Checked") == "yes" %>'? And beyond that what about Checked='<%# Item.Checked == "yes" %>'?
Performance hint: Use DataRowView instead of Eval within the GridView control:
Checked='<%# (((System.Data.DataRowView)Container.DataItem)["Check"].ToString() == "YES") ? true : false %>'

Asp.Net - Write True/False on UI based on boolean value of checkbox

I am working on this form in VS2012 and SQL Server 2008R2. The table gets the bit value of true or false. In the Gridview itemtemplate, instead of displaying the empty checkbox I want to say "True or False". How can I do that? This is what I have done so far:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# (Eval("Banned").ToString())%>' />
You need to set the Text property of the CheckBox control, like this:
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# DisplayTruth(Eval("Banned").ToString()) %>'
Text='<%# DisplayTruthValue(Eval("Banned").ToString()) %>' />
Now in your code-behind, create the two methods, like this:
protected bool DisplayTruth(string banned)
{
if(banned == "0")
{
return true;
}
return false;
}
protected string DisplayTruthValue(string banned)
{
if(banned == "0")
{
return "True";
}
return "False";
}
I recommend this approach for the following reasons:
The markup does not contain conditional/ternary logic
It is easier to debug the logic versus embedded code blocks
You can leverage the power of Visual Studio compiler to catch syntax errors at compile-time versus run-time errors when the logic is embedded into the binding syntax of the markup
I found this information and it is working. Also I changed asp:checkbox to asp:label. However I want to display "No" instead of "False" so I changed "False" to "No" but still my app says "False".
<asp:Label ID="CheckBox1" runat="server" Text='<%# IIf(Convert.ToString(Eval("Banned")) = "", "No", Eval("Banned"))%>' />

Retrieve stored checkbox value from sql server database to re-enable it

I have checkbox value in the gridview which is bound to item template in this manner:
<ItemTemplate>
<asp:CheckBox ID="CheckBoxDisable" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxDisableEvent"/>
</ItemTemplate>
And i'm implementing the CheckBoxDisableEvent method in this manner which does nothing but store the checkbox checked value into sql server table.
SqlCommand cmd = new SqlCommand("InsertDisableFlagIntoComponents", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#disableFlag", SqlDbType.Int).Value = disableFlag;
Now the problem is when the gridview loads on the start of the project next time, i want to enable all those checkboxes depending upon the value that i have stored in database during previous events.
How can i enable the checkbox values in gridview depending on the values that i stored in sql server table.
BTW i'm using vs2008 with asp.net/C#, sqlserver 2005
Thanks in anticipation
<asp:CheckBox ID="CheckBoxDisable" runat="server" Enabled='<%# Convert.ToBoolean(Eval("ColumnName")) ? true : false %>' Checked='<%# Eval("ColumnName") %>' />
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("ColumnName") == true ? false : true %>' />

Binding RadioButtonList to nullable varchar workaround? (SelectedValue which is invalid because it does not exist...)

I have a nullable varchar column in a table in my database.
I am binding a gridview to this table via an objectdatasource and have a TemplateField like so:
<asp:RadioButtonList ID="rblRequirementOption" RepeatDirection="Horizontal" runat="server" RepeatLayout="Flow"
SelectedValue='<%#Bind("RequirementOption")%>'>
<asp:ListItem Value="" Text="" style="display: none" />
<asp:ListItem Value="Need" Text="Need"></asp:ListItem>
<asp:ListItem Value="Do Not Use" Text="Do Not Use"></asp:ListItem>
</asp:RadioButtonList>
If there are any null values in that column in the table, I get the error:
"'rblRequirementOption' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value"
So as you can see, I have tried to add a placeholder radio button with a value of "" to accomodate the null values, which is the commonly advised method to handle this error, but in this case, it seems that "" and DBNull.Value are not considered equivalent.
Surely I don't have to convert nulls to a zero length string before binding do I?
UPDATE
Actually, this is the correct syntax to do it. (I was futzing around with a default value substitute and got all mixed up.)
you shoud use:
<asp:RadioButtonList runat=server ID="rd" SelectedValue='<%# Bind("sex").GetType() == typeof(DBNull) ? null : Bind("sex") %>'
<asp:ListItem Text="male" Value="1"></asp:ListItem>
<asp:ListItem Text="female" Value="2"></asp:ListItem>
</asp:RadioButtonList>
I'm not sure what DB you're using, but I'd start by using the NullIf statement for the nullable database fields.
Select NullIf(RequirementOption, 'None') From ...
That will help you avoid the Object Reference Not Set to an Instance of an Object errors because you won't be returning nulls from the data store.

Resources