Buttons stop working when UserControl is added to page - asp.net

I have a content page with a UserControl. When the UserControl is added to the page, all of the buttons, including buttons in the master page, stop working. If I remove the UserControl the buttons start working again. By not working, I mean if you click them nothing happens at all, no postback. Some buttons still work, such as the GridView header labels which sort the columns. These cause postback, but nothing else.
The only elements in the UserControl are a fieldset containing a html label, containing a RequiredFieldValidator and a TextBox.
This solution here mentions solving a similar problem by including a !isPostBack check to the Page_Load, but what is included? My UserControl Page_Load event is currently empty.
Update
User Control .ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="StaffEditFields.ascx.cs"
Inherits="StaffEditFields" %>
<fieldset>
<label>
Username
<asp:RequiredFieldValidator ID="rfdUser" runat="server" Display="Static"
ControlToValidate="txtUser" />
<br />
<asp:TextBox ID="txtUser" runat="server" CssClass="fvBox" Text='<%# Bind("Username") %>' />
</label>
</fieldset>
Update 2
Removed the fieldset and nothing changed, but when I removed the RequiredFieldValidator the page began to function normally. Now I need to ask how to keep the RequiredFieldValidator and allow the UserControl to function properly?

I figured it out. The problem is the RequiredFieldValidator is not set to validate to any specific button, therefore it will try to validate on ANY button click on the page. I also failed to set any button to CausesValidation="true".
The solution is then to set a validation group for the RequiredFieldValidator and set a button inside or outside the UserControl to CausesValidation="true" and ValidationGroup=[group].

Related

Why ASP.NET Controls defined in mark up already created on Page_PreInit life cycle event

I read Microsoft's article on ASP.Net Page Life Cycle events. It confused on one thing. When Page_PreInit gets called, all the controls' Init method is not yet called. When I setup a test project, I observed a different behavior. In the mark up, I created asp label and button controls and set certain properties such as Text. I put a break point at the beginning of Page_PreInit. When break point got hit, I checked if the controls got created or not by referring to them by their Ids in the Watch window. They all existed and none returned null. Then I checked the Text property and it was what I set in the mark up. So doesn't this contradict what Microsoft says? If this is the case what do Controls' Init method do if they are already initialized? Is there something I miss?
I created asp label and button controls and set certain properties
such as Text. ... Then I checked the Text property and it was what I set
in the mark up.
If you set property value at Design Time, they are in control tree so properties are available in all events.
However, if you add a TextBox and a user clicks on submit, TextBox Text Property only start available at Page_Load event.
The reason is that Page_Load is the place where properties are loaded with information recovered from view state and control state.
Look at this example
<asp:Label runat="server" ID="Label1" Text="Name" />
<asp:TextBox runat="server" ID="TextBox1" />
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
<asp:Button runat="server" ID="SubmitButton"
Text="Submit" OnClick="SubmitButton_Click" />
After Postback,
Notice that you can only retrieve TextBox value at Page_Load event.

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?

I have one issue for ViewState

What is the meaning of EnableViewState="false" and EnableViewState="true"?
I Know EnableViewState="false" = turn Off the ViewState and also EnableViewState="true" = turn On the ViewState
But What is the difference between EnableViewState="false" and EnableViewState="true" ?
I tried this code:
<form runat="server">
<asp:TextBox ID="TextBox1" EnableViewState="true" runat="server">
</asp:TextBox><asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
I am really confused.
When I used EnableViewState="true", I entered some values in textbox and click my button .Now the value is here in the textbox . Its same process when i set EnableViewState="false".
So What happens when EnableViewState="true" and EnableViewState="false" ?
Texbox Doesnt Use Viewstate here is the link to explain all Link Explain
Generally you should use EnableViewState="false" on all controls on the asp.net page. The viewstate of a control is most commonly needed when you want to preserve some visual appearance of the control itself. E.g. if you change the background color of control and you want to persist that across postbacks use EnableViewState="true".
Not all controls are affected by view state. The controls that implement IEventHandler or IDataHandler will not get affected on page postback if view state is disabled.
Textbox is one such control. If you want to see the effect in your code. Try setting the label value at run time on postback like on Click of button and check the results
ViewState is used to persist properties of a control that are set server-side.
So, to take a contrived example, if you do something like the following in Page_Load:
if (!IsPostBack)
{
TextBox1.ForeColor = ...;
}
then the color you set will be preserved across postbacks in ViewState, if it is enabled.

2 user controls on registered on one aspx page not validating validation properly

I have 2 user controls on registered on one aspx page.
UserControl1 us having one text box with require field and one submit button.
UserControl2 is also having one text box with requirefiled and save button.
Expected o/p is-
When I am clicking on any button out of 2(submit or save). Then only related text boxof that user control should be validate.
But the error is
Both text boxes are validate.
Please help me .
Set the ValidationGroup properties to limit which fields get validated when the buttons are pressed.
So for example, if these were contained within the first user control:
<asp:requiredfieldvalidator id="NameValidator"
controltovalidate="NameTextBox"
validationgroup="UserControlOne"
errormessage="required"
runat="Server" />
<asp:button id="Submit"
text="Submit"
causesvalidation="true"
validationgroup="UserControlOne"
runat="Server" />
Clicking the "Submit" button would only cause the validators that have UserControlOne specified as the ValidationGroup to validate.
Edit: When you call Page.Validate() you are validating every group on the page. Call the overloaded Page.Validate(validationGroup) to validate a specific one. e.g. Page.Validate("UserControlOne")

UpdatePanel with GridView with LinkButton with Image Causes Full Postback

So this might be a fairly specific issue but I figured I'd post it since I spent hours struggling with it before I was able to determine the cause.
<asp:GridView ID="gvAttachments" DataKeyNames="UploadedID" AutoGenerateColumns="false" OnSelectedIndexChanged="gvAttachments_SelectedIndexChanged" runat="server">
<EmptyDataTemplate>There are no attachments associated to this email template.</EmptyDataTemplate>
<Columns>
<asp:TemplateField ItemStyle-Width="100%">
<ItemTemplate>
<asp:LinkButton CommandName="Select" runat="server"><img src="/images/icons/trashcan.png" style="border: none;" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In the ItemTemplate of the TemplateField of the GridView I have a LinkButton with an image inside of it. Normally I do this when I have an image with some text next to it but this time, for whatever reason, I just have the image. This causes the UpdatePanel to always do a full postback.
Instead of changing the markup, you can goto web.config and specify ClientIDMode="Auto" in the pages tag.
Reason why UpdatePanel behaving like this is because the ClientIDMode is getting generated will be too long for UpdatePanel to register. So the ClientID got truncated in middle and such control will be treated like unregistered Control.
For more information read the following:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Change the LinkButton to be an ImageButton and the problem is solved.
<asp:ImageButton ImageUrl="/images/icons/trashcan.png" Style="border: none;" CommandName="Select" runat="server" />
Above solutions also work, But There is one more thing to check. Check form tag for your page. If id attribute is missing, you will get same issue.
If you form tag is as given below (without id), you will get issue:
<form runat="server">
<!-- your page markup -->
</form>
Please add id, as given below:
<form id="form1" runat="server">
<!-- your page markup -->
</form>
You will not need to update ClientIDMode in web.config or page or control.
You will not need to change your linkbutton in markup.
You will not need to register control for asynch postback from code behind.

Resources