将几行添加到数据表后,添加按钮停止工作

时间:2019-04-04 10:57:02

标签: c# asp.net

我将数据表存储在没有数据库的gridview中。它正在工作。但是在添加第20行时,ADD按钮停止工作。我没有弄清楚问题所在。这将非常有帮助。如果有人能弄清楚。

点击20次后,我无法确定按钮无法触发onclick事件。

这是.aspx文件:

                                            <div class="row">                                           <div class="col-md-4">
                                                    <label for="ddlAlloAcc">Allocate Account</label>                                                    <asp:DropDownList ID="ddlAlloAcc" runat="server" BorderColor="#666666" CssClass="form-control " AutoPostBack="true" AccessKey="A" TabIndex="11"                                                         OnSelectedIndexChanged="ddlAlloAccOnSelectedIndexChanged" >                                                 </asp:DropDownList>                                             </div>
                                                <div class="col-md-4">                                                  <label for="txtDrAcCode">Account Code</label>                                                   <asp:TextBox ID="txtDrAcCode" runat="server" BorderColor="#666666" CssClass="form-control "></asp:TextBox>                                              </div>
<asp:Panel ID="Panel2" runat="server" CssClass="col-md-4 panlcss" DefaultButton="btnDrSave">
                                                <div class="col-md-9">
                                                    <label for="txtDrAmount">Amount</label>
                                                    <asp:TextBox ID="txtDrAmount" runat="server" BorderColor="#666666" CssClass="form-control " CausesValidation="True"  TabIndex="12"></asp:TextBox>
                                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtDrAmount" ErrorMessage="You Have To Use Neumeric Value" ValidationExpression="([0-9])[0-9]*[.]?[0-9]*" ForeColor="Red"></asp:RegularExpressionValidator>

                                                </div>
                                                <div class="col-md-3">
                                                    <div class="col-md-12">
                                                        <br />
                                                    </div>
                                                    <div class="col-md-12" style="margin: 0px; padding: 0px; padding-top: 10px;" >
                                                        <asp:Button ID="btnDrSave" CssClass="btn btn-primary" Text="Add" TabIndex="13"  runat="server" OnClick="btnDrSaveOnClick" /></div>
                                                </div>
</asp:Panel>

                                            </div>
                                        </div>
                                        <asp:GridView ID="GvShowAllocateaccount" runat="server" Width="100%" CssClass="table table-striped table-bordered table-condensed" AutoGenerateColumns="false"
                                            OnRowDeleting="GvShowAllocateaccount_RowDeleting" OnRowDataBound="GvShowAllocateaccount_RowDataBound" RowStyle-CssClass="zero">
                                            <HeaderStyle CssClass="headerStyle"></HeaderStyle>
                                            <RowStyle CssClass="rowStyle"></RowStyle>
                                            <Columns>
                                                <asp:BoundField DataField="DrAccCode" ItemStyle-HorizontalAlign="Center" HeaderText="Account Code" />
                                                <asp:BoundField DataField="DrAccAccount" ItemStyle-HorizontalAlign="Center" HeaderText="Allocate Account" />


                                                <asp:BoundField DataField="DrAmount" ItemStyle-HorizontalAlign="Center" HeaderText="Amount"></asp:BoundField>

                                                <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Delete">
                                                    <ItemTemplate>
                                                        <asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="X" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>



                                            </Columns>
                                        </asp:GridView>

这是aspx.cs文件:


        protected void btnDrSaveOnClick(object sender, EventArgs e)
        {
            try
            {
                if (ddlAlloAcc.SelectedIndex != 0 && txtDrAmount.Text != "")
                {
                    Div1.Visible = true;

                    if (ViewState["CurrentData"] != null)
                    {
                        DataTable dt = (DataTable)ViewState["CurrentData"];
                        int count = dt.Rows.Count;
                        BindGrid(count);
                        ddlAlloAcc.Focus();
                    }
                    else
                    {
                        BindGrid(1);
                        ddlAlloAcc.Focus();
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Save", "alert('All Field Are Mandatory!');", true);

                }
            }
            catch (Exception ex)
            {
                VDT.ShowMessage(lblMsg, ex.Message, "ERR");
            }
        }
 private void BindGrid(int rowcount)
        {
            DataTable dt = new DataTable();
            DataRow dr;
            DataColumn DrAccCode, DrAccAccount, DrAmount; 
            DrAccCode = new DataColumn("DrAccCode", Type.GetType("System.String"));
            DrAccAccount = new DataColumn("DrAccAccount", Type.GetType("System.String"));
            DrAmount = new DataColumn("DrAmount", Type.GetType("System.String"));
            dt.Columns.Add(DrAccCode);
            dt.Columns.Add(DrAccAccount);
            dt.Columns.Add(DrAmount);
            int temp = 0;
            txtDrAmount.Text = Math.Round(Convert.ToDouble(txtDrAmount.Text), 4).ToString(".0000");
            if (btnDrSave.Text == "Add")
            {

                if (ViewState["CurrentData"] != null)
                {
                    double x = Convert.ToDouble(txtDrAmount.Text);
                        dt = (DataTable)ViewState["CurrentData"];

                        for (int j = 0; j < rowcount; j++)
                        {
                            if (dt.Rows[j]["DrAccCode"].ToString() == txtDrAcCode.Text)
                            {
                                ScriptManager.RegisterStartupScript(this, GetType(), "Save",
                                    "alert('This Charge Head has already been added...');", true);
                                temp++;
                                break;

                            }
                        }
                        if (dt.Rows.Count > 0)
                        {
                            dr = dt.NewRow();
                            dr[0] = dt.Rows[0][0].ToString();
                        }

                    if (temp == 0) {

                    dr = dt.NewRow();
                    dr[0] = txtDrAcCode.Text;
                    dr[1] = ddlAlloAcc.SelectedItem.Text;
                    dr[2] = txtDrAmount.Text;

                    dt.Rows.Add(dr);




                    lblTotal.InnerText = (Convert.ToDouble(lblTotal.InnerText) + x).ToString();
                    }
                }
                else
                {
                    dr = dt.NewRow();
                    dr[0] = txtDrAcCode.Text;
                    dr[1] = ddlAlloAcc.SelectedItem.Text;
                    dr[2] = txtDrAmount.Text;
                    dt.Rows.Add(dr);
                    lblTotal.InnerText = "0";
                    lblTotal.InnerText = txtDrAmount.Text;
                    double x = Math.Truncate(Convert.ToDouble(txtDrAmount.Text) * 10000) / 10000;
                    lblTotal.InnerText = x.ToString();
                    lblTotal.InnerText = (Math.Truncate(Convert.ToDouble(lblTotal.InnerText) * 10000) / 10000).ToString();

                }


            }
            if (ViewState["CurrentData"] != null)
            {
                GvShowAllocateaccount.DataSource =(DataTable)ViewState["CurrentData"];
                GvShowAllocateaccount.DataBind();
                if (btnSave.Text == "Save")
                ClearData();
            }
            else
            {
                GvShowAllocateaccount.DataSource = dt;
                GvShowAllocateaccount.DataBind();
                if(btnSave.Text=="Save")
                ClearData();

            }
            ViewState["CurrentData"] = dt;

        }

1 个答案:

答案 0 :(得分:0)

我建议为Breaking When Exceptions抛出启用Common Language Runtime Exceptions

Visual Studio (2017) => Debug => Windows => Exception Settings

应该在屏幕底部打开一个窗口,第二个选项是Common Runtime Exceptions,检查一下,然后至少在发生服务器端异常的地方有代码中断。