EDN Admin
Well-known member
<span style="color:#464952; font-family:verdana; font-size:11px Im 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.<br style="color:#464952; font-family:verdana; font-size:11px
<br style="color:#464952; font-family:verdana; font-size:11px
<span style="color:#464952; font-family:verdana; font-size:11px 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 its not working? <br style="color:#464952; font-family:verdana; font-size:11px
<br style="color:#464952; font-family:verdana; font-size:11px
<span style="color:#464952; font-family:verdana; font-size:11px 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) its coming back with a Null
Reference. Why would this be happening? What could I be doing wrong? <br style="color:#464952; font-family:verdana; font-size:11px
<span style="color:#464952; font-family:verdana; font-size:11px Thanks in advance!
<div style="color:black; background-color:white
<pre> <span style="color:green // Setup the UDF repeater on the UserBioInfo Form
rptUserDefFields.ItemCreated += <span style="color:blue new RepeaterItemEventHandler(rptUserDefFields_ItemCreated);
GetUserDefFields();
[/code]
<br/>
<div style="color:black; background-color:white
<pre><br/> <span style="color:blue protected <span style="color:blue void GetUserDefFields()
{
<span style="color:blue using (CRMDataContext CRM = <span style="color:blue new CRMDataContext())
{
UserDefFields = (<span style="color:blue from EventUserDefinedField f <span style="color:blue in CRM.EventUserDefinedFields
<span style="color:blue where f.nID == EventID
<span style="color:blue select f).ToList<EventUserDefinedField>();
}
List<UserDefObject> MyFieldsFormatted = <span style="color:blue new List<UserDefObject>();
List<String> fieldNames = (<span style="color:blue from EventUserDefinedField u <span style="color:blue in UserDefFields
<span style="color:blue select u.FieldName).Distinct().ToList<String>();
<span style="color:blue foreach (<span style="color:blue string fname <span style="color:blue in fieldNames)
{
List<EventUserDefinedField> groupedField = (<span style="color:blue from EventUserDefinedField u <span style="color:blue in UserDefFields
<span style="color:blue where u.FieldName == fname
<span style="color:blue select u).ToList<EventUserDefinedField>();
<span style="color:blue switch (groupedField[0].FieldType)
{
<span style="color:green //For text boxes, we need to create the text box control
<span style="color:green //and add it to a new UserDefField, which we then add to
<span style="color:green //the MyFieldsFormatted list.<br/>
<span style="color:blue case <span style="color:#a31515 "Text Box":
TextBox txtF = <span style="color:blue new TextBox();
txtF.Width = Unit.Pixel(250);
txtF.MaxLength = 250;
txtF.ID = <span style="color:#a31515 "txtBox";
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, txtF, groupedField[0].IsRequired));
<span style="color:blue break;<br/>
<span style="color:green //For check boxes, we need to create the check box control
<span style="color:green //and add it to a new UserDefField, which we then add to
<span style="color:green //the MyFieldsFormatted list.
<span style="color:blue case <span style="color:#a31515 "Check Box":
CheckBox cbF = <span style="color:blue new CheckBox();
cbF.ID = <span style="color:#a31515 "chkBox";
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, cbF, groupedField[0].IsRequired));
<span style="color:blue break;
<span style="color:green //For drop downs, we need to loop through the groupedField
<span style="color:green //list, and add the ListItemValues to a DropDownList control
<span style="color:green //and then add it to a new UserDefField, which will be added
<span style="color:green //the MyFieldsFormatted list.
<span style="color:blue case <span style="color:#a31515 "Drop Down":
DropDownList ddlF = <span style="color:blue new DropDownList();
ddlF.ID = <span style="color:#a31515 "ddl";
<span style="color:blue foreach (EventUserDefinedField g <span style="color:blue in groupedField)
{
ddlF.Items.Add(<span style="color:blue new ListItem(g.ListItemValue));
}
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, ddlF, groupedField[0].IsRequired));
<span style="color:blue break;
<span style="color:green //For cbls, we need to loop through the groupedField
<span style="color:green //list, and add the ListItemValues to a CheckBoxList control
<span style="color:green //and then add it to a new UserDefField, which will be added
<span style="color:green //the MyFieldsFormatted list.
<br/> <span style="color:blue case <span style="color:#a31515 "Check Box List":
CheckBoxList cblF = <span style="color:blue new CheckBoxList();
<span style="color:blue foreach (EventUserDefinedField g <span style="color:blue in groupedField)
{
cblF.Items.Add(<span style="color:blue new ListItem(g.ListItemValue));
}
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, cblF, groupedField[0].IsRequired));
<span style="color:blue break;
}
}
rptUserDefFields.DataSource = MyFieldsFormatted;
rptUserDefFields.DataBind();
<br/>}
<span style="color:blue void rptUserDefFields_ItemCreated(<span style="color:blue object sender, RepeaterItemEventArgs e)
{
<span style="color:blue if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
UserDefObject MyField = (UserDefObject)e.Item.DataItem;
Literal litOpen = <span style="color:blue new Literal();
litOpen.Text = <span style="color:#a31515 "<tr class="userDefinedQ <th>";
e.Item.Controls.Add(litOpen);
Label lblFieldName = <span style="color:blue new Label();
lblFieldName.ID = MyField.userFieldType;
lblFieldName.Text = MyField.userFieldName;
e.Item.Controls.Add(lblFieldName);
Literal litMiddle = <span style="color:blue new Literal();
litMiddle.Text = <span style="color:#a31515 "</th><td>";
e.Item.Controls.Add(litMiddle);
e.Item.Controls.Add(MyField.userField);
Literal litClose = <span style="color:blue new Literal();
litClose.Text = <span style="color:#a31515 "</td></tr>";
}
}
}
[/code]
<pre lang="x-c# 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);
}
}
}[/code]
<br/>
<br/>
<br/>
<br/>
<br/>
View the full article
set of user controls like CheckBoxes, Input and DropDown Lists.<br style="color:#464952; font-family:verdana; font-size:11px
<br style="color:#464952; font-family:verdana; font-size:11px
<span style="color:#464952; font-family:verdana; font-size:11px 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 its not working? <br style="color:#464952; font-family:verdana; font-size:11px
<br style="color:#464952; font-family:verdana; font-size:11px
<span style="color:#464952; font-family:verdana; font-size:11px 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) its coming back with a Null
Reference. Why would this be happening? What could I be doing wrong? <br style="color:#464952; font-family:verdana; font-size:11px
<span style="color:#464952; font-family:verdana; font-size:11px Thanks in advance!
<div style="color:black; background-color:white
<pre> <span style="color:green // Setup the UDF repeater on the UserBioInfo Form
rptUserDefFields.ItemCreated += <span style="color:blue new RepeaterItemEventHandler(rptUserDefFields_ItemCreated);
GetUserDefFields();
[/code]
<br/>
<div style="color:black; background-color:white
<pre><br/> <span style="color:blue protected <span style="color:blue void GetUserDefFields()
{
<span style="color:blue using (CRMDataContext CRM = <span style="color:blue new CRMDataContext())
{
UserDefFields = (<span style="color:blue from EventUserDefinedField f <span style="color:blue in CRM.EventUserDefinedFields
<span style="color:blue where f.nID == EventID
<span style="color:blue select f).ToList<EventUserDefinedField>();
}
List<UserDefObject> MyFieldsFormatted = <span style="color:blue new List<UserDefObject>();
List<String> fieldNames = (<span style="color:blue from EventUserDefinedField u <span style="color:blue in UserDefFields
<span style="color:blue select u.FieldName).Distinct().ToList<String>();
<span style="color:blue foreach (<span style="color:blue string fname <span style="color:blue in fieldNames)
{
List<EventUserDefinedField> groupedField = (<span style="color:blue from EventUserDefinedField u <span style="color:blue in UserDefFields
<span style="color:blue where u.FieldName == fname
<span style="color:blue select u).ToList<EventUserDefinedField>();
<span style="color:blue switch (groupedField[0].FieldType)
{
<span style="color:green //For text boxes, we need to create the text box control
<span style="color:green //and add it to a new UserDefField, which we then add to
<span style="color:green //the MyFieldsFormatted list.<br/>
<span style="color:blue case <span style="color:#a31515 "Text Box":
TextBox txtF = <span style="color:blue new TextBox();
txtF.Width = Unit.Pixel(250);
txtF.MaxLength = 250;
txtF.ID = <span style="color:#a31515 "txtBox";
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, txtF, groupedField[0].IsRequired));
<span style="color:blue break;<br/>
<span style="color:green //For check boxes, we need to create the check box control
<span style="color:green //and add it to a new UserDefField, which we then add to
<span style="color:green //the MyFieldsFormatted list.
<span style="color:blue case <span style="color:#a31515 "Check Box":
CheckBox cbF = <span style="color:blue new CheckBox();
cbF.ID = <span style="color:#a31515 "chkBox";
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, cbF, groupedField[0].IsRequired));
<span style="color:blue break;
<span style="color:green //For drop downs, we need to loop through the groupedField
<span style="color:green //list, and add the ListItemValues to a DropDownList control
<span style="color:green //and then add it to a new UserDefField, which will be added
<span style="color:green //the MyFieldsFormatted list.
<span style="color:blue case <span style="color:#a31515 "Drop Down":
DropDownList ddlF = <span style="color:blue new DropDownList();
ddlF.ID = <span style="color:#a31515 "ddl";
<span style="color:blue foreach (EventUserDefinedField g <span style="color:blue in groupedField)
{
ddlF.Items.Add(<span style="color:blue new ListItem(g.ListItemValue));
}
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, ddlF, groupedField[0].IsRequired));
<span style="color:blue break;
<span style="color:green //For cbls, we need to loop through the groupedField
<span style="color:green //list, and add the ListItemValues to a CheckBoxList control
<span style="color:green //and then add it to a new UserDefField, which will be added
<span style="color:green //the MyFieldsFormatted list.
<br/> <span style="color:blue case <span style="color:#a31515 "Check Box List":
CheckBoxList cblF = <span style="color:blue new CheckBoxList();
<span style="color:blue foreach (EventUserDefinedField g <span style="color:blue in groupedField)
{
cblF.Items.Add(<span style="color:blue new ListItem(g.ListItemValue));
}
MyFieldsFormatted.Add(<span style="color:blue new UserDefObject(groupedField[0].FieldName, groupedField[0].FieldType, cblF, groupedField[0].IsRequired));
<span style="color:blue break;
}
}
rptUserDefFields.DataSource = MyFieldsFormatted;
rptUserDefFields.DataBind();
<br/>}
<span style="color:blue void rptUserDefFields_ItemCreated(<span style="color:blue object sender, RepeaterItemEventArgs e)
{
<span style="color:blue if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
UserDefObject MyField = (UserDefObject)e.Item.DataItem;
Literal litOpen = <span style="color:blue new Literal();
litOpen.Text = <span style="color:#a31515 "<tr class="userDefinedQ <th>";
e.Item.Controls.Add(litOpen);
Label lblFieldName = <span style="color:blue new Label();
lblFieldName.ID = MyField.userFieldType;
lblFieldName.Text = MyField.userFieldName;
e.Item.Controls.Add(lblFieldName);
Literal litMiddle = <span style="color:blue new Literal();
litMiddle.Text = <span style="color:#a31515 "</th><td>";
e.Item.Controls.Add(litMiddle);
e.Item.Controls.Add(MyField.userField);
Literal litClose = <span style="color:blue new Literal();
litClose.Text = <span style="color:#a31515 "</td></tr>";
}
}
}
[/code]
<pre lang="x-c# 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);
}
}
}[/code]
<br/>
<br/>
<br/>
<br/>
<br/>
View the full article