ASP.NET Button only executes the method when clicked several times

Asked

Viewed 61 times

1

Hi, I’m facing a very annoying problem because I have a page function to cancel the weigh-in. Weighing because this page is an application that works internally by communicating with a scale over the network, collecting what is being weighed or not.

When I click the button that should close the grid on which it is located, keeping only a primary grid for product selection to be weighed, it does not trigger unless I click often.

This is the part of the code where is located a timer that runs in 50 milliseconds taking the value with precision:

<div id="divBalance" runat="server" class="floating-box-inside" style="width: auto;">
                    <asp:ScriptManager ID="updPanScrMan" runat="server"></asp:ScriptManager>
                    <asp:Timer ID="OIVTimer" runat="server" Interval="50" OnTick="OIVTimer_Tick" Enabled="false"></asp:Timer>

                    <asp:UpdateProgress runat="server" ID="updProgress">
                        <ProgressTemplate>
                            <div class="loader" style="width: 10px; height: 10px;"></div>
                        </ProgressTemplate>
                    </asp:UpdateProgress>

                    <asp:UpdatePanel ID="upPnlBalance" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">

                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="OIVTimer" EventName="Tick" />
                            <asp:PostBackTrigger ControlID="btnConfirmarPesagemOIV" />
                            <asp:PostBackTrigger ControlID="btnCancelarPesagemOIV" />
                        </Triggers>

                        <ContentTemplate>
                            <asp:Table runat="server" ID="tblupBalance" Style="text-align: center;">

                                <asp:TableRow>
                                    <asp:TableCell>
                                        <asp:DropDownList ID="ddlupBalance" runat="server" OnSelectedIndexChanged="ddlupBalance_SelectedIndexChanged" AutoPostBack="true" CssClass="txt_cssclass" Width="200px" Height="30px" Style="margin: 10px 0px 5px 0px;">
                                            <asp:ListItem> </asp:ListItem>
                                        </asp:DropDownList>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow>
                                    <asp:TableCell>
                                        <asp:Label ID="lblScaleAmount" runat="server" Text="<%$ Resources:LAnguageStrings, lblScaleAmount %>" CssClass="lbl_cssclass" Style="text-align: center; font-size: larger; margin: 5px 0px 5px 0px;" Width="200px" Height="30px"></asp:Label>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow>
                                    <asp:TableCell>
                                        <asp:TextBox ID="txtlblScaleAmount" runat="server" Enabled="False" CssClass="txt_cssclass" Style="text-align: center; font-size: x-large; margin: 5px 0px 5px 0px;" Width="200px" Height="40px"></asp:TextBox>
                                    </asp:TableCell>
                                </asp:TableRow>
                            </asp:Table>
                            <asp:Panel runat="server">

                                <asp:Table runat="server">

                                    <asp:TableRow>
                                        <asp:TableCell>
                                            <asp:Button runat="server" ID="btnConfirmarPesagemOIV" Text="<%$ Resources:LanguageStrings, btnConfirm %>" CssClass="btn btn-primary" Style="width: 200px; height: 30px; margin: 5px 0px 5px 0px;" OnClick="btnConfirmarPesagemOIV_Click" />
                                        </asp:TableCell>
                                    </asp:TableRow>
                                    <asp:TableRow>
                                        <asp:TableCell>
                                            <asp:Button runat="server" ID="btnCancelarPesagemOIV" Text="<%$ Resources:LanguageStrings, btnCancel %>" CssClass="btn btn-default" Style="width: 200px; height: 30px; margin: 5px 0px 10px 0px;" OnClick="btnCancelarPesagemOIV_Click" />
                                        </asp:TableCell>
                                    </asp:TableRow>
                                </asp:Table>
                            </asp:Panel>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </div>

This is the code executed by the button that should stop the timer, close the connection and close the screen grid:

        protected void btnCancelarPesagemOIV_Click(object sender, EventArgs e)
    {
        divAmountsOneIngre.Attributes.Add("style", "display: none");
        divBalance.Attributes.Add("style", "display: none");

        OIVTimer.Enabled = false;
        Session["ScaleConn"] = null;

        txtlblScaleAmount.Enabled = false;
        txtlblScaleAmount.Text = "";
        txtlblScaleAmount.CssClass = "txt_cssclass";
        SSCC_barcodeReader.Enabled = false;
        SSCC_barcodeReader.Text = "";

        txtlblMtrCod.Text = "";
        txtlblMtrNam.Text = "";
        txtlblLotID.Text = "";
        txtlblLotIDQtd.Text = "";
        txtlblExpDat.Text = "";

        txtlblMax.Text = "";
        txtlblTar.Text = "";
        txtlblMin.Text = "";
        txtlblAct.Text = "";
        txtlblRequired.Text = "";
    }

I’ve looked for thousands of solutions and haven’t found any solution for this or any other way I can accomplish this.

Remembering: When clicking the button that should close the grid in just one click, even with some delay, it does not. Unless I click several times on the button.

This is the code I use inside Timer_tick():

        protected void OIVTimer_Tick(object sender, EventArgs e)
    {
        var AmRepo = Session["Material"] as Conaprole_DosimetrySystem.Class.Material;
        var ScaleObj = Session["ScaleConn"] as Conaprole_DosimetrySystem.Class.Scale;

        if (ScaleObj == null)
        {
            OIVTimer.Enabled = false;
            Session.Remove("ScaleConn");

            divAmountsOneIngre.Attributes.Add("style", "display: none");
            divBalance.Attributes.Add("style", "display: none");

            txtlblScaleAmount.Enabled = false;
            txtlblScaleAmount.Text = "";
            txtlblScaleAmount.CssClass = "txt_cssclass";
            SSCC_barcodeReader.Enabled = true;
            SSCC_barcodeReader.Text = "";

            btnAddToTotal.Enabled = false;
            btnAddToTotal.Visible = false;
            btnScaleOn.Enabled = false;
            btnScaleOn.Visible = false;
            btnFinalize.Enabled = false;
            btnFinalize.Visible = false;
            btnManualConfirm.Enabled = false;
            btnManualConfirm.Visible = false;

            btnCancel.Enabled = false;
            btnCancel.Visible = false;
            btnConfirm.Enabled = false;
            btnConfirm.Visible = false;

            txtlblMtrCod.Text = "";
            txtlblMtrNam.Text = "";
            txtlblLotID.Text = "";
            txtlblLotIDQtd.Text = "";
            txtlblExpDat.Text = "";

            txtlblMax.Text = "";
            txtlblTar.Text = "";
            txtlblMin.Text = "";
            txtlblAct.Text = "";
            txtlblRequired.Text = "";

            btnHide();
        }
        else
        {

            float Max = AmRepo.MaxAmount - AmRepo.ActualAmount;
            float Min = AmRepo.MinAmount - AmRepo.ActualAmount;

            int returnCode = ScaleObj.commandLv0_SI();

            if (returnCode == 0)
            {
                double peso = ScaleObj.weight;

                txtlblScaleAmount.Text = String.Format("{0:F3}", peso);

                if (peso <= 0f)
                {
                    btnConfirmarPesagemOIV.Enabled = false;
                    txtlblScaleAmount.CssClass = "txt_cssclass_scale_red";
                }
                else if (Min <= AmRepo.ContainerTotalAmount)
                {
                    if (peso >= Min || peso <= Max)
                    {
                        if (peso < Min && peso >= 0f)
                        {
                            btnConfirmarPesagemOIV.Enabled = true;
                            txtlblScaleAmount.CssClass = "txt_cssclass_scale_red";
                        }
                        else if (peso > Max && peso >= 0f)
                        {
                            btnConfirmarPesagemOIV.Enabled = false;
                            txtlblScaleAmount.CssClass = "txt_cssclass_scale_red";
                        }
                        else
                        {
                            btnConfirmarPesagemOIV.Enabled = true;
                            txtlblScaleAmount.CssClass = "txt_cssclass_scale_green";
                        }
                    }
                    else
                    {
                        btnConfirmarPesagemOIV.Enabled = false;
                        txtlblScaleAmount.CssClass = "txt_cssclass_scale_red";
                    }
                }
                else
                {
                    btnConfirmarPesagemOIV.Enabled = true;
                    txtlblScaleAmount.CssClass = "txt_cssclass_scale_red";
                }
            }
            else
            {
                switch (returnCode)
                {
                    case -1:
                        txtlblScaleAmount.Text = Resources.LanguageStrings.lblScaleOverload;
                        break;
                    case -2:
                        txtlblScaleAmount.Text = Resources.LanguageStrings.lblScaleUnderload;
                        break;
                    case -3:
                        txtlblScaleAmount.Text = Resources.LanguageStrings.lblScaleBusy;
                        break;
                    case -4:
                        txtlblScaleAmount.Text = Resources.LanguageStrings.lblCantWrite;
                        break;
                    case -5:
                        txtlblScaleAmount.Text = Resources.LanguageStrings.lblCantRead;
                        break;
                }
            }
        }
    }

I know it seems to be quite repetitive some things within it, but some were needed others were out of desperation. And also that code was made by me and another colleague while I was on vacation. It was working, but in the implementation, I found some errors. One is the one I tell you and I need to solve.

  • Why do you need a timer? Perhaps the operation executed by him takes longer than 50ms and this can be queuing several times the commands (assumption, I did not look carefully at the code). Have you tried increasing this range? Like for something between 1~5s, it is necessary to rely on this latency so low in a web application?

  • @Leandroangelo I do need this update rate so fast, because it is a super accurate scale.

  • And you’re sure that a request-based web application is the best way to deliver all this potential for broadcast?

  • @Leandroangelo For the scenario I developed all this application is necessary. It was previously thought of as a windows application, but the requirements were for a web application. What gets me is that I use this timer system to communicate with the balance on two different screens, but it performs the same code. And the next I don’t face that problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.