Hi micah,
Check this example. Now please take its reference and correct your code.
If you don't want to use Button to insert the record to Database you need to use TextBox OnTextChanged event with AutoPostBack="true".
HTML
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
Comment 1:
<asp:Label ID="Comment1ID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
<asp:TextBox ID="txtcomment1" runat="server" AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox>
<asp:DataList ID="DataList2" runat="server" OnItemDataBound="DataList2_ItemDataBound">
<ItemTemplate>
Comment 2:
<asp:Label ID="Comment2ID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
<asp:TextBox ID="txtcomment2" runat="server" AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox>
<asp:DataList ID="DataList3" runat="server">
<ItemTemplate>
Comment 3:
<asp:Label ID="Comment3ID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
<asp:TextBox ID="txtcomment3" runat="server" AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList1.DataSource = BindDataList();
DataList1.DataBind();
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList orderDataList = e.Item.FindControl("DataList2") as DataList;
orderDataList.DataSource = BindDataList();
orderDataList.DataBind();
}
}
protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList orderDataList = e.Item.FindControl("DataList3") as DataList;
orderDataList.DataSource = BindDataList();
orderDataList.DataBind();
}
}
protected void OnTextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox.ID == "txtcomment1")
{
int comment1ID = Convert.ToInt32((textBox.NamingContainer.FindControl("Comment1ID") as Label).Text);
string comment1 = textBox.Text.Trim();
Insert(comment1ID, comment1, "CommentA");
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + comment1 + " inserted into CommentA table')", true);
}
if (textBox.ID == "txtcomment2")
{
int comment2ID = Convert.ToInt32((textBox.NamingContainer.FindControl("Comment2ID") as Label).Text);
string comment2 = textBox.Text.Trim();
Insert(comment2ID, comment2, "CommentB");
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + comment2 + " inserted into CommentB table')", true);
}
if (textBox.ID == "txtcomment3")
{
int comment3ID = Convert.ToInt32((textBox.NamingContainer.FindControl("Comment3ID") as Label).Text);
string comment3 = textBox.Text.Trim();
Insert(comment3ID, comment3, "CommentC");
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + comment3 + " inserted into CommentC table')", true);
}
}
private DataTable BindDataList()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Rows.Add(1);
return dt;
}
private void Insert(int id, string comment, string tableName)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "INSERT INTO " + tableName + " (ID,Comment) VALUES (@Id,@Comment)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Comment", comment);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Screenshot