TextBox Change Event Inside GridView

Ranjeet_8
Posted by Ranjeet_8 under ASP.NET category on | Points: 40 | Views : 36531
Aspx Design Page :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Testing Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="S No.">
<ItemTemplate>
<asp:Label ID="lblSn" runat="server" Text='<%#Eval("Sno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblRs" runat="server" Text='<%#Eval("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Text="0" OnTextChanged="txtQty_TextChanged"
AutoPostBack="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Amt.">
<ItemTemplate>
<asp:Label ID="lblTotalRs" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>

C# Code :
 
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Create DataTable
DataTable dt = new DataTable();
dt.Columns.Add("Sno");
dt.Columns.Add("Price");
DataRow dr;
dr = dt.NewRow(); // Add New Row In DataTable
dr["Sno"] = "1";
dr["Price"] = "104";
dt.Rows.Add(dr);
dr = dt.NewRow(); // Add New Row In DataTable
dr["Sno"] = "2";
dr["Price"] = "600";
dt.Rows.Add(dr);
// Bind Your GridView
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
// TextBox Change Event.
protected void txtQty_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
GridViewRow gvRow = (GridViewRow)txt.Parent.Parent;
Label lblRs = (Label)gvRow.FindControl("lblRs");
Label lblTotalRs = (Label)gvRow.FindControl("lblTotalRs");
try
{
lblTotalRs.Text = ((Convert.ToInt32(txt.Text)) * (Convert.ToInt32(lblRs.Text))).ToString();
}
catch { lblTotalRs.Text = "0"; txt.Text = "0"; }
}

Comments or Responses

Login to post response