How do I act upon two events at once in asp.net?

  • Thread starter Thread starter Rnes
  • Start date Start date
R

Rnes

I have a textbox defined ..

<asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></
asp:TextBox>

So it will fire off the tbSubmitterId.TextChanged event.

Here is the problem,

1. the user enters a value in the textbox.
2. then they click the "Submitt" button (without moving out of the
textbox)
3. The "TextChanged" event is fired but the button event is not
fired. So basically the user would have to
click the button twice.

How do I tell in the "TextChanged" subroutine that the button was
clicked so I can process that event also?
 
Rnes said:
I have a textbox defined ..

<asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></
asp:TextBox>

So it will fire off the tbSubmitterId.TextChanged event.

Here is the problem,

1. the user enters a value in the textbox.
2. then they click the "Submitt" button (without moving out of the
textbox)
3. The "TextChanged" event is fired but the button event is not
fired. So basically the user would have to
click the button twice.

How do I tell in the "TextChanged" subroutine that the button was
clicked so I can process that event also?

A simple way is to get both events to call another method which does what
you want:

sub someAction()
' do the required action here
end sub

sub textBoxChangedHandler()
someAction()
end sub

sub buttonClickedHandler()
someAction()
end sub

HTH,

Andrew
 
Rnes said:
I have a textbox defined ..

<asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></
asp:TextBox>

So it will fire off the tbSubmitterId.TextChanged event.

Here is the problem,

1. the user enters a value in the textbox.
2. then they click the "Submitt" button (without moving out of the
textbox)
3. The "TextChanged" event is fired but the button event is not
fired. So basically the user would have to
click the button twice.

How do I tell in the "TextChanged" subroutine that the button was
clicked so I can process that event also?

First, I would personally get rid of the TextChanged event unless you are
doing something AJAX with it, as it causes confusion in the program flow.
You can decide whether that makes sense, but I see very few of these "every
control fires a server post" scenarios that are sane.

The answer to the problem at hand depends on what version of ASP.NET. The
rules are pretty much the same, however. You have to find the button in the
TextChanged event and call the action you want to perform if the button was
submitted. For example, in ASP.NET 4.0, both events would fire. I believe
this may be true in older versions, but I am fairly steeped in VS 2010 now.

Let's take a best practices approach:

1. The actual event handler calls routines and DOES NOT do the work

protected void SubmitButton_Click(object sender, EventArgs e)
{
//Don't do work here
SubmitForm();
}

private void SubmitForm()
{
//Do work here
}

2. In the text changed event, you need to test for the button submit. Here
is a fully qualified ASP.NET name version:

if(Request.Form["ctl00$MainContent$SubmitButton"] = "{Text on button}")
{
//Call form submit routine
}

But the naming might be more like this:

if(Request.Form["SubmitButton"] = "{Text on button}")
{
//Call form submit routine
}

So here is a TextChanged event:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
ChangeUserTextBox();

if (Request.Form["SubmitButton"] = "Submit")
{
SubmitForm();
}

}

In ASP.NET 4.0, you can alter naming declaratively to get to the more simple
syntax. :-)

Now, this might still not work in some versions of ASP.NET (don't have any
info on this, but if the JavaScript does not capture the Submit button on
the form submit from text changed, you will capture nothing).




--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 
Rnes said:
I have a textbox defined ..

<asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></
asp:TextBox>

So it will fire off the tbSubmitterId.TextChanged event.

Here is the problem,

1. the user enters a value in the textbox.
2. then they click the "Submitt" button (without moving out of the
textbox)
3. The "TextChanged" event is fired but the button event is not
fired. So basically the user would have to
click the button twice.

How do I tell in the "TextChanged" subroutine that the button was
clicked so I can process that event also?

You remove the AutoPostBack off of the Textbox. You get the TextBox.Text
at the Submit button click event and do whatever with the text value
before you do anything else in the button click event.

You get rid of the Textchanged event so only one event fires, the button
click event.
 
I have a textbox defined ..
<asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></
asp:TextBox>
So it will fire off the tbSubmitterId.TextChanged event.
Here is the problem,
  1. the user enters a value in the textbox.
  2. then they click the "Submitt" button (without moving out of the
textbox)
  3. The "TextChanged" event is fired but the button event is not
fired.  So basically the user would have to
      click the button twice.
How do I tell in the "TextChanged" subroutine that the button was
clicked so I can process that event also?

First, I would personally get rid of the TextChanged event unless you are
doing something AJAX with it, as it causes confusion in the program flow.
You can decide whether that makes sense, but I see very few of these "every
control fires a server post" scenarios that are sane.

The answer to the problem at hand depends on what version of ASP.NET. The
rules are pretty much the same, however. You have to find the button in the
TextChanged event and call the action you want to perform if the button was
submitted. For example, in ASP.NET 4.0, both events would fire. I believe
this may be true in older versions, but I am fairly steeped in VS 2010 now.

Let's take a best practices approach:

1. The actual event handler calls routines and DOES NOT do the work

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            //Don't do work here
            SubmitForm();
        }

        private void SubmitForm()
        {
            //Do work here
        }

2. In the text changed event, you need to test for the button submit. Here
is a fully qualified ASP.NET name version:

if(Request.Form["ctl00$MainContent$SubmitButton"] = "{Text on button}")
{
    //Call form submit routine

}

But the naming might be more like this:

if(Request.Form["SubmitButton"] = "{Text on button}")
{
    //Call form submit routine

}

So here is a TextChanged event:

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            ChangeUserTextBox();

            if (Request.Form["SubmitButton"] = "Submit")
            {
                SubmitForm();
            }

        }

In ASP.NET 4.0, you can alter naming declaratively to get to the more simple
syntax. :-)

Now, this might still not work in some versions of ASP.NET (don't have any
info on this, but if the JavaScript does not capture the Submit button on
the form submit from text changed, you will capture nothing).

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog:http://gregorybeamer.spaces.live.com

************************************************
|    Think outside the box!                        |
************************************************- Hide quoted text -

- Show quoted text -

Thanks Greg.

FYI, I am using ASP.net 2.0

I need the autopostback on the text box, because it does a lookup to
validate the code that was entered there. Most of the time the user
enters the data, then either hits TAB to go to the next field or
clicks on another part of the page to do something and then clicks on
the button.

I tried as you suggested (but with vb) ...

If Request.Form("BtnGenerateList") = "Generate List"
Then
Handle_GenerateList_Click()
End If

The problem is that since the event for the textbox change is fired
then "Request.Form("BtnGenerateList") " shows as nothing. But, if
just the button is clicked then the statment "If
Request.Form("BtnGenerateList") = "Generate List" " becomes true.
 
Rnes said:
Thanks Greg.

FYI, I am using ASP.net 2.0

I need the autopostback on the text box, because it does a lookup to
validate the code that was entered there. Most of the time the user
enters the data, then either hits TAB to go to the next field or
clicks on another part of the page to do something and then clicks on
the button.

That's a business rule and business rules shouldn't be at the Web UI. At
best, the Submit button should be doing all validation and look-ups
prior to a save/submit of the data has occurred.

If everything is valid, then the save/submit continues, otherwise it's
stopped.

You should consider on how to use a Model View Presenter pattern in your
Web UI application, as the Web UI should be a dumb UI, and all it does
is call events/methods on the Presenter.

It's the Presenter that does the look-up and applies directly business
rules or calls a Business Layer to apply business rules while the Web UI
remains dumb.

http://en.wikipedia.org/wiki/Model_View_Presenter
 
Back
Top