locked
Why are textbox changes executing code elsewhere? RRS feed

  • Question

  • User248267340 posted

    I created a form that reads a user-supplied product ID, and when I click the submit button, the code inside the button will go get SQL Server data and display to screen.

    However, if I wanted to change the contents of a textbox, once I press <enter> the code inside the submit button runs and reloads the same SQL Server data. That means anything I changed will just revert back.

    So how can I get the software to not run the submit button code, when I change a textbox value?

    Thursday, October 15, 2020 10:00 PM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2020 10:20 PM
  • User409696431 posted

    I just noticed something.  You did not follow my disable button instructions exactly.

    You have:

    <asp:Button ID="btnDisable" runat="server" Visible="false" OnClientClick="return false" />

    Visible false is not the same thing as display:none.  Use

    <asp:Button ID="btnDisableEnter" runat="server" Text="" OnClientClick="return false;"
                    Style="display: none;" />

    Visible false means the button is not even in the page.  Display none means it's in the page, but not displayed to the user.

    Also, just because the default button is associated with the whole form, and you don't want it to not be there if anything in your form is not in the page at the time (templates that are not active, visible false applied to a container its in, and so on), I generally park that button right before the closing </form>.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2020 9:51 PM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2020 10:20 PM
  • User248267340 posted

    Fair enough! Thanks for the link on that. So, when a user wants to modify textbox data to correct something, what is a good solution?

    (I tried the script example from the article and nothing changed).

    Thursday, October 15, 2020 11:32 PM
  • User409696431 posted

    To prevent "enter" from submitting your form, define the default button for the form, and make that a non-visible and non-functional button.  Then wire up any buttons that are on the page to do what you want.

    <form id="form1" runat="server" defaultbutton="btnDisableEnter">
    
    and, still inside the form,
    
    <asp:Button ID="btnDisableEnter" runat="server" Text="" OnClientClick="return false;"
                    Style="display: none;" />
    
    Other buttons in the form should explicitly call the function they are supposed to perform, e.g.:
    
    <asp:Button ID="Button1" runat="server" OnClick="UpdateButton1_Click" Text="Save Your Changes" />

    Obviously you know how that textbox value change is supposed to relate to the data being fetched, if it does, and would check for its value (it might be new) before fetching the data again.

    Friday, October 16, 2020 12:14 AM
  • User303363814 posted

    what is a good solution?
    Maybe education?

    For keyboard users you exit a field with 'tab', (or 'shift+tab' to go backwards).  It's a good lesson because it should work on all forms in all situations (native Windows apps or html based forms).

      Don't prevent the 'Enter' key from doing its normal, expected behaviour.

    Friday, October 16, 2020 1:01 AM
  • User1535942433 posted

    Hi coreysan,

    However, if I wanted to change the contents of a textbox, once I press <enter> the code inside the submit button runs and reloads the same SQL Server data. That means anything I changed will just revert back.

    So how can I get the software to not run the submit button code, when I change a textbox value?

    Accroding to your description,I don't understand your requirment  clearly. You changed the textbox's value and enter the submit button.It will postbcak,so you could revert back.You could use !IsPostBack in the code behind.

    What's meaning of don't run submit button when you change the textbox value? You have click <enter>,it is same with click.So,you could use <tab> to blur on the textbox.

    Best regards,

    Yijing Sun 

    Friday, October 16, 2020 3:01 AM
  • User248267340 posted

    Kathy,

    absolutely brilliant suggestion. For some reason it didn't work in my project. If I change the contents of any testbox and press enter,

    it still reverts back to the original value. Based on what others have said, it's because the postback still re-executes my main button for some reason!

    Friday, October 16, 2020 4:18 PM
  • User248267340 posted

    Education - you bet I need it! I'm learning to code ASP.net for the first time, but I don't have any kind of tutor or class I can take. So I read online

    material, and listen to all you people!

    By the way - did you know the singer Ella Fitzgerald worked with a jazz pianist named Paul Smith? He was brilliant at jazz.

    Friday, October 16, 2020 4:21 PM
  • User248267340 posted

    Hi! From what I've learned so far, apparently when a user presses <enter> it fires the primary default button, which in my project is a record-lookup in SQL Server. I was hoping to isolate a user's keypress for changing values inside any textbox, but that's not going to work.

    So I'll train the users to rely on <tab> instead!

    Thanks for pitching in. I've learned a lot!

    Friday, October 16, 2020 4:23 PM
  • User409696431 posted

    I use it all the time, and it works.  I suspect you've got something else going on that you are not handling properly, but you  haven't shown us your code so I can't guess.

    Friday, October 16, 2020 5:03 PM
  • User248267340 posted

    Thanks for offering to look at my code. I have no idea how much you want to see, so I'll start with this:

        <form id="form1" runat="server" defaultbutton="btnDisable">
            <div class="container modal-xxl">
                <div class="row">
                    <div class="col-lg-4">
                        <asp:Label ID="lbl_Claimno" runat="server" Text="Claimno:"></asp:Label>
                        <asp:TextBox ID="txt_Claimno" runat="server" MaxLength="20" CssClass="font-weight-bold" ></asp:TextBox>
                        <asp:Button ID="btnDisable" runat="server" Visible="false" OnClientClick="return false" />
                        <asp:Button ID="btnLookup" runat="server" width="80px" height="24px" Text="Lookup" OnClick="btnLookup_Click" />
                        <asp:Button ID="btnClear" runat="server" Text="CLR" OnClick="btnClear_Click" CssClass="btn btn-small btn-success LH ShiftRight"/>
                    </div>
    
                    <div class="col-lg-4">
                        <asp:Label ID="lbl_DCN" runat="server" Text="DCN:"></asp:Label>
                        <asp:TextBox ID="txt_DCN" runat="server" CssClass="font-weight-bold" ></asp:TextBox>
                        <asp:Button ID="mmm3" runat="server" OnClick="mmm3_Click" Text="3M" CssClass="btn btn-small btn-primary LH ShiftRight"/>
                    </div>
    
                    <div class="col-lg-4">
                        <asp:Button ID="btnProcess" runat="server" OnClick="btnRefresh_Click" Text="Refresh" CssClass="btn btn-small btn-primary LH ShiftRight"/>
                    </div>
                </div>
            </div>
        </form>
    
    

    In the code behind, I have a method:

            public void btnLookup_Click(object sender, EventArgs e)
            {
                int ilen = txt_Claimno.Text.Length;
    
                 (I call SQL Server from here...)
            }

    So when I load the page, I use the btnLookup button to fetch data, and whenever a user presses <enter> it automatically runs the btnLookup all over again. I'm only puzzled as to why a postback would run btnLookup only, and not all the other buttons too!!!

    Friday, October 16, 2020 7:46 PM
  • User409696431 posted

    "So when I load the page, I use the btnLookup button to fetch data"

    Do you load the page first, then click the button?  Or are you loading the data in the Page_Load function when the page is first rendered?

    To load the data the first time, when the page is first loaded, do it inside an is not post back condition.  Then only load it again when you explicitly click the button.

    Since you didn't show your page_load code, I can't know if this is the problem or not.

    private void Page_Load()
    {
        if (!IsPostBack)
        {
            // Stuff you want to do only on the first page load
        }
       //Stuff you want to do on every post back
    }

    Also, why is there a modal-xxl style there? Is this a modal popup?

    Friday, October 16, 2020 8:16 PM
  • User409696431 posted

    I just noticed something.  You did not follow my disable button instructions exactly.

    You have:

    <asp:Button ID="btnDisable" runat="server" Visible="false" OnClientClick="return false" />

    Visible false is not the same thing as display:none.  Use

    <asp:Button ID="btnDisableEnter" runat="server" Text="" OnClientClick="return false;"
                    Style="display: none;" />

    Visible false means the button is not even in the page.  Display none means it's in the page, but not displayed to the user.

    Also, just because the default button is associated with the whole form, and you don't want it to not be there if anything in your form is not in the page at the time (templates that are not active, visible false applied to a container its in, and so on), I generally park that button right before the closing </form>.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2020 9:51 PM
  • User303363814 posted

    Visible false means the button is not even in the page.  Display none means it's in the page, but not displayed to the user.
    Other way around, I think

    Visible: false means in the page but not shown

    display: none means removed from the page

    (https://www.w3schools.com/cssref/pr_class_visibility.asp)

    Friday, October 16, 2020 10:23 PM
  • User409696431 posted

    I should have been more precise.  An asp.net control with an attribute Visible="false" will not be put in the page.  A style of display:none will still have the element in the page, taking up no space. (View source to see this)  A style of visibility:hidden (not visible:false, there is no such style) will be invisible but still take up space in the page.

    It's the Visible="false" attribute of the asp.net control, the button, that means it's not in the page at all.

    Friday, October 16, 2020 10:42 PM
  • User248267340 posted

    Ahh yes. You are correct. I didn't have display:none. So once I added it, it worked as expected. It more or less disables the keypress for <enter>.

    It doesn't advance to next field, which makes sense - in which case I think I'll stick with <tab>.

    This has been a great experience in learning. Thanks to you and PaulTheSmith!

    Friday, October 16, 2020 10:48 PM