How to customize calendar selection in ASP.NET - asp.net

i'm trying to come up with a DateTime Picker in ASP.Net using textBox, Calendar, Button and Updatefields.
if a user clicks a button the panel with a hidden calendar will become visible, and if a user selects a date calendar's visibility will become false and he date will be assigned to textBox.
My problem comes if a user wants to change month because in this case calendar will disappear because selectionChange is being triggered.
My question is how can I fish out the changing of the month and bound the hiding of the panel only if a date is selected?
thank you very much in advance.
here are the code samples:
front part:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" />
<asp:ImageButton ID="Imbut" runat="server" ImageUrl="imagesNew/calender.png" Height="17px" Width="17px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Calendar1" EventName="SelectionChanged" />
</Triggers>
</asp:UpdatePanel>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:Calendar ID="Calendar1" runat="server" Height="129px" Width="209px">
</asp:Calendar>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Imbut" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
back part
Protected Sub Imbut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Imbut.Click
Panel1.Visible = True
End Sub
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
TextBox2.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy")
Panel1.Visible = False
End Sub

I struggled with a similar problem. The problem is (as you stated) on the postback the control gets reset. I never found a means to do this elegantly with asp.net controls. I was able to accomplish this using the jQuery calendar and asp:hidden field. Keeping it client side via javascript I was able to allow the user to navigate the calendar change dates etc. When a date was selected the values were set to both a textbox within the ui as well as a hidden field then on submission, the value within the textbox or hidden field was picked up server side and used (in my situation) as a filter parameter.

Related

update panel webcontrol required form field

I created a simple control which uses an update panel with a button click trigger and yes a script manager above the control. Click the button and a label is updated with the current time and this has worked fine for 6 years
Today, I changed the page to be responsive with Bootstrap but this is irrelevant to this question.
The control was added to a page which has labels and textboxes (simple form with first name / last name).
<asp:TextBox ID="txtFirstName" runat="server" CssClass="form-control input-lg" Placeholder="Your First Name" MaxLength="20" Required="true"></asp:TextBox>
If I remove:
Required="true"
and click the button in the control the date / time updates but placing this back stops the date / time updating. I need to use both as I wish for the first name to be required but also for the time to update
Simple Example:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" runat="server" Text=""></asp:Label>
<asp:Button ID="btnRefreshTime" runat="server" Text="Refresh Time" OnClick="btnRefreshTime_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRefreshTime" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Code Behind:
Protected Sub btnRefreshTime_Click(sender As Object, e As EventArgs)
lblTime.Text = DateTime.Now.ToString()
End Sub
What am I doing wrong and what would I need to do to resolve it without opening the form up to abuse (script attacks) etc? I don't wish to add the following to the page directive:
ValidateRequest="false"
Thank you in advance
Probabily the button server side event is prevented to execute due to the client side validation of required = "true".
Try to change the button with somewhat else which does not trigger client side validation by design. I would try with a LinkButton.

Ajax Update Panel - What am I missing?

Problem: When suburb drop down list value is changed - Page is posting back.
Desired result: Changing value in drop down list updates post code text box value without a page post back (post code text box is normally hidden)
Page code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:DropDownList ID="Suburb" runat="server" DataTextField="LocalityPhrase"
DataValueField="Locality" AutoPostBack="true" class="DropDown" OnSelectedIndexChanged="Suburb_SelectedIndexChanged"/>
<asp:UpdatePanel runat="server" id="UpdatePanelPostCode" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="Postcode" runat="server" Visible="true"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Suburb" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Code behind:
Protected Sub Suburb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Suburb.SelectedIndexChanged
'get postcode from suburb drop down
Dim pCode As String = ""
If Suburb.SelectedValue <> "" Then
pCode = Suburb.SelectedItem.Text.Substring(Len(Suburb.SelectedItem.Text) - 5, 4)
End If
Postcode.Text = pCode
End Sub
It appears there was something wrong with my install of the AJAX toolkit. Removed and reloaded it in Visual Studio (2008) and all is well.

trigger controlID from user control page

I have the code below which looks like it should work. However, the trigger that references the control with the controlID of "gvSearch" is actually a gridview inside a user control page.
How can I access that gridview so that I can use it as a trigger?
Thanks!
<asp:UpdatePanel ID="pnlSearch" ChildrenAsTriggers="true" runat="server" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSearch" />
<asp:AsyncPostBackTrigger ControlID="gvSearch" />
</Triggers>
</asp:UpdatePanel>
Thanks!
Outline:
<%# Page Title="test">
<%# Register src="test1.ascx" tagname="test1" tagprefix="test1uc" %>
<UpdatePanel>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSearch" />
<asp:AsyncPostBackTrigger ControlID="gvSearch" />
</Triggers>
<ContentTemplate>
<test1uc:test1 ID="test1a" runat="server" />
</ContentTemplate>
</UpdatePanel>
What event do you want to use as trigger, the SelectedIndexChanged-event? Then apply the appropriate EventName.
<asp:AsyncPostBackTrigger ControlID="gvSearch" EventName="SelectedIndexChanged" />
However, that should already be the default event for GridView (see the remarks section).
Have a look at this thread: http://forums.asp.net/t/1136932.aspx
Update I think the best approach in your situation is to provide a custom event in your UserControl that is bubbled from the SelectedIndexChanged event from the GridView in the UC. Then you can use this event as AsyncPostBackTrigger for your UpdatePanel, e.g.:
<asp:AsyncPostBackTrigger ControlID="test1a" EventName="GridSearchClicked" />
Setting aside the fact that requiring access to a subcontrol is not good practice, here's my best suggestion...
Create a Public Sub in your user control as follows:
Private ParentUpdatePanel As System.Web.UI.UpdatePanel
' Must be called on every Page_Load!
Public Sub RegisterAsyncTrigger(MyScriptManager As System.Web.UI.ScriptManager, MyUpdatePanel As System.Web.UI.UpdatePanel)
MyScriptManager.RegisterAsyncPostBackControl(gvSearch)
ParentUpdatePanel = MyUpdatePanel
End Sub
On the Page_Load event, you'd call the function as follows:
Protected Sub Page_Load(Sender As Object, e As EventArgs)
' Call our new function, passing in the current ScriptManager and the UpdatePanel
' The ScriptManager handles the asynchronous postbacks
' The UpdatePanel handles the dynamic updates
test1.RegisterAsyncTrigger(ScriptManager.GetCurrent(Me), pnlSearch)
End Sub
In the event handler inside the gvSearch GridView control which must update the panel would contain the following code:
ParentUpdatePanel.Update()

ASP.NET Ajax Text Box with Button not firing

Hey just wondering if my syntax is wrong here
<asp:UpdatePanel ID="UpdatePanel2"
runat="server"
UpdateMode="Always">
<ContentTemplate>
<asp:textbox id="searchProductName" runat="server"></asp:textBox> <asp:Button ID="btnProductSearch" runat="server" Text="Search Product Name" CssClass="search" OnClick="ProductSearch_Click" UseSubmitBehavior="true" CausesValidation="false" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnProductSearch" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
and my OnClick method
Protected Sub ProductSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
' Filter by ProductName
If searchProductName.Text.Length > 0 Then
srcProductListPerCustomer.FilterExpression = " (productName like '%" + searchProductName.Text.ToString & "%')"
productListTable.DataBind()
Else
srcProductListPerCustomer.FilterExpression = ""
productListTable.DataBind()
End If
End Sub
The problem is nothing is happening when I click on the button. The button works fine without the Ajax
Your button doesn't need to be in an UpdatePanel. The controls in a UpdatePanel should be the controls that are to be updated asynchronously. Put your UpdatePanel around the GridView you're updating, and use the AsyncPostBackTrigger in the same way.
It's best to keep UdpatePanels as small as possible; the fewer controls inside them, the less HTML will be sent back from the server (less bandwidth, faster request/response time). PostBackTriggers can refer to controls outside of the UpdatePanel with no problems.

Update Parent UpdatePanel from Child UpdatePanel - Conditionally

I am trying to trying to setup an updatepanel to update every X seconds, the problem is I don't want the control to actually refresh unless there is new data. So I currently have a child updatepanel in a parent UpdatePanel, the child updatepanel gets refreshed by a timer. But I can't seem to find a way to trigger the parent panel to update. Again, only when certain conditions(data changed) are met.
Sample Code:
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000"></asp:Timer>
<asp:updatepanel id="upParent" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label id="lblParenttime" Runat="server">Parent Time Here</asp:Label>
<asp:updatepanel id="upChild" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label id="lblChildtime" Runat="server">Child Time Here</asp:Label>
</ContentTemplate>
<triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</triggers>
</asp:updatepanel>
</ContentTemplate>
</asp:updatepanel>
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
lblChildtime.Text = Now.Tostring
if X = Y then
'Cause Parent to Update
lblParenttime.Text = Now.Tostring
end if
End Sub
You can trigger some javascript when you UpdatePanel refreshes with the following javascript code:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(YourJavascriptFunctionNameHere);
You could then put a hidden button on the other UpdatePanel and manually call the __DoPostBack from the javascript function you have hooked onto the UpdatePanel reload.
There is probably other ideas for how to leverage add_pageLoaded in your scenario but this should at least get you on the right track.
Have you tried upParent.Update() in the Timer1_Tick event when conditions are met ?

Resources