Getting Databinded CheckBoxlist to display Boolean value ?

Discussion in 'ASP .Net' started by Patrick.O.Ige, Jul 28, 2005.

  1. I'm DataBinding a CheckBoxList and i want to get the checkboxes selected
    when the page is loaded depending on a Boolean value from the Database..
    chkDebtor.DataSource = objDR
    chkDebtor.DataValueField = "checked" -- this is a column in the
    table that returns 1 or 0
    chkDebtor.DataTextField = "DebtorCode"
    chkDebtor.DataBind()

    Dim i As Integer = 0
    While i < chkDebtor.Items.Count
    chkDebtor.Items(i).Selected =
    Microsoft.VisualBasic.IIf((chkDebtor.Items(i).Value = "1"), True, False)
    System.Math.Min(System.Threading.Interlocked.Increment(i),
    i - 1)
    End While

    The above code works
    But i have some problems I need another DataValueField which is
    "DebtorCodeID" but since i need the boolean value i used
    the chkDebtor.DataValueField = "checked"
    I need the "DebtorCodeID" later in my code.
    My question is what other ways can i loop through the checkblxlist without
    setting the DataValueField = "checked" but to
    DataValueField = "DebtorCodeID"
    Is there a way to get the Checked boolean values and pass it to the
    checkboxlist to get False or TRUE?
    Patrick.O.Ige, Jul 28, 2005
    #1
    1. Advertising

  2. I think, A while back I faced a similar problem and I found it easier to use
    a Repeater control with checkboxes in it instead of a CheckBoxList

    While using Repeater control, you will be able to easily achive this with the
    ItemDataBound event..

    pls let me know , if you need help in implementing this with Repeater

    Sreejith

    "Patrick.O.Ige" wrote:

    > I'm DataBinding a CheckBoxList and i want to get the checkboxes selected
    > when the page is loaded depending on a Boolean value from the Database..
    > chkDebtor.DataSource = objDR
    > chkDebtor.DataValueField = "checked" -- this is a column in the
    > table that returns 1 or 0
    > chkDebtor.DataTextField = "DebtorCode"
    > chkDebtor.DataBind()
    >
    > Dim i As Integer = 0
    > While i < chkDebtor.Items.Count
    > chkDebtor.Items(i).Selected =
    > Microsoft.VisualBasic.IIf((chkDebtor.Items(i).Value = "1"), True, False)
    > System.Math.Min(System.Threading.Interlocked.Increment(i),
    > i - 1)
    > End While
    >
    > The above code works
    > But i have some problems I need another DataValueField which is
    > "DebtorCodeID" but since i need the boolean value i used
    > the chkDebtor.DataValueField = "checked"
    > I need the "DebtorCodeID" later in my code.
    > My question is what other ways can i loop through the checkblxlist without
    > setting the DataValueField = "checked" but to
    > DataValueField = "DebtorCodeID"
    > Is there a way to get the Checked boolean values and pass it to the
    > checkboxlist to get False or TRUE?
    >
    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Jul 28, 2005
    #2
    1. Advertising

  3. Yes i think i would need to look at Repeater then.
    It would be nice if you can provide me some info.
    Thx


    "Sreejith Ram" <> wrote in message
    news:D...
    > I think, A while back I faced a similar problem and I found it easier to

    use
    > a Repeater control with checkboxes in it instead of a CheckBoxList
    >
    > While using Repeater control, you will be able to easily achive this with

    the
    > ItemDataBound event..
    >
    > pls let me know , if you need help in implementing this with Repeater
    >
    > Sreejith
    >
    > "Patrick.O.Ige" wrote:
    >
    > > I'm DataBinding a CheckBoxList and i want to get the checkboxes selected
    > > when the page is loaded depending on a Boolean value from the Database..
    > > chkDebtor.DataSource = objDR
    > > chkDebtor.DataValueField = "checked" -- this is a column in

    the
    > > table that returns 1 or 0
    > > chkDebtor.DataTextField = "DebtorCode"
    > > chkDebtor.DataBind()
    > >
    > > Dim i As Integer = 0
    > > While i < chkDebtor.Items.Count
    > > chkDebtor.Items(i).Selected =
    > > Microsoft.VisualBasic.IIf((chkDebtor.Items(i).Value = "1"), True, False)
    > >

    System.Math.Min(System.Threading.Interlocked.Increment(i),
    > > i - 1)
    > > End While
    > >
    > > The above code works
    > > But i have some problems I need another DataValueField which is
    > > "DebtorCodeID" but since i need the boolean value i used
    > > the chkDebtor.DataValueField = "checked"
    > > I need the "DebtorCodeID" later in my code.
    > > My question is what other ways can i loop through the checkblxlist

    without
    > > setting the DataValueField = "checked" but to
    > > DataValueField = "DebtorCodeID"
    > > Is there a way to get the Checked boolean values and pass it to the
    > > checkboxlist to get False or TRUE?
    > >
    > >
    > >
    > >
    Patrick.O.Ige, Jul 28, 2005
    #3
  4. A DATALIST you need, would look like below
    *****************************************
    <asp:DataList id="DataList1" RepeatDirection="Horizontal"
    RepeatLayout="Table" RepeatColumns="3"
    runat="server">
    <ItemTemplate>
    <asp:CheckBox ID="Checkbox1"
    checked='<%#CheckYesNo(DataBinder.Eval(Container.DataItem,
    "Checked").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem,
    "Chktext")%>' Runat=server>
    </asp:CheckBox>
    <asp:Label visible=False Runat=server ID="Label2"
    Text='<%#DataBinder.Eval(Container.DataItem, "Chkvalue")%>' >
    </asp:Label>
    </ItemTemplate>
    </asp:DataList>
    *****************************************
    Please notice the checked='<%#CheckYesNo(DataBinder.Eval(Container.DataItem,
    "Checked").ToString())%>'

    this function would look like

    protected bool CheckYesNo(string val)
    {
    if(val=="1")
    return(true);

    return(false);
    }

    *****************************************

    Value/ID that you do not want to display , but still store with checkbox

    <asp:Label visible=False Runat=server ID="Label1"
    Text='<%#DataBinder.Eval(Container.DataItem, "Chkvalue")%>' > </asp:Label>

    *****************************************


    hope this helps



    "Patrick.O.Ige" wrote:

    > Yes i think i would need to look at Repeater then.
    > It would be nice if you can provide me some info.
    > Thx
    >
    >
    > "Sreejith Ram" <> wrote in message
    > news:D...
    > > I think, A while back I faced a similar problem and I found it easier to

    > use
    > > a Repeater control with checkboxes in it instead of a CheckBoxList
    > >
    > > While using Repeater control, you will be able to easily achive this with

    > the
    > > ItemDataBound event..
    > >
    > > pls let me know , if you need help in implementing this with Repeater
    > >
    > > Sreejith
    > >
    > > "Patrick.O.Ige" wrote:
    > >
    > > > I'm DataBinding a CheckBoxList and i want to get the checkboxes selected
    > > > when the page is loaded depending on a Boolean value from the Database..
    > > > chkDebtor.DataSource = objDR
    > > > chkDebtor.DataValueField = "checked" -- this is a column in

    > the
    > > > table that returns 1 or 0
    > > > chkDebtor.DataTextField = "DebtorCode"
    > > > chkDebtor.DataBind()
    > > >
    > > > Dim i As Integer = 0
    > > > While i < chkDebtor.Items.Count
    > > > chkDebtor.Items(i).Selected =
    > > > Microsoft.VisualBasic.IIf((chkDebtor.Items(i).Value = "1"), True, False)
    > > >

    > System.Math.Min(System.Threading.Interlocked.Increment(i),
    > > > i - 1)
    > > > End While
    > > >
    > > > The above code works
    > > > But i have some problems I need another DataValueField which is
    > > > "DebtorCodeID" but since i need the boolean value i used
    > > > the chkDebtor.DataValueField = "checked"
    > > > I need the "DebtorCodeID" later in my code.
    > > > My question is what other ways can i loop through the checkblxlist

    > without
    > > > setting the DataValueField = "checked" but to
    > > > DataValueField = "DebtorCodeID"
    > > > Is there a way to get the Checked boolean values and pass it to the
    > > > checkboxlist to get False or TRUE?
    > > >
    > > >
    > > >
    > > >

    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Jul 29, 2005
    #4
  5. Sreejith thx for the hint..
    Using DataList was a good workaround but after thinking over the weekend
    i came out with a solution using
    FindByValue Method of the LisItem Collection which searches the
    collection for a ListItem with a Value property that contains the
    specified value and what i did is if it finds the value i set the
    checkbox to TRUE.But if it doesn't it returns NOTHING..
    See the Msdn documentation here:-
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/h
    tml/frlrfSystemWebUIWebControlsListItemCollectionClassFindByValueTopic.a
    sp
    Patrick


    *** Sent via Developersdex http://www.developersdex.com ***
    Patrick Olurotimi Ige, Aug 1, 2005
    #5
  6. Patrick.O.Ige

    sana_elka

    Joined:
    Oct 30, 2006
    Messages:
    1
    Displaying the value of an item in a table in a checkbox (in a datagrid)

    I have a datagrid with at least 6 columns. The last column contains an item template with has 5 checkboxes labeled 0,1, 2, 3, 4 . These checkboxes are referred to the level of difficulty of a certain course depending on the value of that item in the database. The name of that item in a database is "Level". I have tried to get it to work but i wasnt able to.
    HELP! :) :loo:
    sana_elka, Oct 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    How to add DataBinded CheckBoxList in Datalist

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Jun 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,591
    Patrick Olurotimi Ige
    Jun 17, 2005
  2. Patrick.O.Ige
    Replies:
    5
    Views:
    19,059
    Patrick.O.Ige
    Jun 19, 2005
  3. Patrick Olurotimi Ige
    Replies:
    0
    Views:
    1,461
    Patrick Olurotimi Ige
    Jun 22, 2005
  4. J Leonard
    Replies:
    4
    Views:
    12,598
    Mark Space
    Jan 19, 2008
  5. Metre Meter
    Replies:
    7
    Views:
    350
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page