Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding Checkbox to Values from Database on Page_Load 2

Status
Not open for further replies.

eb24

Programmer
Dec 17, 2003
240
US
I have a checkox on a web form that I would on Page_Load to display it's value from the database, i.e. either checked or unchecked. The database field name is 'Emailed' and set to type BIT, thus only having 1 or 0.

Now on my ASP.NET web form for updating, I have a Data Reader that checks the values from the database and displays these values via textboxes, dropdownlists, and checkboxes, but I am having trouble displaying the checkboxes correct.

For example, if the value in the DB is 1, then I want the checkbox to be CHECKED and vice versa. But I am seeing is the word 'TRUE' or 'FALSE' next to my checkboxes.

This is what I have:

<asp:CheckBox ID=&quot;chkEmail&quot; Runat=&quot;server&quot; Text=&quot;<%# strEmailed %>&quot; />
[/red]
I am certain I have to incorporate the following but it doesn't work:

Checked='<%# DataBinder.Eval(Container, &quot;DataItem.Emailed&quot;) %>'
[/red]
FYI, in my code section I have something like this:

' a lot of code has been intentionally left out[/red]
Dim ..., strEmailed, ... AS String

Sub BindData()
While objDR.Read()
...
strEmailed=objDR(&quot;Emailed&quot;)
...
End While
page.databind()
End Sub
[/blue]

Anyone's assistance will be greatly appreciated.
 
try this
Code:
Checked='<%# Boolean.Parse(DataBinder.Eval(Container, &quot;DataItem.Emailed&quot;)) %>'

in your html.

Alternatively, in your code behind, you san set the CheckBox's Checked property to a Boolean value, such as what you might get from your datareader, during the ItemDataBoundEvent of the DataGrid, DataList or whatever. I am a C# programmer, so I'm afraid my VB.NET would not be very accurate for an example of it.

HTH

David
[pipe]
 
dragonwell:

Thanks for you post. I tried entering your code:

Checked='<%# Boolean.Parse(DataBinder.Eval(Container, &quot;DataItem.Emailed&quot;)) %>'
[/blue]
but I received the following error:

DataBinder.Eval: 'ASP.Audit_8_aspx' does not contain a property with the name DataItem
[/red]
So given this, I guess I should probably use a Datalist to show all of my stuff, i.e. textboxes, dropdownlists, and checkboxes, or is there another way just with what I have right now, i.e. DataReader?

Thanks once again.
 
If you have a single checkbox, can you do it in the code?
Code:
bool emailed = false
While objDR.Read()
 ...
 emailed = Convert.ToBoolean(objDR(&quot;Emailed&quot;)
 
 ...
End While
chkEmail.Checked = emailed
 
OK - I was thinking you were trying to check a box within a list control, but actually the CheckBox is a stand-alone control on the page?

If so, you already have it as a class member so just set it's Checked property to a Boolean value that you set from your DataReader.
Code:
Sub BindData()

    Dim isEmailed As Boolean
    While objDR.Read()
         ...
             isEmailed = objDR.GetBoolean(&quot;Emailed&quot;)
         ...
    End While

    Me.chkEmail.Checked = isEmailed

    page.databind()
End Sub

Hope that clears it up!


David
[pipe]
 
LV and dragonwell:

Thanks for your posts. I finally got it to work beautifully with your very informative posts!

Thanks for the assistance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top