checkboxlist on item,edit and insert form templates

Last post 01-23-2008 9:24 AM by dewaynec. 9 replies.

Sort Posts:

  • checkboxlist on item,edit and insert form templates

    12-12-2007, 10:29 AM
    • Member
      32 point Member
    • dewaynec
    • Member since 08-22-2007, 4:23 PM
    • Posts 22

    Hi all,

     I have a checkboxlist on my insert item template that I want to duplicate in the item and edit item templates showing a check in the selected items. I have a main table and an items table that populates the checklistbox. When items are selected in the insert temp, it writes the values to a field in the main table, seperated by commas. How to I preselect the checkboxlist items based on the stored values (1,3,4,11,)?

    Filed under:
  • Re: checkboxlist on item,edit and insert form templates

    12-13-2007, 10:08 PM

    Hi:

      Assume you're using FormView. In FormView's DataBound event handler you can try FormView.FindControl() method to find the CheckBoxList and loop through it. The logic is like this:

    DataRowView drv = e.Row.DataItem as DataRowView;
    string s=drv["field_name"].ToString();

    string[] s1 = s.Split(',');

    CheckBoxList c = FormView1.FindControl("checkboxlist1") as CheckBoxList;

       for(int i=0;i<c.Items.Count;i++)
            {
                if(s1.Contains(i.ToString()))
                {
                    c.Items[i].Selected = true;

                }
            }

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: checkboxlist on item,edit and insert form templates

    12-17-2007, 11:01 AM
    • Member
      32 point Member
    • dewaynec
    • Member since 08-22-2007, 4:23 PM
    • Posts 22

    Allen,

     Thanks for the reply. What I want to do is re-populate the checkboxlist from values stored in a text box. I was thinking I could pull the comma seperated string (example: 2,4,5,12), convert them to ints, and check the items in a checkbox list with them. Any ideas?

     Thanks

  • Re: checkboxlist on item,edit and insert form templates

    12-18-2007, 3:03 AM

    Hi:

    dewaynec:
     Thanks for the reply. What I want to do is re-populate the checkboxlist from values stored in a text box. I was thinking I could pull the comma seperated string (example: 2,4,5,12), convert them to ints, and check the items in a checkbox list with them. Any ideas?

     

      It all depends on your needs. Could you please paste your current code and tell us what's the expected effect?

    Thanks

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: checkboxlist on item,edit and insert form templates

    12-18-2007, 9:06 AM
    • Member
      32 point Member
    • dewaynec
    • Member since 08-22-2007, 4:23 PM
    • Posts 22

    Allen,

    Well, I guess the goal is this. I want to take a comma seperated list from a text field (3,5,7,12...) and check the corresponding boxes in a checkboxlist control. I have been trying so many variations to try to make it work, I don't even know if the logic of my code is sound anymore. I started out in the wrong event handler in the first place (PropDescList_SelectedIndexChanged)(duh). I moved it to page load but I still don't have it working. Here's the code.

    If FormView1.CurrentMode = FormViewMode.ReadOnly Then

    Dim myControl1 As Control = FormView1.FindControl("PropertyDescLabel")

    Dim myControl2 As Control = FormView1.FindControl("PropDescList")

    Dim sourceString As String = myControl1.ToString

    Dim splitstring() As String = sourceString.Split(",")

    Dim PropDescList As CheckBoxList = CType(myControl2, CheckBoxList)

     

    Dim i As Integer

    For i = 0 To PropDescList.Items.Count - 1

    If (splitstring(i).Contains(i.ToString())) Then

    PropDescList.Items(i).Selected = True

    End If

    Next

     

    End If

    Thanks for the help.

  • Re: checkboxlist on item,edit and insert form templates

    12-19-2007, 1:05 AM

    Hi:

      I think your code should work at least in FormView's PreRender event handler. BTW, if you mean (3,5,7,12..) is the Text of the items instead of index, try this:

    For i = 0 To PropDescList.Items.Count - 1

    If (splitstring(i).Contains(PropDescList.Items(i).Text)) Then

    PropDescList.Items(i).Selected = True

    End If

     

     

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: checkboxlist on item,edit and insert form templates

    12-20-2007, 9:05 AM
    • Member
      32 point Member
    • dewaynec
    • Member since 08-22-2007, 4:23 PM
    • Posts 22

    Allen,

     I think I am making progress. Now I am getting Index was outside the bounds of the array. When I put in a breakpoint, the array has a lenth of 1. It seems like its not splitting and populating the array properly. Here is the code as it appears now...

    1    Protected Sub FormView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
    2            If FormView1.CurrentMode = FormViewMode.ReadOnly Then
    3    
    4                Dim myControl1 As Control = FormView1.FindControl("PropertyDescLabel")
    5                Dim myControl2 As Control = FormView1.FindControl("PropDescList")
    6                'Dim sourceString() As String = myControl1.ToString.Split(",")
    7                Dim myarray As Array = Split(myControl1.ToString, ",")
    8                'Dim splitstring() As String = sourceString.Split(",")
    9                Dim PropDescList As CheckBoxList = CType(myControl2, CheckBoxList)
    10               Dim d As Integer = 0
    11               Dim i As Integer
    12               For i = 0 To PropDescList.Items.Count - 1
    13                   If i <> Nothing Then
    14   
    15   
    16                       If (myarray(i).Contains(PropDescList.Items(i).Value.ToString)) Then
    17   
    18                           PropDescList.Items(i).Selected = True
    19                           'd += 1
    20                       End If
    21                   End If
    22               Next
    23           End If
    
     
  • Re: checkboxlist on item,edit and insert form templates

    12-20-2007, 9:26 PM
    Answer

    Hi:

      Try this please:

     4               Dim myControl1 As Label= FormView1.FindControl("PropertyDescLabel")

    .....

    7   Dim myarray As Array = Split(myControl1.Text, ",")

    ...

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: checkboxlist on item,edit and insert form templates

    01-09-2008, 2:32 PM
    • Member
      32 point Member
    • dewaynec
    • Member since 08-22-2007, 4:23 PM
    • Posts 22

    Allen,

    Just got back from vacation and hope to tackle this again in the next couple of days. Yes it will help to address it as a label though. Good eye.

    Thanks,

    DeWayne

  • Re: checkboxlist on item,edit and insert form templates

    01-23-2008, 9:24 AM
    • Member
      32 point Member
    • dewaynec
    • Member since 08-22-2007, 4:23 PM
    • Posts 22

    Allen,

     I got it. Here is the code. 

    Dim myControl1 As Label = FormView1.FindControl("PropertyDescLabel")
            Dim myControl2 As Control = FormView1.FindControl("PropDescList")
            Dim PropDescList As CheckBoxList = myControl2
            Dim i As Integer
            Dim j As Integer
            Dim myarray As Array = Split(myControl1.Text, ",")
            For i = 0 To PropDescList.Items.Count - 1
                For j = 0 To UBound(myarray)
                    If PropDescList.Items(i).Value.Trim = myarray(j).ToString.Trim Then
    
                        PropDescList.Items(i).Selected = True
    
                    End If
                Next
    
            Next
    

     I had forgotten to set the checkbox list properties Data Value field to the proper database id field. Works great now.

    Now I am getting a Null Reference Exception error on the array declaration when I click the update link in the edit form template though. I need to iron that out next. Making progress though.

     Thanks,
    DeWayne

Page 1 of 1 (10 items)