Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Gridview as follows
C#
Subject     coursedate     Candidates   Checkbox

English      12 May 14         10       Checkbox1
Tamil        13 May 14         20       Checkbox2


When i click the checkbox in Gridview i want to display the selected checkbox gridview row in textbox.

i have one textbox as follows;

In textbox i want output as follows

C#
English 12 May 14 10 Tamil 13 May 14 20 (Retrive from the gridview)


for that how can i do in asp.net using c#.
Posted
Updated 14-May-14 2:58am
v2

1 solution

adapt from this example:
aspx page
XML
<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>

            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server" Width="532px"></asp:TextBox>

code behind
using System;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default14 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
            return;
        DataTable dtTemp = new DataTable();
        dtTemp.Columns.Add("Student ID");
        dtTemp.Columns.Add("First Name");
        dtTemp.Columns.Add("Last Name");
        dtTemp.Rows.Add("1", "Peter", "Leow");
        dtTemp.Rows.Add("2", "James", "Song");
        dtTemp.Rows.Add("3", "Mick", "Sam");
        dtTemp.Rows.Add("4", "MAry", "Cool");
        GridView1.DataSource = dtTemp;
        GridView1.DataBind(); 
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        TextBox1.Text = "";
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkBox = (CheckBox)row.FindControl("CheckBox1");
            if (chkBox != null && chkBox.Checked)
            {
                TextBox1.Text += " " + row.Cells[1].Text.ToString() + " " + row.Cells[2].Text.ToString() + " " + row.Cells[3].Text.ToString();
            }
        }
        
    } 
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900