1
Answer

Using Repeater and Capturing Items Information

Damian Clem

Damian Clem

12y
1.7k
1
I'm really new to ASP.NET and C# so please bare with me.  I have the following source code that builds a repeater control using data from a database.  The repeater is "building" a set of user controls like CheckBoxes, Input and DropDown Lists.

The repeater information gets created on Page_PreRender();  The repeater is loading in fine and the controls are getting rendered correctly on the page. My Problem is I want to capture the data from the items in the repeater so I can save the information back to the database? I have a foreach loop that "should" loop through all the repeater items and grab the data as needed. For some reason it's not working?  

When I debug the source code the items are showing in the Repeater Object and in the foreach Repeater Object.  But in the FindControl() (in the Repeater loop) it's coming back with a Null Reference.   Why would this be happening?  What could I be doing wrong?    
Thanks in advance!


            foreach (RepeaterItem item in rptUserDefFields.Items)
            {
                if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
                {
                    CheckBox chb = (CheckBox)item.FindControl("chkBox");
                    if (chb.Checked)
                    {
                        Debug.WriteLine("I have the following IDs: " + chb.Checked);
                    }


                }
            }


            rptUserDefFields.ItemCreated += new RepeaterItemEventHandler(rptUserDefFields_ItemCreated);
            GetUserDefFields();


        protected void GetUserDefFields()
        {


            using (CRMDataContext CRM = new CRMDataContext())
            {
                UserDefFields = (from EventUserDefinedField f in CRM.EventUserDefinedFields
                              where f.nID == EventID
                              select f).ToList<EventUserDefinedField>();
            }


            List<UserDefObject> MyFieldsFormatted = new List<UserDefObject>();


            List<String> fieldNames = (from EventUserDefinedField u in UserDefFields
                                       select u.FieldName).Distinct().ToList<String>();


            foreach (string fname in fieldNames)
            {
                List<EventUserDefinedField> groupedField = (from EventUserDefinedField u in UserDefFields
                                                            where u.FieldName == fname
                                                            select u).ToList<EventUserDefinedField>();
                switch (groupedField[0].FieldType)
                {
                    //For text boxes, we need to create the text box control
                    //and add it to a new UserDefField, which we then add to 
                    //the MyFieldsFormatted list.
                    case "Text Box":
                        TextBox txtF = new TextBox();
                        txtF.Width = Unit.Pixel(250);
                        txtF.MaxLength = 250;
                        txtF.ID = "txtBox";
                        MyFieldsFormatted.Add(new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, txtF, groupedField[0].IsRequired));
                        break;
                    //For check boxes, we need to create the check box control
                    //and add it to a new UserDefField, which we then add to 
                    //the MyFieldsFormatted list.
                    case "Check Box":
                        CheckBox cbF = new CheckBox();
                        cbF.ID = "chkBox";
                        MyFieldsFormatted.Add(new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, cbF, groupedField[0].IsRequired));
                        break;
                    //For drop downs, we need to loop through the groupedField
                    //list, and add the ListItemValues to a DropDownList control
                    //and then add it to a new UserDefField, which will be added
                    //the MyFieldsFormatted list.
                    case "Drop Down":
                        DropDownList ddlF = new DropDownList();
                        ddlF.ID = "ddl";
                        foreach (EventUserDefinedField g in groupedField)
                        {
                            ddlF.Items.Add(new ListItem(g.ListItemValue));
                        }
                        MyFieldsFormatted.Add(new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, ddlF, groupedField[0].IsRequired));
                        break;
                    //For cbls, we need to loop through the groupedField
                    //list, and add the ListItemValues to a CheckBoxList control
                    //and then add it to a new UserDefField, which will be added
                    //the MyFieldsFormatted list.
                    case "Check Box List":
                        CheckBoxList cblF = new CheckBoxList();
                        foreach (EventUserDefinedField g in groupedField)
                        {
                            cblF.Items.Add(new ListItem(g.ListItemValue));
                        }
                        MyFieldsFormatted.Add(new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, cblF, groupedField[0].IsRequired));
                        break;
                }
            }


            rptUserDefFields.DataSource = MyFieldsFormatted;
            rptUserDefFields.DataBind();
        }


        void rptUserDefFields_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                UserDefObject MyField = (UserDefObject)e.Item.DataItem;


                Literal litOpen = new Literal();
                litOpen.Text = "<tr class=\"userDefinedQ\"><th>";
                e.Item.Controls.Add(litOpen);


                Label lblFieldName = new Label();
                lblFieldName.ID = MyField.userFieldType;
                lblFieldName.Text = MyField.userFieldName;
                e.Item.Controls.Add(lblFieldName);


                Literal litMiddle = new Literal();
                litMiddle.Text = "</th><td>";
                e.Item.Controls.Add(litMiddle);


                e.Item.Controls.Add(MyField.userField);


                Literal litClose = new Literal();
                litClose.Text = "</td></tr>";
            }


            }
        }



Answers (1)