Hi there fellow CPians,
I am a bit puzzled because a button Click event is not firing and I do not know why.
I have a Gridview wth a bunch of textboxes inside template fields. When I enter a value into any of the textboxes then my submit button click event does not fire, when I remove the values from the textboxes then the submit button click event does fire.
The textboxes onChange event fires a javascript that updates to textboxes in the same row in the grid view.
I need the click event of the submit button to fire after a user has edited the textboxes.
Please advise on how to resolve this issue:
I have created a page with just the neccessary controls to reproduce the issue. Below see my code:
<pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FloatConfigurationTestApplication._Default" %>
<!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>Untitled Page</title>
<style type="text/css">
.row_hide
{
display: none;
}
.style2
{
width: 334px;
height: 25px;
}
.style3
{
height: 25px;
}
.style4
{
width: 84px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript" language="javascript">
function ZeroPad(num, count) {
var numZeropad = num + '';
while (numZeropad.length < count) {
numZeropad = "0" + numZeropad;
}
return numZeropad;
}
function UpdateTotals(parentCtrl, isInt, floatPackValue) {
var txtMonday = document.getElementById(parentCtrl + '_txtMonday')
var txtTuesday = document.getElementById(parentCtrl + '_txtTuesday')
var txtWednesday = document.getElementById(parentCtrl + '_txtWednesday')
var txtThursday = document.getElementById(parentCtrl + '_txtThursday')
var txtFriday = document.getElementById(parentCtrl + '_txtFriday')
var txtSaturday = document.getElementById(parentCtrl + '_txtSaturday')
var txtSunday = document.getElementById(parentCtrl + '_txtSunday')
var txtTotalUnits = document.getElementById(parentCtrl + '_txtTotalUnits');
var txtTotalFloatValue = document.getElementById(parentCtrl + '_txtTotalValue');
var totalUnits = parseFloat(txtMonday.value== "" ? "0" :txtMonday.value)
+ parseFloat(txtTuesday.value == "" ? "0" : txtTuesday.value)
+ parseFloat(txtWednesday.value== "" ? "0" :txtWednesday.value)
+ parseFloat(txtThursday.value== "" ? "0" :txtThursday.value)
+ parseFloat(txtFriday.value== "" ? "0" :txtFriday.value)
+ parseFloat(txtSaturday.value== "" ? "0" :txtSaturday.value)
+ parseFloat(txtSunday.value == "" ? "0" : txtSunday.value);
if (isInt == 0) {
txtTotalUnits.value = totalUnits;
txtTotalFloatValue.value = totalUnits * floatPackValue
}
else {
txtTotalUnits.value = totalUnits.toFixed(2);
txtTotalFloatValue.value = (totalUnits * floatPackValue).toFixed(2);
}
}
function QtyUpdated(dayOfWeek) {
var ctrlParent = 'grdFloat1_ctl';
var ctrlExt = '_txt' + dayOfWeek;
var count = 2;
var totalUnits = 0;
var totalValue = 0;
var dayFloatPackValue = 0;
var floatPackUnits = 0;
var floatPackValue = 0;
var txtDay = document.getElementById(ctrlParent + ZeroPad(count, 2) + ctrlExt);
while (txtDay != null) {
if (isNaN(txtDay.value) || txtDay.value == '')
txtDay.value = "";
var txtFloatValue = document.getElementById(ctrlParent + ZeroPad(count, 2) + '_txtFloatValue');
floatPackUnits = parseFloat(txtDay.value);
floatPackValue = parseFloat(txtFloatValue.value);
dayFloatPackValue = floatPackUnits * floatPackValue;
if (floatPackValue > 0) {
totalUnits = totalUnits + parseFloat(floatPackUnits);
totalValue = totalValue + parseFloat(dayFloatPackValue);
UpdateTotals(ctrlParent + ZeroPad(count, 2), 0,floatPackValue);
}
else {
txtDay.value = totalUnits;
UpdateTotals(ctrlParent + ZeroPad(count, 2), 0,totalUnits);
count++;
txtDay = document.getElementById(ctrlParent + ZeroPad(count, 2) + ctrlExt);
txtDay.value = totalValue.toFixed(2);
UpdateTotals(ctrlParent + ZeroPad(count, 2), 1);
}
count++;
txtDay = document.getElementById(ctrlParent + ZeroPad(count, 2) + ctrlExt);
}
}
</script>
<div>
</div>
<div>
<table>
<tr>
<td>
<asp:Label ID="lblFloatType" runat="server" Text="Select Float Type:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlFloatType" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</div>
</br>
<div>
<asp:GridView ID="grdFloat1" runat="server" AlternatingRowStyle-BackColor="LightGray" OnRowDataBound="grdFloat1_OnRowDataBound" >
<Columns>
<asp:BoundField DataField="AmountTypeId" Visible="false" />
<asp:BoundField DataField="AmountType" HeaderText="Float Pack" Visible="true" />
<asp:BoundField DataField="value" HeaderText="Value" Visible="true" />
<asp:BoundField DataField="Units" HeaderText="Units" Visible="true" />
<asp:BoundField DataField="Monday" HeaderText="Monday" Visible="false" />
<asp:BoundField DataField="Tuesday" HeaderText="Tuesday" Visible="false" />
<asp:BoundField DataField="Wednesday" HeaderText="Wednesday" Visible="false" />
<asp:BoundField DataField="Thursday" HeaderText="Thursday" Visible="false" />
<asp:BoundField DataField="Friday" HeaderText="Friday" Visible="false" />
<asp:BoundField DataField="Saturday" HeaderText="Saturday" Visible="false" />
<asp:BoundField DataField="Sunday" HeaderText="Sunday" Visible="false" />
<asp:BoundField DataField="TotalUnits" HeaderText="Total Units" Visible="false" />
<asp:BoundField DataField="TotalValue" HeaderText="TotalValue" Visible="false" />
<asp:TemplateField HeaderText="Monday" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox id="txtMonday" runat="server" Width="100px" BorderStyle="Groove" onchange="javascript: QtyUpdated('Monday');" ></asp:TextBox>
<asp:RegularExpressionValidator id="Regularexpressionvalidator3" runat="server" ValidationExpression="\d{4}"
Display="Dynamic" ControlToValidate="txtMonday"></asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tuesday" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox id="txtTuesday" runat="server" Width="100px" BorderStyle="Groove" onchange="javascript: QtyUpdated('Tuesday');" ></asp:TextBox>
<asp:RegularExpressionValidator id="Regularexpressionvalidator4" runat="server" ValidationExpression="\d{4}"
Display="Dynamic" ControlToValidate="txtTuesday"></asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wednesday" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox id="txtWednesday" runat="server" Width="100px" BorderStyle="Groove" onchange="javascript: QtyUpdated('Wednesday');" ></asp:TextBox>
<asp:RegularExpressionValidator id="Regularexpressionvalidator5" runat="server" ValidationExpression="\d{4}"
Display="Dynamic" ControlToValidate="txtWednesday"></asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Thursday" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox id="txtThursday" runat="server" Width="100px" BorderStyle="Groove" onchange="javascript: QtyUpdated('Thursday');" ></asp:TextBox>
<asp:RegularExpressionValidator id="Regularexpressionvalidator6" runat="server" ValidationExpression="\d{4}"
Display="Dynamic" ControlToValidate="txtThursday"></asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Friday" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox id="txtFriday" runat="server" Width="100px" BorderStyle="Groove" onchange="javascript: QtyUpdated('Friday');" ></asp:TextBox>
<asp:RegularExpressionValidator id="Regularexpressionvalidator7" runat="server" ValidationExpression="\d{4}"
Display="Dynamic" ControlToValidate="txtFriday"></asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saturday" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox id="txtSaturday" runat="server" Width="100px" BorderStyle="Groove" onchange="javascript: QtyUpdated('Saturday');" ></asp:TextBox>
<asp:RegularExpressionValidator id="Regularexpressionvalidator8" runat="server" ValidationExpression="\d{4}"
Display="Dynamic" ControlToValidate="txtSaturday"></asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sunday" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox id="txtSunday" runat="server" Width="100px" BorderStyle="Groove" onchange="javascript: QtyUpdated('Sunday');" ></asp:TextBox>
<asp:RegularExpressionValidator id="Regularexpressionvalidator9" runat="server" ValidationExpression="\d{4}"
Display="Dynamic" ControlToValidate="txtSunday"></asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Units" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox ID="txtTotalUnits" runat="server" Width="100px" BorderStyle="Groove" ReadOnly="true" TabIndex="-1" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Value" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox ID="txtTotalValue" runat="server" Width="100px" BorderStyle="Groove" ReadOnly="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtFloatValue" runat="server" Width="100px" BorderStyle="None"></asp:TextBox>
</ItemTemplate>
<ItemStyle CssClass="row_hide" />
<HeaderStyle CssClass="row_hide" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace FloatConfigurationTestApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoadGrid();
}
private void LoadGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("AmountTypeId");
dt.Columns.Add("AmountType");
dt.Columns.Add("Value");
dt.Columns.Add("Units");
dt.Columns.Add("Monday");
dt.Columns.Add("Tuesday");
dt.Columns.Add("Wednesday");
dt.Columns.Add("Thursday");
dt.Columns.Add("Friday");
dt.Columns.Add("Saturday");
dt.Columns.Add("Sunday");
dt.Columns.Add("TotalUnits");
dt.Columns.Add("TotalValue");
dt.Rows.Add(new object[] { "1", "R101", "101", "", "", "", "", "", "", "", "", "", "" });
dt.Rows.Add(new object[] { "2", "R202", "202", "", "", "", "", "", "", "", "", "", "" });
dt.Rows.Add(new object[] { "3", "R303", "203", "", "", "", "", "", "", "", "", "", "" });
dt.Rows.Add(new object[] { "4", "R404", "204", "", "", "", "", "", "", "", "", "", "" });
grdFloat1.AutoGenerateColumns = false;
grdFloat1.DataSource = dt;
grdFloat1.DataBind();
}
protected void grdFloat1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtFloatValue = (TextBox)e.Row.Cells[12].FindControl("txtFloatValue");
txtFloatValue.Text = e.Row.Cells[2].Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdFloat1.Rows)
{
string a = ((TextBox)row.Cells[12].FindControl("txtMonday")).Text;
}
}
}
}