简体   繁体   中英

how to get column data when check box is checked using data list in asp.net

this is eswar.k , i have one problem in asp.net..that is ..

i have one datalist .that is shows data from database ..that is contains .check box,image,and lables..here what is the problem .. when i am checked on check box ,i have to display the email labels into the text box..(like multiple recipients eg:eswar@gmil.com,eee@yahoo.in..etc )

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        string strconnstring = System.Configuration.ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
        string strquery = "select chid,chname,chlanguage,chrating,chemail,contenttype,data from tbl_channel_join Order by chid";
        SqlCommand cmd = new SqlCommand(strquery);
        SqlConnection con = new SqlConnection(strconnstring);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        try        
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
            //GridView2.DataSource = dt;
            //GridView2.DataBind();
            dl_channels.DataSource = dt;
            dl_channels.DataBind();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
            dt.Dispose();
        }

Let's say you have a Gridview with checkbox like this :

 <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="checkIT" runat="server"/>
                </ItemTemplate>
            </asp:TemplateField>
    </asp:GridView>

 <asp:Button ID="btnDisplay" runat="server" Text="Show data selected"  OnClick="btnDisplay_Click"/>    

<asp:TextBox id="textboxDataDisplay" runat="server" />

with a button to show the selected checkbox columns

C# code

protected void btnDisplay_Click(object sender, EventArgs e)
{
    string data = "";

    foreach (GridViewRow row in GridView1.Rows)
    {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
                if (chkRow.Checked)
                {
                    string yourFirstRowCell = row.Cells[1].Text;
                    string yourSecondRowCell = row.Cells[2].Text;
                    string yourThirdRowCell = row.Cells[3].Text;
                    data = yourFirstRowCell + yourSecondRowCell + yourThirdRowCell; 
                }
            }
    }

    textboxDataDisplay.text = data; 
}

Row cells are the cells in that row you want to get where the checkbox is checked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM