In this article I will explain how to create dynamic control
inside repeater using placeholder. Using placeholder we can add dynamic control
from code behind like TextBox, RadioButtonList, Checkbox etc (depending upon
the requirement).
In this Example, I am creating small Question form with
different options to give your answer, Like Textbox to enter your answer, Radio
button list to select answer and checkboxlist to select multiple answers.
Click on image to enlarge view
Follow these steps:
1. Create a simple web form in your application.
2. Now add a Repeater control to this web form.
3. Customize repeater control as per your requirement
and add a Placeholder control where
you want to insert Input control.
4. No add ItemDataBound
event handler.
5. Bind Repeater and write code on ItemDataBound
event handler to generate Dynamic control.
Create a Table in database:
Create a Table in database:
Design or Html Part of Demo Application:
<asp:Repeater ID="rptPrint" runat="server" OnItemDataBound="rptPrint_ItemDataBound">
<ItemTemplate>
<table style="width: 7.6in">
<tr>
<td colspan="5" style="width: 7in">
<strong>
<asp:Label ID="LblQuestion" Text='<%#Eval("Question")%>' runat="server"></asp:Label></strong><br />
<asp:HiddenField ID="hfAnswer" Value='<%#Eval("Answer") %>' runat="server" />
<asp:HiddenField ID="hfType" Value='<%#Eval("Type") %>' runat="server" />
</td>
</tr>
<tr>
<td colspan="4">
<asp:PlaceHolder ID="phRow" runat="server" />
<br />
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
Now write the
following code in Code behind:
Add Following Namespace:
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
Now Bind Repeater on Page Load
and Generate Dynamic control on ItemDataBound:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Fill_Repeater();
}
private void Fill_Repeater()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * from Question", con);
DataTable dt = new DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
rptPrint.DataSource = dt;
rptPrint.DataBind();
}
}
protected void rptPrint_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
string options = ((HiddenField)e.Item.FindControl("hfAnswer")).Value;
string type = ((HiddenField)e.Item.FindControl("hfType")).Value;
Label lblquestion = ((Label)e.Item.FindControl("LblQuestion"));
PlaceHolder phRow = (PlaceHolder)e.Item.FindControl("phRow");
if (type == "Text")
{
TextBox txtAnswer = new TextBox();
phRow.Controls.Add(txtAnswer);
}
//Check Box Options for
Questions
else if (type == "Check")
{
CheckBoxList chklist = new CheckBoxList();
chklist.RepeatDirection = RepeatDirection.Horizontal;
chklist.Font.Italic = true;
chklist.RepeatColumns = 4;
foreach (string option in options.Split(','))
{
ListItem items = new ListItem();
items.Text = option;
items.Value = option;
chklist.Items.Add(items);
}
phRow.Controls.Add(chklist);
}
//Radio options for Question
else
{
RadioButtonList rdblist = new RadioButtonList();
rdblist.RepeatDirection = RepeatDirection.Horizontal;
rdblist.Font.Italic = true;
rdblist.RepeatColumns = 4;
foreach (string option in options.Split(','))
{
ListItem items = new ListItem();
items.Text = option;
items.Value = option;
rdblist.Items.Add(items);
}
phRow.Controls.Add(rdblist);
}
}
}
How can we add text boxes on radio list checkbox list on change events in the repeater?
ReplyDeleteThis comment has been removed by the author.
DeleteYou can use place holder to add controls dynamically same as given in above example... if you want then I can post a example.. please explain you requirement so I'll work on that...
ReplyDeleteHow to retrieve the dynamic textbox values
ReplyDeleteI'll post my next article to explain you the concept of getting value and save it to database.
Deletecan you please share link for the mentioned case. Thanks in advance.
Deletealguin me puede ayudar con lo del foreach
ReplyDelete