tracking variable values in asp.net ajax user control - asp.net

Working on an asp.net page I created a custom ajax web user control that displays the contents of a list value (which is loaded via database calls). The control includes "next" and "previous" buttons, as well as a couple text boxes. My initial version used a session variable (iterator) to keep track of where the user is while clicking the buttons and filling the text boxes with values from the List[].
In my next iteration, I need to be able to use this custom control, which will be presented many times in a table (or more probably grid), but I'm having trouble figuring out how to handle the iterator, since the session variable will obviously no longer work.
Does anyone have a suggested approach, or a recommended article?

I solved the problem by creating a second form on my user control that is segregated from the controls contained within the UpdatePanel. Before those controls are refreshed, the segregated control (a label in this case) is programmatically assigned a value and when the control Page_Load event fires it references the segregated label value to keep track of it's current position. I also had to move the ScriptManager out of my control and place it on the page(s) the control will be used on, since there will be several of these custom user controls on the page and only one ScriptManager is permitted per page.
<form id="NaviControl" >
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Panel ID="controlsPanel" runat="server" HorizontalAlign="Center" >
<asp:Panel ID="Panel1" runat="server" >
<asp:Button ID="btnLast" runat="server" Text="Previous" Height="25px" Width="75px" OnClick="btnLast_Click" />
<asp:TextBox ID="tbStatusCode" runat="server" ></asp:TextBox>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" HorizontalAlign="Center" >
<asp:TextBox ID="tbDescription" runat="server" ></asp:TextBox>
<asp:Button ID="btnNext" runat="server" Text="Next" Height="25px" Width="75px" OnClick="btnNext_Click" />
</asp:Panel>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
<form>
<asp:Label ID="lblCounter" runat="server" visible="false" />
</form>
Hope some other nubs find this approach useful.

Related

how to work with multiple forms in one asp.net page

I hava a asp.net that I thought is going to have multiple forms.I put a asp panel and added the form to it and validators to controls everyting works.but if I try to put another panel and a form page become invalid.the reason I do this is because any attempt to postback data will trigger all validtors.I know that I can brake each form to each page.but for my design this seems silly.
what im looking for a way to have multiple forms in one page.
or have validators work only on certain situations.
or group validators to work if the button click only happen in the same panel where validators reside.
You cannot place more than one form tag in a single ASPX page. It is a limitation of ASP.Net Web Form.
However, you can fake it with ValidationGroup. For example -
<%-- Group One --%>
<asp:TextBox ID="TextBox1" runat="server"
ValidationGroup="One" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
Display="Dynamic"
ValidationGroup="One"
ControlToValidate="TextBox1"
Text="Textbox one is required." />
<asp:Button runat="server" ID="Button1"
OnClick="Button1_Click"
Text="Submit"
ValidationGroup="One" />
<hr/>
<%-- Group Two --%>
<asp:TextBox ID="TextBox2" runat="server"
ValidationGroup="Two" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server"
Display="Dynamic"
ValidationGroup="Two"
ControlToValidate="TextBox2"
Text="Textbox two is required.." />
<asp:Button runat="server" ID="Button2"
OnClick="Button2_Click"
Text="Submit"
ValidationGroup="Two" />
Note: If you want multiple form tags in a single page, you might want to look at ASP.Net MVC.

Calendar control without postback

I notice the calendar control in ASP.NET does not have an AutoPostBack property. I want that when I select a date from the control, the control must not post back the page. How do I do that?and one thing more when i hover over on the calender it shows me a script. "javascript:__doPostBack('Calendar2','5098')". So my question is how to disable this javascript to don't postback the values
There is no way to turn off the postbacks to the server but you can use updatepanel control which can help you some what.
According to PLBlum at the ASP.NET forums:
The Calendar control included with ASP.NET uses postbacks only. If you
added Microsoft ASP.NET AJAX to the page and put the calendar into an
UpdatePanel, it can reduce the appearance of postbacks by using
callbacks. But it still makes a trip to the server for each click on a
date or month.
Many people have created replacements to the Calendar control that
uses javascript to do the work. I am the author of one, in "Peter's
Date Package". You can locate others in these sites: the Control
Gallery (here under Resources), www.123aspx.com, and www.411asp.net.
You can add like following
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Calendar ID="ad_Cal" runat="server" OnSelectionChanged="ad_Cal_SelectionChanged" ></asp:Calendar>
<asp:Button ID="btn_Del_Day" runat ="server" Text ="Remove Day" OnClick="btn_Del_Day_Click" UseSubmitBehavior="False" BackColor="#4AA3DF" Font-Bold="True" ForeColor="White" />
<asp:Button ID="btn_sort_Date" runat="server" Text="Sort Days" UseSubmitBehavior="False" BackColor="#4AA3DF" Font-Bold="True" ForeColor="White" OnClick="btn_sort_Date_Click" />
<asp:ListBox ID="lst_ad_dates" runat="server"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
notice that you should use UseSubmitBehavior="False" with buttons also to stop postback for buttons in the same updatePanel
It works fine
I think you can use "CalendarExtender" from AjaxToolkit
<asp:TextBox ID="txtCalendarExtender" runat="server"></asp:TextBox>
<cc3:CalendarExtender ID="Calendar1" PopupButtonID="imgPopup" runat="server" TargetControlID="txtCalendarExtender" Format="dd/MM/yyyy">
</cc3:CalendarExtender>
Remember to add this code at the top of your page:
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc3" %>

Insert.aspx.vb - Set focus to control in dynamic FormView

I am having a bit of trouble with a dynamic asp.net FormView (within Insert.aspx.vb) in my ASP.NET LINQ to SQL Website. Here is the code for the FormView...
<asp:FormView runat="server" ID="FormView1" DataSourceID="DetailsDataSource" DefaultMode="Insert"
OnItemCommand="FormView1_ItemCommand" OnItemInserted="FormView1_ItemInserted" RenderOuterTable="false">
<InsertItemTemplate>
<table id="detailsTable" class="DDDetailsTable" cellpadding="6">
<asp:DynamicEntity runat="server" Mode="Insert" />
<tr class="td">
<td colspan="2">
<asp:LinkButton runat="server" CommandName="Insert" Text="Insert" />
<asp:LinkButton runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FormView>
When this form is displayed in a browser, there are 5 user input controls, and two buttons (insert/cancel). But here in the code, I do not see any reference to the 5 user input controls, unless they are represented by this line of code...
<asp:DynamicEntity runat="server" Mode="Insert" />
First, am I correct in assuming that this line of code builds the user input controls dynamically?
And, my actual problem -- I need to set the Focus to the first of these input's upon page initialization. Basically when someone chooses to insert a new item and is sent to this page, I want them to be able to start typing right away, without the need to actually click in that textbox to begin. Any help would be greatly appreciated?
I was able to resolve my issue by adding a Search field via Code, and then using this in the Page_Load event...
If table.DisplayName = "Employees" Then
MultiSearchFieldSet.Visible = True
txbMultiColumnSearch.Focus()

ModalPopupExtender and dropdownlist

I have the following code on my page:
<asp:ModalPopupExtender ID="mpLabelCheck" PopupControlID="pnlModal" TargetControlID="lstCategory"
OkControlID="btnOK" runat="server" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
<asp:Panel id="pnlModal" CssClass="modalPopup" runat="server">
<p>Please make sure all data is entered before continuing.</p>
<p><asp:Button ID="btnOK" Text="OK" runat="server" /></p>
</asp:Panel>
The target control ID is a dropdownList. What happens is any interaction with the dropdownlist triggers the popup. I'd like to wait until the user makes a selection, then conditionally show the popup based on the result of another method.
Any thoughts on how I can accomplish this?
Use dummy hidden control as target for extender and show it in server code with Show method call

Checkbox inside user control in repeater doesn't fire

I have user control inside a page with single repeater. The problem is when this control is placed inside repeater check box doesn't fire CheckedChanged event. If this would be a dynamically added control I would check whether CheckedCanged event has a handle assigned in Init() but here the event is specified in ascx so I imagine asp.net is responsible for doing all the assignments in right place. I'm lost here :/
By the way. Placing single checkbox inside repeater doesn't fire to. But I can at least use OnItemCommand there
User Control
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="QuoteContainer.ascx.cs" Inherits="EhdSite.Controls.QuoteContainer" %>
<asp:TableRow runat="server" ID="trQuoteRow">
<asp:CheckBox runat="server" ID="cbxConfirm" AutoPostBack="true" OnCheckedChanged="CbxConfirmCheckedChanged" />
</asp:TableRow>
Page
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager2" runat="server" />
<asp:UpdatePanel runat="server" ID="upQuotes" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Repeater runat="server" ID="rptQuotes">
<ItemTemplate>
<ehd:QuoteContainer
runat="server"
ID="qcQuote"
RowCssClass="rowStyle"
HighlightedRowCssClass="selectedRowStyle"
Quote="<%# Container.DataItem %>"
OnQuoteSelectedChanged="QuoteSelectedChanged" />
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
It has to do with the event-chain in ASP.Net, this MSDN page helps to understand it.
The .Net framework resolves the ViewState between the Init- and the Load-event. So if the control is not there after the Init-event, it's ViewState will never be loaded and also it's events will never be fired.
This can get quite tricky at times, especially if one dynamically loaded control should affect the loading of another one...

Resources