Manually trigger Update Panel - c#

I have an UpdatePanel in the page.
Inside this UpdatePanel, I have a control which has a button.
Is it possible to Manually add a postback trigger for this button (which is inside the user control) in the code behind?
Here's the sample code
//Page
<UpdatePanel>
<uc1:UserControl />
</UpdatePanel>
//UserControl
<textbox></textbox>
<asp:button></asp:Button>

As I understand, you want to register the button in your user control as a PostbackTrigger of an UpdatePanel in your ASPX page which contains your user control.
I have managed to write a simple web application that replicates this scenario and without further information I cannot see where you're going wrong or what the error that you're getting is, if any. It would help if you expanded on your original question e.g. by providing relevant snippets of your code. I'll try my best...
From within the user control you can register the button as a post back trigger of the update panel with the following code:
public void RegisterPostbackTrigger(UpdatePanel updatePanel)
{
PostBackTrigger buttonTrigger = new PostBackTrigger();
buttonTrigger.ControlID = TestButton.ClientID;
updatePanel.Triggers.Add(buttonTrigger);
}
You will then have to call the RegisterPostbackTrigger from within you page's code behind passing in you UpdatePanel's reference as an argument. MyControl refers to the UserControl
MyControl.RegisterPostbackTrigger(MainUpdatePanel);
An alternative way of doing this, which may be relevant to your particular issue is to access the ScriptManager directly and use its RegisterPostBackControl method passing in your button as the parameter. Here's the a link!
Can't really help you anymore without more information!
Good luck!

Related

HTML button onserverclick event doesn't work

I'm trying to use the onclickserver on ASP.net to do something when the user clicks on a button but when I click the button nothing happens.
I really can't see what I'm doing wrong.
Here is the button:
<button id="BttnLead" class="bttnBlck" runat="server" onserverclick="BtnLead_OnClick">Lead</button>
And here the event I'm trying to use:
protected void BtnLead_OnClick(object sender, EventArgs e){}
Thank you.
I would bet it has to do with failed validation somewhere on the page, as #kman pointed out in a comment above. Are you using any <asp:FieldValidator/> controls anywhere on the page? If so, and they're not being handled correctly, all you would need to do to cause this button's postback to be triggered is to add the CausesValidation="false" property to the <button> control. This would have the button avoid the validation code that is (if it's the issue, which I really think it is) inevitably failing and thus never reaching the handler method.
P.S. It should be noted that you do NOT have to use an ASP.NET control (i.e. <asp:Button>) and the pure HTML <button> control with the runat="server" property renders a server side control just the same. However, if you're in the ASP.NET world anyway and you have access to these controls, they do provide some benefit that is nice; but that's a different conversation.

Remove statically added controls at runtime

The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?

AJAX Partial page update problem when displaying a control

I have an interesting problem when using partial page update in asp.net with scriptmanager and a update panel.
My scenario looks like this: I'm using the tab control from the ajax toolkit. I also implemented this control using lazy loading, so that when the page is loaded only the current tab gets loaded all the other tabs don't get rendered, because Im using an UpdatePanel (on a .ascx control) on each of these tabs and when a tab gets selected the updatepanel makes a async postback to load the content for a selected tab.
On one of my tabs Im using a combobox control from obout.com, and it doesn't work.
Now I know why it doesn't work. It doesn't work because the control is shown via a partial page refresh, but to correctly display the control it has to do some "magic" that is - register some .css and .js includes on the page (in the head I guess)....but because I load this control via async page refresh...it can't do these stuff.
What kind of workarround do you suggest?
Thanks!
Assuming you have done partial ajax updates in asp.net before, then you simply need to register a client block with the ScriptManager for when the partial update is triggered.
Without knowing your problem space, it's difficult to paste specific code for you, so instead I'll just suggest you read this and then ask more questions if you have them...
http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerclientscriptblock.aspx
BTW, if you're dynamically creating the controls as well as partially updating them, then you will also need to be very careful of your viewstate. This is a can of worms, so hopefully you are not and won't have to worry about it.
As I suspected you have to manually register the needed scripts for ajax controls to work in such a manner.
For example here is the solution for telerik controls:
http://www.telerik.com/help/aspnet-ajax/troubleshooting.html
Here is a solution using devexpress controls:
protected void Page_Load(object sender, EventArgs e)
{
DevExpress.Web.ASPxClasses.ASPxWebControl.RegisterBaseScript(this);
}
I didn't found a solution for obout.com controls.

Masterpage + updatepanel on child page

So I have a masterpage with a login that is in an update panel. I have a child page that has a literal control that should update when the login updates. What it doesn't do is reload the method I use to generate the content for that literal when it posts back. I tried to call the method on the child page from the master page once you click log in, but I get an error that the literal control cannot be found (because it exists on the child page not the master page). How would I reference that control in the masterpage to pass it to my method?
The article below shows how the control tree works with MasterPages and how to reference different controls at different levels of the control tree.
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
So the scenario is that you have an update panel on the child page that when triggered doesn't update/refresh your lets say, label which is in your header on your master page.
What you do is, in the code behind of your master page create a function that changes the value of the label.
Include an update panel on the master page for the label, which is triggered by the textchanged event etc.
Now, in your child page code behind or your let say, button click event, call upon the function that exists in the master page and send the needed value in the parenthesis.
C#:
((MyMaster)this.Page.Master).ShowMessage(text);
VB.NET:
DirectCast(Me.Page.Master, MyMaster).ShowMessage(text)
This should update the label with the correct value whilst triggering the update panel on the master page and thus refreshing your label as well.
I'm about to try this now for myself, wish me luck. :D

How can I pass data from an aspx page to an ascx modal popup?

I'm fairly new to ASP.NET and trying to learn how things are done. I come from a C# background so the code-behind portion is easy, but thinking like a web developer is unfamiliar.
I have an aspx page that contains a grid of checkboxes. I have a button that is coded via a Button_Click event to collect a list of which rows are checked and create a session variable out of that list. The same button is referenced (via TargetControlID) by my ascx page's ModalPopupExtender which controls the panel on the ascx page.
When the button is clicked, the modal popup opens but the Button_Click event is never fired, so the modal doesn't get its session data.
Since the two pages are separate, I can't call the ModalPopupExtender from the aspx.cs code, I can't reach the list of checkboxes from the ascx.cs code, and I don't see a way to populate my session variable and then programmatically activate some other hidden button or control which will then open my modal popup.
Any thoughts?
All a usercontrol(.ascx) file is is a set of controls that you have grouped together to provide some reusable functionality. The controls defined in it are still added to the page's control collection (.aspx) durring the page lifecylce. The ModalPopupExtender uses javascript and dhtml to show and hide the controls in the usercontrol client-side. What you are seeing is that the click event is being handled client-side by the ModalPoupExtender and it is canceling the post-back to the server. This is the default behavior by design. You certainly can access the page's control collection from the code-behind of your usercontrol though because it is all part of the same control tree. Just use the FindControl(xxx) method of any control to search for the child of it you need.
After some research following DancesWithBamboo's answer, I figured out how to make it work.
An example reference to my ascx page within my aspx page:
<uc1:ChildPage ID="MyModalPage" runat="server" />
The aspx code-behind to grab and open the ModalPopupExtender (named modalPopup) would look like this:
AjaxControlToolkit.ModalPopupExtender mpe =
(AjaxControlToolkit.ModalPopupExtender)
MyModalPage.FindControl("modalPopup");
mpe.Show();
Sorry, but I'm confused. You can't call an ascx directly, so...
Is your modal code that you are calling from within the same page, like a hidden panel, etc;
Or is it another aspx page that you are calling on a click event?

Categories