Hi ashokpumca,
I have created sample code which fullfill your requirement.
HTML
<div>
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=ChkBatchSize]').on('click', function () {
if ($(this).is(':checked')) {
$('[Id*=hdId]').val($(this).attr('value'));
}
});
});
</script>
</div>
<asp:HiddenField ID="hdId" runat="server" />
<asp:Panel ID="phold" runat="server">
</asp:Panel>
<table>
<tr>
<td style="width: 250px">
<b>Select Batch Sizes</b>
</td>
<td style="width: 412px">
<asp:CheckBoxList ID="ChkBatchSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ChkBatchSize_SelectedIndexChanged"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
</td>
<td style="width: 250px">
</td>
</tr>
<tr id="Tr1" runat="server">
<td style="width: 250px">
<b>Mother Batch </b>
</td>
<td style="width: 412px">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
<td style="width: 250px">
</td>
</tr>
</table>
</div>
C#
private string constring = ConfigurationManager.ConnectionStrings["conString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetCustomer();
}
}
private void GetCustomer()
{
SqlConnection con = new SqlConnection(constring);
string strQuery = "SELECT * FROM Customers";
SqlCommand cmd = new SqlCommand(strQuery, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["Name"].ToString();
item.Value = sdr["CustomerId"].ToString();
ChkBatchSize.Items.Add(item);
}
}
con.Close();
cmd.Dispose();
}
protected void ChkBatchSize_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem checkbox in ChkBatchSize.Items)
{
if (checkbox.Selected)
{
if (checkbox.Value == hdId.Value)
{
TextBox tb = new TextBox();
tb.ID = "txtDynamic" + checkbox.Value; //Adding checkbox value to Textbox for creating unique textbox ID (e.g. txtDynamic46).
tb.CssClass = "textnow";
phold.Controls.Add(tb);
Literal lt = new Literal();
lt.Text = "<br/>";
phold.Controls.Add(lt);
}
}
else
{
foreach (Control item in phold.Controls.OfType<Control>())
{
if (item.ID == "txtDynamic" + checkbox.Value)
{
phold.Controls.Remove(item);
}
}
}
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
foreach (string key in keys)
{
this.CreateTextBox("txtDynamic" + i);
i++;
}
}
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
phold.Controls.Add(txt);
Literal lt = new Literal();
lt.Text = "<br />";
phold.Controls.Add(lt);
}
Screenshot