1.11M Members

how to get dynamic checkbox checked value in c#?

 
0
 
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
 
0
 

Its 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 = new HtmlTable();
                t = (HtmlTable) FindControl("dataTable");
                foreach (HtmlTableRow trc in t.Rows)
                {
                    foreach (HtmlTableCell tc in trc.Cells)
                    {
                        foreach (Control htc in tc.Controls)
                        {
                            var cb = htc as CheckBox;
                            if (cb != null)
                            {
                                Response.Write(cb.ID + " " + cb.Checked + "<br />");
                            }
                        }

                    }
                }

            }
            catch (Exception d)
            {
                Response.Write(d.Message);
            }
 
0
 
Thank u Mr.ggamble,
             now it's working fine and i've one doubt i am using class file for this application and database Sql Server 2008. i have to get input value from UI to store database.for example my dadabase table name is "table1" and fields are (Name,Insert,Edit,Delete,View).i retrieve values from this to generate checkbox dynamically. upto this working fine. then i have to get user input from that dynamic checkboxes. then insert into "table2"-fields are (Grouppolicy_name,title(this field from table1-Name),staff_insert,staff_edit,staff_delete,staff_view.......like that). and i'm using stored procedure and class files.properties in class file.,
             //properties
             private string grouppolicy_name;
             public string Grouppolicy_Name
             {
                get { return grouppolicy_name; }
                set { grouppolicy_name = value; }
             }
             ... like all table data_fields
             when i get user input at the time i've to assign that input values into properties.,then i'll pass "storedprocedure_Name + private variable" to database.. this is the process.
             so i have to check dynamic checkboxes one by one, if check box is checked "true" input value is "1" otherwise "0"
             i don't know how to get input from checkboxes(dynamic) and assign input values into properties.
             i attached displaying checkboxes
Attachments Capture.JPG 21.48KB
 
0
 

Are 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();
 
0
 
 Mr.ggamble,
            This is my current out put. now i want to get input values from these checkboxes.but i dont know.
Attachments Capture.JPG 14.58KB
 
0
 

Hai All,
i am developing web application. in my application three users (Admin,Staff,Student), Admin can give permission to these two users(Staff and student).
Staff module : 1.User Information - options(Edit and Update)
2.Subject Details - options(Edit and Update)
3.Marks Details - options(Add,Edit,Update)
Student Module: 1.User Information - options(Edit and Update)
2.Subject Details - option(view)
3.Student Marks - options(view)
So Here Admin can give permission dynamically for these two users, this is my logic.

In my database there is two tables "Table1" and "Table2"
Table1 Fields: ModuleTitle varchar(50),
f_Insert int,
f_Edit int,
f_Delete int,
f_view int
in this table field values are used to generate dynamic checkbox.
Next,
Table2 Fields: Userpolicyname
[remaining fields are according to the "Table1"-ModuleTitle field.
for example: Table1 - ModuleTitle field value is "UserInfo"]

so remaining fields are : UserInfo_ins
UserInfo_Edit
UserInfo_Delete
UserInfo_view
...
like that.
Then my application I'm using "Stored procedure" and "Class File"

This is my classfile:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class AddValuesClass
{
    #region global var
    DataBaseClass objDataBaseClass;
    string returnString;
    Boolean stateOfInsertion;
    DataTable Table;
    DataSet ds;
    #endregion
    #region Properties 
    private string moduletitle;
    public string ModuleTitle
    {
        get { return moduletitle; }
        set { moduletitle = value; }
    }
    private int ins;
    public int Ins
    {
        get { return ins; }
        set { ins = value; }
    }
    private int edit;
    public int Edit
    {
        get { return edit; }
        set { edit = value; }
    }
    private int delete;
    public int Delete
    {
        get { return delete; }
        set { delete = value; }
    }
    private int view;
    public int View
    {
        get { return view; }
        set { view = value; }
    }
    #endregion
    #region Table2 properties
    private string policyname;
    public string PolicyName
    {
       get { return policyname; }
       set { policyname = value; }
    }
    private int userinfo_ins;
    public int Userinfo_ins
    {
      get { return userinfo_ins; }
      set { userinfo_ins = value;  }
    }
    private int userinfo_edit;
    public int Userinfo_Edit
    {
      get { return userinfo_edit; }
      set { userinfo_edit = value;  }
    }
    private int userinfo_delete;
    public int Userinfo_delete
    {
      get { return userinfo_delete; }
      set { userinfo_delete = value;  }
    }
    private int userinfo_view;
    public int Userinfo_View
    {
      get { return userinfo_View; }
      set { userinfo_view = value;  }
    }
    #endregion
    #region insertion part
    public Boolean Insertvalues()
    {
        Boolean stateOfInsertion;
        objDataBaseClass = new DataBaseClass();
        stateOfInsertion = objDataBaseClass.GetExecuteNonquey("spInservalue'" + name + "','" + ins + "','" + edit + "','" + delete + "','" + view + "'");
        return stateOfInsertion;
    }
    #endregion
    #region 
    public DataTable getmenuvalues()
    {
        objDataBaseClass = new DataBaseClass();
        Table = new DataTable();
        Table = objDataBaseClass.GetDataTable("sp_getvalues");
        return Table;
    }
    #endregion 
    public AddValuesClass()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}

i want to get user inputs from dynamic checkbox to assign table2 properties..

now my codings done upto generate dynamic checkboxes...

i don't know how to get values from dynamic checkbox.

this is my code behind page 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();
            table.ID = "dataTable";
            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 == "Title")
                        {
                            Label lbl = new Label();
                            lbl.ID = "lbl" + j + k;
                            //string lblid = 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;
                            //cbid.Text = dt.Rows[j][dt.Columns[k].ToString()].ToString();
                            //cbid.CheckedChanged += new EventHandler(cbid_CheckedChanged);
                            tc.Controls.Add(cbid);
                            tr.Controls.Add(tc);
                        }
                    }
                    table.Controls.Add(tr);                   
                }
                //end creating checkbox for displaying records information
                //button creation
                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 Checkbox Event
    //void cbid_CheckedChanged(object sender, EventArgs e)
    //{
    //    //throw new NotImplementedException();
    //}
    //#endregion
    #region Event handler
    protected void btn_Click(object sender, EventArgs e)
    {
        try
        {
            HtmlTable t = new HtmlTable();
            t = (HtmlTable)FindControl("dataTable");
            foreach (HtmlTableRow trc in t.Rows)
            {
                foreach (HtmlTableCell tc in trc.Cells)
                {
                    foreach (Control htc in tc.Controls)
                    {
                        var cb = htc as CheckBox;
                        if (cb != null)
                        {
                        }
                    }
                }
            }
        }
        catch (Exception d)
        {
            Response.Write(d.Message);
        }
    }
    #endregion
}

now i'm totaly disappointed due to this.,
if anyone know pls help me ...

Thank you

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article