hi all,
Im working with a asp.net c# application and using sql server, as my database and using stored procedures.
And i've to generate dynamic check box and button's.which is users can click the checkbox and when user can submit at the time i have to get checked checkbox values and store into DB.
My question is: How to get dynamic checkbox checked values?
I have a code for creating dynamic checkbox and button
This is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
public partial class Check_box_checkbox : System.Web.UI.Page
{
#region global var
AddValuesClass objAddValuesClass;
DataSet ds;
DataTable dt;
CheckBox cbid = new CheckBox();
#endregion
protected void Page_Load(object sender, EventArgs e)
{
generateckbox();
}
#region generate checkbox
public void generateckbox()
{
try
{
//creating a table dynamically
HtmlTable table = new HtmlTable();
HtmlTableRow tr = null;
HtmlTableCell tc = null;
// creating classfile obj
objAddValuesClass = new AddValuesClass();
//dataset obj
dt = new DataTable();
ds = objAddValuesClass.getmenuvalues1();
dt=ds.Tables[0];
//displaying labels for displaying column names in the table
if (dt.Columns.Count - 1 > 0)
{
tr = new HtmlTableRow();
for (int i = 0; i < dt.Columns.Count; i++)
{
tc = new HtmlTableCell();
Label lbl = new Label();
lbl.Text = dt.Columns[i].ColumnName;
lbl.ID = "lbl" + dt.Columns[i].ColumnName;
tc.Height = "50px";
tc.Width = "150px";
tc.Controls.Add(lbl);
tr.Controls.Add(tc);
table.Controls.Add(tr);
}
//creating checkbox for displaying records information
for (int j = 0; j < dt.Rows.Count; j++)
{
tr = new HtmlTableRow();
for (int k = 0; k < dt.Columns.Count; k++)
{
tc = new HtmlTableCell();
if (dt.Columns[k].ColumnName == "Name")
{
Label lbl = new Label();
lbl.ID = "lbl" + j + k;
lbl.Text = dt.Rows[j][dt.Columns[k].ToString()].ToString();
tc.Controls.Add(lbl);
tr.Controls.Add(tc);
}
else
{
CheckBox cbid = new CheckBox();
cbid.ID = "cbid" + j + k;
//cb.Text = dt.Rows[j][dt.Columns[k].ToString()].ToString();
tc.Controls.Add(cbid);
tr.Controls.Add(tc);
}
}
table.Controls.Add(tr);
}
//Creating Button
for (int b = 1; b <= 2; b++)
{
tc = new HtmlTableCell();
Button btn = new Button();
if (b == 1)
{
btn.ID = "btnadd" + b;
btn.Text = "Add";
btn.Click += new EventHandler(btn_Click);
tc.Controls.Add(btn);
tr.Controls.Add(tc);
}
else
{
btn.ID = "btnclear" + b;
btn.Text = "clear";
tc.Controls.Add(btn);
tr.Controls.Add(tc);
}
}
form1.Controls.Add(table);
}
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
}
#endregion
#region button Event handler
protected void btn_Click(object sender, EventArgs e)
{
try
{
// here i have to write that code
}
catch
{
}
}
#endregion
}
Please anyone help me
Thank u
raajsj 0
Newbie Poster
Recommended Answers
Jump to PostIts sloppy, but works. But first you need to add an ID to the table so you can find it. After that you can loop through it and get your values.
table.ID = "dataTable"; protected void btn_Click(object sender, EventArgs e) { try { HtmlTable t = …
Jump to PostAre you asking how to add the results to a database call?
sqlConnection = new SqlConnection(dbConnectionString); SqlCommand command = new SqlCommand("sp_StoredProcedure", sqlConnection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@Id", SqlDbType.VarChar).Value = cbid.Text; command.Parameters.Add("@Checked", SqlDbType.Boolean).Value = cbid.checked; sqlConnection.Open(); command.ExecuteNonQuery(); sqlConnection.Close();
All 5 Replies
BMXDad 23
Newbie Poster
raajsj 0
Newbie Poster
BMXDad 23
Newbie Poster
raajsj 0
Newbie Poster
raajsj 0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.