How can I update an UpdatePanel in a control from the page? - asp.net

I have a modal on a page that also contains a user control.
When clicking the 'OK' button in the modal, I would like to update an UpdatePanel that is contained within the user control on the page.
Currently the 'OK' button on the modal does a full page postback, I'd like to just update the panel, but I'm not sure how to add it as a trigger since it's not in the control the UpdatePanel is.
Thanks.
I have a partial answer... I can update the panel once by doing this:
Dim addTrigger As New AsyncPostBackTrigger
addTrigger.ControlID = MyButton.ID
addTrigger.EventName = "Click"
MyUserControl.GetUpdatePanel.Triggers.Add(addTrigger)
This will cause a partial post-back the first time, but after that first time it causes an entire page reload. Any ideas?

You can explicitly add the OK button as a AsyncPostBackTrigger for the UpdatePanel.
In the aspx markup for the UpdatePanel:
<asp:UpdatePanel ID="updPanel" runat="server">
<ContentTemplate>
....
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="the control" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
or in the code-behind:
protected override void OnInit(EventArgs e)
{
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = "the Control";
trigger.EventName = "Click";
updPanel.Triggers.Add(trigger);
base.OnInit(e);
}

Maybe I am missing some subtlety here, but can't you just call the Update() method on the Update Panel?

You can use the following pattern to control a panel postback in JavaScript:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm._panelsToRefreshIDs = UPComments.uniqueID;
prm._doPostBack(eventTarget,eventArgument);
theForm.submit();

Can't you just add an AsyncPostBackTrigger programmatically from the code-behind?
Like This ?

just put the contents of the Panel in an update panel sothat you have:
Your user control here.
this will also prevent the popup from disapearing until you want it to.
In your code behind you can then call it with popup.show(0 or pop.hide() and link these to your user control by addin gan event ot it and handling it inyour pages code behind. this way your user control can direct the popup what to do and when. you can even for it to update hte update panel if you needed for some reason.
for the case above tha tI just realized is tha tyou wan to updat ehte panel in the control. Create a method i nthe control that calls the update method o teh update panel an dthen fire that event from your page. It's the same principal in reverse. By utilizing event well you can wire up your application to do some pretty cool stuff.

Related

Issue with getting control id when tabcontainer is used

I have an AJAX Control Toolkit TabContainer in my ASP.NET page. In one of the tabs in the TabContainer, there is a GridView. Now, I have an UpdatePanel for which I want to give trigger as "RowCommand" of GridView mentioned above. The UpdatePanel is outside the TabContainer. But when I give the GridView id, I am getting the error:
A control with ID 'grvSummary' could not be found for the trigger in UpdatePanel 'updSegment'.
Trigger markup:
<asp:AsyncPostBackTrigger ControlID="grvSummary" EventName="RowCommand" />
You will have to programmatically add the trigger to your UpdatePanel. This is because your GridView may or may not start in a different ContentPlaceHolder than the UpdatePanel (namely, the <ContentTemplate> of your TabContainer). Like this (note that this really needs to be done in Page_Init, due to the Page Life Cycle):
protected void Page_Init(object sender, EventArgs e)
{
AsyncPostBackTrigger trig = new AsyncPostBackTrigger();
trig.ControlID = grvSummary.UniqueID;
trig.EventName = "RowCommand";
updSegment.Triggers.Add(trig);
}
It looks like this may be a problem with ASP.NET / AJAX assuming the wrong Control.UniqueID value for the Control being used as a trigger.
Source: Triggering an UpdatePanel in a different ContentPlaceHolder

ASPxGridView PerformCallback() does full page postback

I have a checkbox that when clicked, calls a javascript program that calls grid.PerformCallback(), where grid is the client instance name of my ASPxGridView gridview. This gridview also has a custom callback method which databinds the table. However when i click my checkbox, instead of only performing callback on the gridview, my page does a full postback, which posts the form. How do i make it so that it only updates the gridview?
function toggle()
{
productGrid.PerformCallback();
}//end toggleExch()
<dx:ASPxGridView ClientInstanceName="productGrid" Width="100%" ID="productGrid" runat="server"
DataSourceID="ProductSQL" EnableCallBacks="true" OnCustomCallback="productGrid_OnCustomCallback">
</dx:ASPxGridView>
protected void productGrid_OnCustomCallback(object sender,
DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
{
System.Diagnostics.Debug.WriteLine("in postback");
productGrid.DataBind();
}//end productGrid_OnCustomCallback()
so basically the debug line is not printed and the page goes into full postback - how do i only postback and databind the grid? (i need to do more server side processing before databinding or directly binding from jquery is out of the question)
found the answer - should use iscallback instead of ispostback
Unfortunately, you did not post the aspx markup of the button. However, if this is the ASPxButton, make certain that its AutoPostback property is false ...

Add user control after page is loaded

I am trying to add dynamically a user control I created to an aspx page after it is loaded.
I tried sending an ajax request to a handler that used RenderControl function and sent back the control's html. I appended it to the DOM using javascript.
The problem is that some of the controls must go through their Page_Load function and that doesn't happen when using RenderControl function.
Does anyone have any idea how can I do that?
try this:
System.Web.UI.Page page = new System.Web.UI.Page();
CustomUserControl userControl = page.LoadControl("~/control.ascx") as CustomUserControl;
if (userControl == null)
{
//error
}
userControl.ArticleId = id;
//call any other properties or methods you need
page.Controls.Add(userControl);
System.IO.StringWriter sw = new System.IO.StringWriter();
HttpContext.Current.Server.Execute(page, sw, false);
responseString = sw.ToString();
hope this will help!
Why dont you load and add the user control dynamically to the page?
I don't think you can do this that way.
Your controls don't go through their Page_Load, because they are not added to any control. All you are doing is rendering their html (using RenderControl) and loading it using javascript. There's no place for Page_Load event.
I would use UpdatePanel instead in that way:
<asp:UpdatePanel>
<ContentTemplate>
<asp:Panel ID="pnlDynamic" />
</ContentTemplate>
</asp:UpdatePanel>
and then in code behind add your dynamic controls to panel
pnlDynamic.Controls.Add(new YourControl());

How do I target a GridViewRow with an AJAX.Net Toolkit PopupExtender?

I have a lot of data to display in a GridView. Because there's so much information per row, I'd like to be able to display additional information when a user clicks on the row, so I thought a PopupExtender from the AJAX Toolkit would be perfect.
Ideally, I want the popup to display whenever any of the controls within the row are selected. I've been able to successfully attach the PopupExtender to a single control within the row, but I can't get the pop-up to attach to the row itself.
I would have thought that setting the PopupExtender's TargetControlId to the Row's ClientID within the RowDataBound event would work, but when I do this I get a runtime error:
TargetControlID of 'popupExtId' is not valid.
A control with ID 'gvList_ctl02' could not be found.
I noticed that the GridViewRow is rendered, the tr element does not include an id, so I also tried extending the GridView control to override the CreateRow method to render the id - using this method I was able to render the row's ID (e.g. gvList_ctl02), but the same runtime error was thrown when I added the PopupExtender back into the code.
I also tried binding the showPopup() javascript command to the row's onclick event to get the popup to display manually; whilst the click event is registered OK and is definitely triggered, the popup is still not shown.
Does anyone have any idea how to / if you can bind a PopupExtender to a GridViewRow?
My row bound code is as follows:
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Bind the popup extender's target ID to the row ID
// This will cause a runtime error
PopupControlExtender pop = e.Row.FindControl("popupExtId") as PopupControlExtender;
pop.TargetControlID = e.Row.ClientID;
// Also bind the client side click handler to try to get the popup to show
// The alert is triggered and no javascript error is generated, but the popup does not display
e.Row.Attributes.Add("onclick", "alert('Row Clicked'); $find('" + pop.BehaviorID + "').showPopup();");
}
}
Many thanks.
If you're not opposed to using an ajax ModalPopupExtender, I use a little bit of javascript and some sneaky hidden button clicks to fire off my modal popups from within a grid view. I usually make my modal popup extender's target control id my hidden button, then, via javascript, fire my hidden button's click event to show the modal popup.
Here's my modal popup and hidden button markup.
<asp:Button ID="hiddenButton" runat="server" Text="" style="display:none"></asp:Button>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender2" runat="server"
TargetControlID="hiddenButton" PopupControlID="Panel1" CancelControlID="CancelButton"
BackgroundCssClass="modalBackground" Drag="True"/>
Here's my javascript to show my popup.
function showModal(btnID) {
btn = document.getElementById(btnID);
btn.click();
}
In my rowdatabound event, I call the javascript function showModal from button's onclick event.
Button myButton = (Button)e.Row.Cells[9].Controls[1];
matchButton.Attributes.Add("onclick", "showModal('" + hiddenButton.ClientID + "');");
Hope this might help point you in the right direction.

control with ID 'GridView1' could not be found for the trigger in UpdatePanel 'UpdatePanel1'

I have two gridview in update panel and m adding entries from one gridview to another on selectedIndexChanged event what m trying to do is updating update panel on this event selectedindexchanged ...but my gridview is inside accordian control so it does not get initialized and hence i get this error .....
control with ID 'GridView1' could not be found for the trigger in UpdatePanel 'UpdatePanel1'
Anybody knows solution?
I haven't had a chance to test this yet, but this might be what you're looking for. It's possible that you need to dynamically add the trigger at Page_Init. Like this:
protected void Page_Init()
{
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.EventName = "SelectedIndexChanged";
trigger.ControlID = GridView1.UniqueID.ToString();
UpdatePanel1.Triggers.Add(trigger);
}
Try adding that to your code-behind contemporary to Page_Load() and removing the trigger from your mark-up.

Resources