Get TextBox Value When check box is checked in a GridView using c#

Posted by Yshsrinivasan under ASP.NET on 11/7/2012 | Points: 10 | Views : 9017 | Status : [Member] | Replies : 4
I had girdview in that i had textbox and checkbox when checkbox is checked in gridview textbox value display in responsce.write i think it has to be done with event bubling.

Subramanyam


Responses

Posted by: Sourabh07 on: 11/8/2012 [Member] Starter | Points: 25

Up
0
Down
hi...

please try this....

Source Page:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Event_Bubling.aspx.cs" Inherits="Event_Bubling" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="S.No" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TextBox">
<ItemTemplate>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CheckBox">
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="chk_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


Code Behind Page:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Event_Bubling : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindGrid();
}

void BindGrid()
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
gv.DataSource = dt;
gv.DataBind();
}

protected void chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;

GridViewRow gr = (chk.NamingContainer) as GridViewRow;

TextBox txt = gr.FindControl("txt") as TextBox;

if (!chk.Checked)
return;

if (txt.Text.ToString().Trim() == string.Empty)
Response.Write("Please Enter the value in the TextBox.");

else
Response.Write(txt.Text.ToString().Trim());
}
}


Sourabh07

Yshsrinivasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 11/8/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Just go through this link it was so nice
http://stackoverflow.com/questions/9294184/how-to-get-the-value-in-the-gridview-which-the-checkbox-is-checked

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Yshsrinivasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Yshsrinivasan on: 11/8/2012 [Member] Starter | Points: 25

Up
0
Down
Excellent Thank you Sourabh

Subramanyam

Yshsrinivasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 11/8/2012 [Member] Starter | Points: 25

Up
0
Down
hi...........

Thanks Subramanyam:-)

Sourabh07

Yshsrinivasan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response