Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have added the checkbox column through item template in grid view and want to see the value of selected row in the text box

C#
public partial class Default3 : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
        DataTable dt = new DataTable();
        //dt.Columns.Add(new DataColumn("select", typeof(bool)));
        dt.Columns.Add("Roll no");
        dt.Columns.Add("Name");
        dt.Columns.Add("marks");
        dt.Rows.Add("D001", "Aditya", "80");
        dt.Rows.Add("D002", "Arun", "75");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        gvnew.DataSource = dt;
        gvnew.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewRow row = gvnew.SelectedRow;
        row.Cells[4].Text = TextBox2.Text;  
        row.Cells[5].Text = TextBox3.Text;
    }
    protected void gvnew_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvnew.SelectedRow;
        TextBox1.Text= row.Cells[3].Text;
        TextBox2.Text= row.Cells[4].Text;
        TextBox3.Text = row.Cells[5].Text;
        TextBox1.ReadOnly = true;
           
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvnew.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkbselect");
            if (chk!=null && chk.Checked)
            {
                TextBox1.Text = gvnew.DataKeys[row.RowIndex].Value.ToString();
                TextBox2.Text = gvnew.Rows[0].Cells[0].Text.ToString();
                TextBox3.Text = row.Cells[5].Text;
                TextBox1.ReadOnly = true;
                //Response.Write("you have selected "+ gvnew.DataKeys[row.RowIndex].Value +"<br />");

            }
        
        }
    }
}
Posted
Updated 16-Aug-12 18:55pm
v5
Comments
Sergey Alexandrovich Kryukov 17-Aug-12 0:52am    
Tag it: WPF? Forms? Silverlight? what?
--SA
Aditya Asati 17-Aug-12 0:54am    
forms in asp.net webpage
Aditya Asati 17-Aug-12 0:59am    
can anyone help me out in the the code above is not having effect.
thanks in advance
Santhosh Kumar Jayaraman 17-Aug-12 0:59am    
so what happened? any error? or whats the proble,?
Santhosh Kumar Jayaraman 17-Aug-12 1:06am    
What is this checkbox1? Can you share ur aspx code?

1 solution

ASP.NET
<asp:gridview id="gvnew" runat="server" cellpadding="4" forecolor="#333333" gridlines="None" onselectedindexchanged="gvnew_SelectedIndexChanged" xmlns:asp="#unknown"> <asp:templatefield>


///use itemtemplate here

ASP.NET
<itemtemplate>


 <asp:checkbox id="CheckBox1" runat="server" /> <asp:checkbox id="chkbselect" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" /> 

<pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center" /> <asp:checkbox id="Chkbselect" runat="server" checked="True" />

and in c# code
C#
TextBox2.Text = gvnew.Rows[0].Cells[0].Text.ToString();// changed here
Textbox txt=(TextBox)row.findcontrol("textboxid");


and rather than textbox readonly you can use on client side

onkeypress="javascript: return false;" on textbox.

try it


Please Vote if it helps you
 
Share this answer
 
v2
Comments
Aditya Asati 17-Aug-12 2:36am    
its not working for me

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