In the insert statement, I can submit the values of the dynamically generated Textbox to the database.Please help fix it.
What I have tried:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControl.ascx.cs" Inherits="UserControl" %>
<style type="text/css">
.style1
{
width: 131px;
}
</style>
<table>
<tr>
<td>
Enter State
</td>
<td>
State ID
</td>
<td class="style1">
State Colour</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtState" runat="server" />
</td>
<td>
<asp:TextBox ID="txtStateId" runat="server" />
</td>
<td class="style1">
<asp:TextBox ID="txtStateClr" runat="server" />
</td>
<td>
<asp:Button ID="btnRemove" runat="server" OnClick="btnRemove_Click" Text="Remove" />
</td>
</tr>
</table>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserControl : System.Web.UI.UserControl
{
public event EventHandler RemoveUserControl;
protected internal void btnRemove_Click(object sender, System.EventArgs e)
{
RemoveUserControl(sender, e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CSNew.aspx.cs" Inherits="CSNew" %>
<%@ Register Src="~/UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="sm1" runat="server" />
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<div class="demo">
<table>
<tr>
<td>
<asp:PlaceHolder ID="ph1" runat="server" />
<br />
<asp:Button ID="btnAdd" runat="server" Text="Add" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
<asp:Literal ID="ltlCount" runat="server" Text="0" Visible="false" />
<asp:Literal ID="ltlRemoved" runat="server" Visible="false" />
<br />
<br />
<asp:GridView runat="server" ID="gvInsertedRecords" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class CSNew : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
AddAndRemoveDynamicControls();
}
private void AddAndRemoveDynamicControls()
{
Control c = GetPostBackControl(Page);
if ((c != null))
{
if (c.ID.ToString() == "btnAdd")
{
ltlCount.Text = (Convert.ToInt16(ltlCount.Text) + 5).ToString();
}
}
ph1.Controls.Clear();
int ControlID = 0;
for (int i = 0; i <= (Convert.ToInt16(ltlCount.Text) - 1); i++)
{
UserControl DynamicUserControl = (UserControl)LoadControl("UserControl.ascx");
while (InDeletedList("uc" + ControlID) == true)
{
ControlID += 1;
}
DynamicUserControl.ID = "uc" + ControlID;
DynamicUserControl.RemoveUserControl += this.HandleRemoveUserControl;
ph1.Controls.Add(DynamicUserControl);
ControlID += 1;
}
}
private bool InDeletedList(string ControlID)
{
string[] DeletedList = ltlRemoved.Text.Split('|');
for (int i = 0; i <= DeletedList.GetLength(0) - 1; i++)
{
if (ControlID.ToLower() == DeletedList[i].ToLower())
{
return true;
}
}
return false;
}
public void HandleRemoveUserControl(object sender, EventArgs e)
{
Button remove = (sender as Button);
UserControl DynamicUserControl = (UserControl)remove.Parent;
ph1.Controls.Remove((UserControl)remove.Parent);
ltlRemoved.Text += DynamicUserControl.ID + "|";
ltlCount.Text = (Convert.ToInt16(ltlCount.Text) - 1).ToString();
}
protected void btnAdd_Click(object sender, System.EventArgs e)
{
}
protected void btnInsert_Click(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("StateId"), new DataColumn("State"), new DataColumn("Statecolour") });
foreach (Control c in ph1.Controls)
{
if (c.GetType().Name.ToLower() == "usercontrol_ascx")
{
UserControl uc = (UserControl)c;
TextBox tbState = uc.FindControl("txtState") as TextBox;
TextBox tbStateId = uc.FindControl("txtStateId") as TextBox;
TextBox tbStateClr= uc.FindControl("txtStateClr") as TextBox;
if (!string.IsNullOrEmpty(tbStateId.Text.Trim()) && !string.IsNullOrEmpty(tbState.Text.Trim()) && !string.IsNullOrEmpty(tbStateClr.Text.Trim()))
{
dt.Rows.Add(tbStateId.Text.Trim(), tbState.Text.Trim(),tbStateClr.Text.Trim());
}
}
}
gvInsertedRecords.DataSource = dt;
gvInsertedRecords.DataBind();
}
private void Insert(string state, string id, string Statecolour)
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO tblStates VALUES (@StateId, @State)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@StateId", id);
cmd.Parameters.AddWithValue("@State", state);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public Control GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if ((ctrlname != null) & ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
return control;
}
}