Values Entered in a textbox or radio button list changes after firing an event clear when the event posts back? - asp.net

In ASP.net I have a simple textbox and radio button list with yes or no options. On the VB server-side I implement OnClick of the radio button and it does things.
<asp:RadioButtonList ID="rbltest" runat="server" AutoPostBack="true" OnSelectedIndexChange="rbltest_SelectedIndexChanged" <asp:ListItem Text="Yes" Value="0"></asp:ListItem>
<asp:ListItem Selected="True" Text="No" Value="1"></asp:ListItem>
</asp:RadioButtonList>
The only problem is simply that if I click the radio button, then enter data into the TextBox (immediately - in less than 2 seconds), when the radio button returns, it deletes the data. This is annoying. Is there anyway around it?

The only way to fix this is to remove AutoPostBack=true on the RadioButtonList control. The auto-postback option immediately initiates an HTTP post back to the server, so anything typed after then is going to be lost.
Other options:
Implement a client-side onclick handler, and disable the text while the post-back processes (ie, make it clear to the user that they can't type anything, aka the principle of least astonishment).
Implement the change logic completely on the client side using Javascript
Use the ASP.Net AJAX UpdatePanel to do the postback asynchronously (this would not require a full postback to the server, so would allow you to keep typing).

You must delete EnableViewState="false" on your control who lose data, and let default value of ViewState (true) on control.

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.

OnTextChanged loses focus when AutoPostBack is true

I have a ASP.Net webform that has several textboxes. Some of the textboxes have an OnTextChanged event with AutoPostBack set to true.
When the user enters some text and leaves the textbox, I want some code to run. This part works fine.
The problem is that if a user enters some text, then clicks or tabs to another textbox, the OnTextChanged of the current textbox event fires fine but the textbox that the user clicked on does not keep focus. This causes problems because the user thinks they are on the next textbox but they aren't. And no object seems to have the focus.
Is there anything that can be done to make the next object keep focus while the OnTextChanged event of the current textbox fires?
One option is to use <asp:UpdatePanel> Control.
Facts about using it:
The postback request would be made via AJAX.
It would not recreate the whole page HTML.
When the UpdatePanel updates, it replaces the DIV innerHTML, that would make the textbox lose focus (bad point).
To maintain the focus, you would have to avoid the UpdatePanel from updating when the textbox posts back.
You can avoid the update by setting the UpdateMode and ChildrenAsTriggers properties.
Here is an example:
<asp:UpdatePanel ID="uppTextboxes" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txb1" runat="server" AutoPostBack="true" OnTextChanged="txb1_OnTextChanged" />
<asp:TextBox ID="txb2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Since the whole page is recreated on postback on serverside and the browser will recreate all html on clientside, you have to tell ASP.NET that your TextBox needs focus.
Use Page.SetFocus:
Page.SetFocus(IdOfControl);
However, i would prefer not to postback at all if i can. Can't you use a button that the user has to click after he has entered all necessary data?

asp.net textarea postback before submit button

I have an ASP.NET form with a few controls and a submit button at the bottom, all inside an update panel:
<asp:UpdatePanel runat="server" ID="upContent">
<ContentTemplate>
<asp:TextBox runat="server" ID="tbxMyTextBox" AutoPostBack="true" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return doStuff()" OnClick="btnSubmit_Click" />
</ContentTemplate>
</asp:UpdatePanel>
If I write something in the TextBox and click 'submit' immediately (without clicking out of the TextBox first), the changes are not recorded (as seen in the server-side event handler). However, if I write something in the TextBox, and then change focus to another control, an AutoPostback happens through the UpdatePanel and then clicking 'submit' does recognize these changes. How can I force this content to update right as I click the submit button, while still running both the clientside and serverside events attached to it? Thanks!
Is is possible that your text-box gets cleared due to some on-submit/on-click event handler? I will suggest you do use some tool such as Fiddler to inspect the POST data in request. Or you can put a break-point at your server side code and inspect the request data. Look for particularly Request.Form[tbxMyTextBox.UniqueID] - i.e. look for presence of value for your text-box's name (name property at client side which corresponds to UniqueID property at server side). If the request contains the value typed in the text-box then something is happening on server side but good news is you can always retrieve the value from Request object. If the value is not present in the Request object then it means that client side code is clearing the value before the submit.
If an onclick method returns false, it cancels the action that the button would normally perform. As your button would normally submit your form, but it appears not to be submitting, your client side javascript code in doStuff is probably returning false.

ASP.NET: CheckBox in Page hidden by UserControl does not stay checked when made visible again

Here's the setup. I have an ASP.NET4 Page with viewstate enabled, with a RadioButtonList with 2 radio buttons, a CheckBox, and an included UserControl. I want to hide the CheckBox if one radio button is selected, and show it if the other is selected. I want the CheckBox to maintain it's state when it is hidden and re-shown; if it was checked before hiding, when it is shown again I still want it to be checked.
The twist is that the code to show/hide the CheckBox, based on what radio button is chosen, lives in the OnPreRender event of the UserControl. So it does Parent.FindControl()s to get the controls, then hides/shows the CheckBox based on the state of the RadioButtonList.
The issue is, when doing this logic in OnPreRender() of the UserControl, the checkbox does not maintain its state after it is hidden, then reshown. That is, if the checkbox is checked, then I click the radio button to hide it, when I click the other radio button I want the checkbox to still be checked when it is now shown. I have a feeling I'm not understanding some of the view state mechanisms, but when I tried adding a TextBox and hiding/showing it, it did maintain it's text value as expected.
I can move the logic to Page_Load of the UserControl and the checkbox state behaves as expected. But I'm trying to get it to work in OnPreRender(), or at least looking for an explanation as to why I'm seeing this behavior.
In default.aspx:
<asp:RadioButtonList runat="server" ID="uxRadio" AutoPostBack="true">
<asp:ListItem Text="choice 1" Value="1"></asp:ListItem>
<asp:ListItem Text="choice 2" Value="2"></asp:ListItem>
<asp:ListItem Text="choice 3" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<asp:CheckBox runat="server" ID="uxCheck" AutoPostBack="true" />
<uc1:Control ID="uxControl" runat="server"></uc1:Control>
UserControl.ascx.cs:
protected override void OnPreRender(EventArgs e)
{
RadioButtonList rb = (RadioButtonList)Parent.FindControl("uxRadio");
CheckBox cb = (CheckBox)Parent.FindControl("uxCheck");
if (rb.SelectedValue == "2")
{
cb.Visible = false;
}
else
{
cb.Visible = true;
}
base.OnPreRender(e);
}
Ok, i could reproduce this issue but i'm not 100% sure why the ViewState is not retained.
I assume that something like this happens:
ASP.NET retrieves the checked-state from CheckBoxes from the posted request but only if it's Visible on serverside(otherwise it wouldn't even be rendered as html). If it's invisible it gets the checked-state from ViewState.
I think that the UserControl's PreRender is too late in this case. ASP.NET does not know that it has to save this value in ViewState because the condition(Visible-State) is changed afterwards. Hence there is no posted value and no ViewState value and ASP.NET must use the default-state(unchecked).
If you move this code from PreRender to Page_Load it works.
But i would strongly recommend to move it to the Page itself because it really belongs there. A usercontrol should not insist on the existence of specific controls in it's page because it should also work in other pages.

ASP.NET CheckBox disabling postback with javascript

I'm trying to wire up a CheckBox to handle an event when check/unchecked. If the user has JavaScript enabled, use that, otherwise use a postback.
Here is my code:
<asp:CheckBox ID="ApplicationInProcessCheckBox" runat="server"
Text="Application In Process" AutoPostBack="true"
oncheckedchanged="ApplicationInProcessCheckBox_CheckedChanged"
onclick="return false;" />
The return false in the javascript onclick event is disabling the postback. However, it also won't let the box check or uncheck. (I have more code to add to the javascript event... I just want to get the concept working first).
What am I doing wrong?
I think we can't post back on clicking checkbox without Javascript enabled.

Resources