ASP.NET :: Can a textbox control's onblur/lostFocus event trigger a server-side event? - asp.net

I am trying to attach a server-side event to lookup the city/state for the user-entered zipcode in a field like the one below.
<asp:TextBox ID="TextZipcode" runat="server" CssClass="inputtext" Columns="10" MaxLength="10"></asp:TextBox>
Since there is no lost focus event to capture, has anyone had any luck getting this to work?

Sure, why not? Just attach a JavaScript handler and have it invoke an AJAX action.

Related

Radcombobox OnItemRequested firing page validation

I have some really weird stuff going on here.
I have the following RadComboBox and button in the master page:
<telerik:RadComboBox ID="rcbPesquisa" runat="server" CausesValidation="false" ShowToggleImage="false" LoadingMessage="Carregando..." HighlightTemplatedItems="true" OnClientKeyPressing="onKeyPressing" EmptyMessage="Buscar" EnableLoadOnDemand="True" EnableVirtualScrolling="true" OnItemsRequested="rcbPesquisa_ItemsRequested" AllowCustomText="True" AutoPostBack="true" OnSelectedIndexChanged="rcbPesquisa_SelectedIndexChanged" Width="350">
<asp:Button runat="server" ID="btnExcluir" Text="Excluir" OnClick="btnExcluir_Click" CausesValidation="true" ValidationGroup="Excluir" CssClass="btn" OnClientClick="return confirm('Deseja realmente excluir o registro?');" />
In the page, I have this validator:
<asp:CustomValidator ID="cuvExclusaoRelacionamento" runat="server" ValidationGroup="Excluir" OnServerValidate="cuvExclusaoRelacionamento_ServerValidate"></asp:CustomValidator>
What happens is: When i click the combobox, and it tries to load it's items, cuvExclusaoRelacionamento validator is called on the server, and of course things go south.
By the way... __EventTarget on cuvExclusaoRelacionamento_ServerValidate is empty
Unfortunately, and without seeing the full code I'm only guessing, I believe there is no way of getting around this due to the following settings:
ValidationGroup="Excluir" The ValidationGroup property assigned to an input control is the Validation Group which should be triggered when the control posts back; The ValidationGroup property of a validator control is the name of the group to which it is assigned; Finally the ValidationGroup property of a submitting control (which could be a button but can also be the input control) is the name of the group of Validators to validate the page against before posting back to the server. I should point out at this timethat it will not post back (server-side event handlers will not be triggered) if the validation fails. Given that the validation group of the control is the same as the validation group of your Custom Validator the validator will be triggered when the control attempts to post back to the server (e.g. when being clicked), if this validation fails it will not post back.
AutoPostBack="true" When you set the control to auto post back it will post back to the server when updated. Given you have an OnClick event I'm guessing you need to perform some server side action when the state of the control is changed.
CausesValidation="true" When set to true all validators assigned to the same "Validation Group" as the submitting control will be triggered when the control attempts to post back to the server. As I've said above, this will prevent post back (meaning the server-side Event Handlers will not be triggered) if the validation fails.
Given the information provided, I'm guessing that the best action you could take is to set CausesValidation="false" for the control. The validation can still be performed so long as you have a submit button (or other control which triggers a post back) elsewhere which is assigned to the "Excluir" Validation Group.

How can I insert two OnClick event handlers into an .ascx user control?

Bearing in mind I have started a new job which demands I use Umbraco and ASP.NET - platforms/languages I've only been toying with for 2 days and am a total noob at - I have a query regarding inserting a Google Analytics event tracking code into a line of an .ascx file.
Here goes: I've drilled down into the specific line I'm required to insert a .gaq tracking code into; it's an onclick handler that corresponds to a submit button on a contact form. The line is:
<asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" CausesValidation="true" CssClass="btnSubmit floatRight">
<span>Submit</span>
</asp:LinkButton>
I'd like to add an event tracker to that OnClick property in the form of "_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])" and set up a relevant Event menu in GA. Only thing is, I don't know how to add the code into the existing OnClick setup - it's not a case of separating the two with a semi-colon, as I could do with JavaScript entries in normal HTML (eg, onclick="blah(); dah();").
Anyone able to tell me how to set two event handlers in one OnClick property in ASP.NET? If I sound murky on this it's because this language is extremely new to me.
Cheers.
Use OnClientClick which is used to run JavaScript Before the page posts back.
Something like:
<asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" OnClientClick="_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])" CausesValidation="true" CssClass="btnSubmit floatRight">
<span>Submit</span>
</asp:LinkButton>
Another solution would be to do:
Page.ClientScript.RegisterStartupScript(this.GetType(), ="_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])", true);
in the codebehind eventhandler for the click. This will output the script so that it is run when the page is loaded by the browser.
Hope this helps!
As you mention you need one more click event for Google Analytics only.
I far as I know, Google Analytics event tracking code is in javascript. So, in this scenario, you can use OnClientClick event of LinkButton for Google Analytics click event handling. as :
<asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" CausesValidation="true" CssClass="btnSubmit floatRight" OnClientClick="gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])"></asp:LinkButton>.
This should work fine!
You can use the OnClientclick property of the button and use Javascript function to push the code.
Thanks one and all. I've inserted the code - now I guess I wait. I can't see the category, action or label showing up in my GA account, but I have heard that Analytics takes a while to update - or will someone actually have to trigger the event first for the event tracking to fire?

How can I persist the changes of a text box when it loses focus?

I have several text boxes on a page. I want to save the text in the corresponding TextBox on LostFocus. It is to ensure the data is not lost when power failure or internet connectivity is lost. How can I accomplish something like this?
Another solution would be to use an UpdatePanel. You could then leverage server-side events rather than an event handler. You're markup might look something like this:
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<asp:TextBox ID="SomeTextBox" runat="server" TextChanged="SomeTextBox_TextChanged" AutoPostBack="true" />
</asp:UpdatePanel>
</asp:ScriptManager
and then in the TextChanged handler you could do your work.
Explanation: the TextChanged event will fire on the text box in which the text was actually changed when the post back occurs. The AutoPostBack property is necessary so that the page will in fact post back when the text box loses focus.
One final thing to remember, you will probably need to leverage this.IsPostBack on the page in the Load handler because every time a control is updated it's going to reconstruct the page. It's common that logic in the Load handler only needs to execute one time, but that would pose a problem if you didn't check to see if it was a post back because then it would execute every single time you left a text box.
Use jquery.
First on attach blur event handler, where you call ajax method to server passing new value of the textbox.
Handle this ajax event on serverside and write your data to the database or anywhere else.
Here is a piece of code that may help.
$('#textbox_id').blur(function() {
$.ajax({url:"handler_url.ashx?textbox_value=" + $('#textbox_id').val()});
});
Then create your ashx handler on server and handle your requests with ProcessRequest method. From there you will have access to Request.QueryString where new value of textbox will be stored.

ajax Combobox not firing event when combobox is empty

In my .aspx I've written the following:
<ajaxToolkit:ComboBox ID="cmbAddressAlias" runat="server" DropDownStyle="Simple" AutoCompleteMode="Suggest" CaseSensitive="false" AutoPostBack="true" RenderMode="Inline" Width="170px" CssClass="cmbProvince" OnSelectedIndexChanged="cmbAddressAlias_SelectedIndexChanged"> </ajaxToolkit:ComboBox>
It's binding correctly (datasource dynamically binded) and it raises event too, while changing index.
However it's not raising the event when I manually clear the combobox text.
If currently combobox having text "ASP" then I manually select that entire text and using del key I am deleting but it is not raising event for me. When I change index it automatically raises the event.
I need to raise the event while combobox is empty.
You should handle this event as well "OnTextChanged" and note that it's different than "OnSelectedIndexChanged" which has been handled by you. Handle this event and it will fire when you clear the text on the combo.
And if the above does not work this post might help you.

How to send info from one postback handler to another?

I have a third-party CustomControl which requires some info for databinding or anything else what it happen in a postback event handler.
Trying to send this info via, say, dropDownList i face that this dropdownlist postback event handler isn't firing before CustomControlEventHandler, as it happens in ASP.Net. What is usual workaround?
Or I should never rely on server side transfer and try to transfer this info on client-side etc.?
try
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"></asp:DropDownList>
note: AutoPostBack="true"

Resources