Asp.NET dropdownlist autopostback - asp.net

I have a dropdownlist with autopostback enabled. and have a repeater where i populate checkboxes from database.
If i set the autopostback to true then when selecting a value checkboxes lose its value...
Any workarounds on this?
Here is the code :
<asp:DropDownList ID="dropdown" runat="server" class="pop" AutoPostBack="true" >
</asp:DropDownList>
<asp:Repeater ID="rptD" runat="server" >
<ItemTemplate>
<td valign="top" >
<input type="checkbox" class="al" />
</ItemTemplate>
</asp:Repeater>

I assume this is because you are DataBinding the Repeater not only if(!IsPostBack) but also on postbacks. Therefore the checked state will be overriden.
So do this in Page_Load(assuming C#):
if(!IsPostBack){
DataBindRepeater();
}
Whereas DataBindRepeater is a method that sets the DataSource property and DataBind the Repeater.
You might also want to use an ASP.NET Checkbox control instead of the html input type="checkbox". The checked state is reloaded only if it's a server WebControl that implements IPostBackDataHandler.

This sounds indicative of populating the checkboxes in Page_Load. Is that the case? If you're populating the controls in Page_Load then you'll want to wrap it in a conditional:
if (!IsPostBack)
{
// populate your controls from data
}
Otherwise, they'll get re-populated with each postback. When you have an autopostback or click a button or perform some other action on the page which initiates a postback, Page_Load is called before the event handler. So in effect, this is happening:
User navigates to the page
Page_Load clears and populates the checkboxes
User chooses an item in the DropDownList (triggering a postback)
Page_Load clears and populates the checkboxes
DropDownList autopostback handler is called
(On a side note... Please look into using AJAX for dynamic client-server interaction like this. Autopostback makes for a poor user experience, and as you're discovering also makes for a difficult development experience.)

Related

Button in GridView, OnCommand, Event Bubbling, Master Page

Prerequisite, I am fairly new to event bubbling.
I have a page with a gridview that has buttons in the rows. When the button is clicked, it fires an OnCommand event and everything works as expected.
<ItemTemplate>
<asp:Button ID="btnG" runat="server" OnCommand="btnG_Command" Text="View"/>
</ItemTemplate>
... (on the codebehind) ..
Protected Sub btnG_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
'Does some stuff
When I added a textbox (with validators) and a button on the Master Page, now when the buttons in the gridview are clicked, it fires the validators in the Master page for some reason.
<asp:RequiredFieldValidator ID="ReqValidator" runat="server" ControlToValidate="txtTextbox" Display="Dynamic" ErrorMessage="Please enter a valid number."></asp:RequiredFieldValidator>
Through some research I found that it has to do with event bubbling somehow since the button controls in the gridview are using OnCommand rather than OnClick.
My question is why is it firing the validators off in the Master page? Shouldn't the bubbling first go through the GridView instead of the Master page? How can I bypass the validators or event bubbling so that the correct event is fired from the content page?
You should add a validation group to your validator in the master page. Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.
You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group
Source: https://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx
<asp:RequiredFieldValidator ID="ReqValidator" runat="server"
ControlToValidate="txtTextbox" Display="Dynamic"
ErrorMessage="Please enter a valid number."
ValidationGroup="MasterNumber"></asp:requiredfieldvalidator>

Row click in GridView when ViewState is disabled

We are using classic ASP.NET 4. There is a GridView that's causing problems because there is no paging and the ViewState is too large. So, disabling the view state via EnableViewState="False" seemed to solve the problem with the viewstate size, but now I can no longer click on rows. The page refreshes, but the call-back corresponding to CommandName="OpenItem" is not firing.
<ItemTemplate>
<asp:ImageButton
ID="Id"
runat="server"
CommandName="OpenItem"
/>
</ItemTemplate>
I tried adding OnClick="ClickHandler" to the ImageButton, but that didn't work either -- the OnClick callback is not firing.
Is there any way, other than adding paging, to have the ViewState disabled and handle clicks on GridView rows?
Thanks.

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: 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 Repeater template sub-control visibility before databind

I have a custom control which contains a Repeater control. The Repeater has an ItemTemplate. Inside that item template I have a panel which is going to hide something based on "IsEditable" a boolean property of the custom control. What I would like to do is set the panel's visibility once before the Repeater is databound.
I know I could do an onItemDataBound event and use FindControl to get the panel but that seems a little excessive since it will always be either visible or not for all rows and I have no other actions that need to occur on databind.
Is there a way to find the control in the ItemTemplate before the Repeater is databound?
try this:
<ItemTemplate>
<asp:Panel Visible='<%# this.IsEditable %>' runat="server">
editableStuff
</asp:Panel>
</ItemTemplate>

Resources