trigger controlID from user control page - asp.net

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()

Related

Script controls may not be registered before PreRender. when using timer control

I'm a newbie to ASP.net (vb), and i'm working with the timer controls. I've once again researched the answer to this and done as said (overloading prerender method). My problem is I have a master page which i've added a timer control, in order to update a count down field, on going to the sites index page i keep getting the error : "Script controls may not be registered before PreRender." The timer works well on other pages except the index page.
<form id="fMain" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer></div>
<div>
<asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Literal ID="litTimer" runat="server"></asp:Literal>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
The Codebehind:
Sub timer1_tick() Handles timer1.Tick
ltr1.text = "test"
End Sub`
Thank you in advance.
This is because you are using Telerik controls and have overridden the OnPreRender method.
You need to add the following to this method:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

How to customize calendar selection in 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.

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.

UserControl causes full postback in UpdatePanel

I'm fighting this problem for few hours now and I'm not getting any closer to solution.
The thing is: I'm having few UpdatePanels on my master page. And then, I have RadTreeViewcontrol on the page, that should cause partial postback each time node is clicked. Everything works fine there.
Since I'm using the same tree on some other pages, I moved this functionality to UserControl. Stuff I've done before, so no problem. Moved some code to ascx, created some events. Everything always worked for me well. But not now.
RadTreeView is nested inside UserControl, and this control on master page with update panels, see below:
<asp:Panel ID="pnlContentWrapper" runat="server" CssClass="ContentWrapper">
<div id="HeaderBreadCrumb">
<asp:ContentPlaceHolder ID="HeaderBreadCrumbContent" runat="server" />
</div>
<div id="HeaderMenu">
<asp:UpdatePanel ID="upnlTreeMenu" runat="server">
<ContentTemplate>
<asp:ContentPlaceHolder ID="HeaderMenuContent" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="treeProductTree" />
</Triggers>
</asp:UpdatePanel>
</div>
<div id="TreeDiv">
<fp:ProductTree ID="treeProductTree" runat="server" />
</div>
<asp:Panel ID="ContentDiv" ClientIDMode="Static" runat="server">
<asp:UpdatePanel ID="upnlTreeContent" runat="server">
<ContentTemplate>
<asp:ContentPlaceHolder ID="TreePageContent" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="treeProductTree" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
</asp:Panel>
And the user control is really simple:
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="ProductTree.ascx.vb"
Inherits="ProductTree" %>
<div>
<telerik:RadTreeView ID="treeProductTree" ClientIDMode="Static" runat="server" EnableDragAndDrop="false"
SkinID="CustomSkin" CssClass="MasterProductTree" DataFieldID="NodeId" DataFieldParentID="NodeParentId"
DataTextField="NodeName" DataValueField="NodeId" />
</div>
And some code behind:
Imports Telerik.Web.UI
Public Class ProductTree
Inherits System.Web.UI.UserControl
Public Event NodeExpand As EventHandler(Of ProductTreeNodeExpandEventArgs)
Public Event SelectedNodeChange As EventHandler
Protected Sub ProductTree_NodeExpand(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs) _
Handles treeProductTree.NodeExpand
Dim nodeId As Integer = CInt(e.Node.Value)
Dim evetArgs = New ProductTreeNodeExpandEventArgs(nodeId)
RaiseEvent NodeExpand(Me, evetArgs)
//'some logic
End Sub
Protected Sub ProductTree_OnNodeClick(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs) _
Handles treeProductTree.NodeClick
RaiseEvent SelectedNodeChange(Me, New System.EventArgs())
End Sub
End Class
What I don't know is: why is this causing full postback instead of partial? I suspect that it may have something to do with raising my for SelectedNodeChange, but I don't know how to deal with it other way. I need to let other components know, that node selection changed. How can I improve this to make it work with UpdatePanels?
Suspect that registering a UserControl as an AsyncPostbackTrigger is not going to get you very far. The UC itself is not a postback control, rather, it contains one. I would expose the tree control as a public property of the usercontrol, and then in code behind in your containing page, register the tree control itself as the trigger, rather than the UserControl.
Check this for a similar situation... it's not a great Q&A thread but it shows the basic gist.
Well, I have the similar situation but it's a little more complicated.
User control causing postback is actually added dynamically depending on the request parameter value.
I was so glad the ClietIDMode paramter was added, I didn't think a problem like tat would be overlooked by Microsoft.
The same problem that I am facing right now... I thing that could be fixed by wrapping up the UC with an iFrame.

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