Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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;
 
                    //DayOfWeek - units cell
                    var txtDay = document.getElementById(ctrlParent + ZeroPad(count, 2) + ctrlExt);
                    while (txtDay != null) {
                        //validate text value - must be numeric
                        if (isNaN(txtDay.value) || txtDay.value == '')
                            txtDay.value = "";
 
                        var txtFloatValue = document.getElementById(ctrlParent + ZeroPad(count, 2) + '_txtFloatValue');
 
                        //float pack units
                        floatPackUnits = parseFloat(txtDay.value);
                        //float pack value
                        floatPackValue = parseFloat(txtFloatValue.value);
 
                        dayFloatPackValue = floatPackUnits * floatPackValue;
 
                        if (floatPackValue > 0) {
                            //total units for day
                            totalUnits = totalUnits + parseFloat(floatPackUnits);
                            //total value for day
                            totalValue = totalValue + parseFloat(dayFloatPackValue);
 
                            //total units for float pack
                            UpdateTotals(ctrlParent + ZeroPad(count, 2), 0,floatPackValue);
 

                        }
                        else {
                            txtDay.value = totalUnits;
 
                            //total units for float pack
                            UpdateTotals(ctrlParent + ZeroPad(count, 2), 0,totalUnits);
 
                            count++;
                            txtDay = document.getElementById(ctrlParent + ZeroPad(count, 2) + ctrlExt);
                            txtDay.value = totalValue.toFixed(2);
 
                            //total value for float pack
                            UpdateTotals(ctrlParent + ZeroPad(count, 2), 1);
 

                        }
 
                        count++;
                        //DayOfWeek - units cell
                        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;
                //int amountType = (int)grdFloat1.DataKeys[row.RowIndex].Value;
                //orderData.Add(amountType, new dailyOrder(day,amountType,int.Parse(((TextBox)row.Cells[12].FindControl("txt" + dayName)).Text)));

            }
        }
        
    }
}
Posted 24-May-11 3:06am
sucram4.2K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I have figured it out, I feel like a complete dumbass now. My regular expression validator did not have an error message so I never noticed that my regular expression was not what I wanted.
  Permalink  
Comments
digital man at 24-May-11 10:15am
   
Have a 5 for admitting that you're as fallible as the rest of us! :-)
thatraja at 24-May-11 12:08pm
   
5 for the self effort
Steve van Niman at 9-Aug-11 8:53am
   
Thank You, Thank You, Thank You! I had removed a couple of text boxes from my web form, only to find the Submit button would not fire. Turns out, I had not removed the expression validator. Removed, and all is firing again. Sorry you had to run into this first.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
In my case none of the events (either button or check box) were firing. I was able to overcome by this problem using following way.
 
In my master page I had closed a <script> tag referring to an external .js file with /> notation instead of <script></script>.
 
for Ex: chanage <script src='../../Scripts/common/menu.Js' type='text/javascript' /> to <script src='../../Scripts/common/menu.Js' type='text/javascript' ></script>
 
Hope this will help you,
Ramesh
  Permalink  
Comments
Mohd Zubair Khan at 2-Jul-13 3:09am
   
Sucram thanks you made my bday... great answer..........
manimaran4187 at 9-Jul-13 6:53am
   
i have a <input tag with id and input name and ait = go and src =".jpg" like this it is not having onclick event .am not able to fire the button. is there any way to fire input type = image.
PrasadShastri at 14-Jan-14 4:54am
   
Ramesh, Thanks for sharing. It worked for me.
It is a little thing which most of us never think as solution on such issue,
but it is a correct in my case solution !!!! :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Start here[^].
  Permalink  
Comments
sucram at 24-May-11 9:18am
   
Thanks, figured it out eventually I forgot to add an errormessage to the validator and my regular expression was not exactly what I wanted it to be.
 
The user input failed validation without notifying me, hence the postback did not fire. I also only noticed that the regular expression was not what I wanted it to be after I added the error message to the validator.
Teju MB at 4-Oct-12 5:28am
   
my 5! was in same situtaion..

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 1 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100