I have a case that I'm using an update panel that when updated it will load a user control containing a child update panel, and its code is as follows:
<asp:UpdatePanel runat="server" ID="Parent" UpdateMode="Conditional" ChildrenAsTriggers="false" >
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="ChildUPNL"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
In the code behind I got an object then use the Update method to refresh the parent update panel
Then It' should have my user control filled with data
in the user control I have an update panel also like:
<asp:UpdatePanel runat="server" ID="Child" OnLoad="ChildLoad">
<ContentTemplate>
<asp:LinkButton Text="Link button" runat="server" />
<asp:Button Text="Button" runat="server" OnClick="ButtonClick"/>
</ContentTemplate>
</asp:UpdatePanel>
When I click on the link button the full page reloaded even the parent and my user control disappered as it not in the page direct
and if I click on the normal button nothing done !
didn't go to the update panel on load event nor the button click event !
Any idea why this action done !
I need when click on button to change some values or save values to database so I need to go throw the server side code without post back the page
You have to load every time usercontrol on page_init or page_load. You can use MultiView to achieve your goal.
You can also use Events and delegate and call custom event and bind parent control after click on child panel button.
**//On Page Behind Code**
protected void Page_Load(object sender, EventArgs e)
{
UserControl11.EventCallfromOtherUserControl += new UserControl1.CallfromOtherUserControl(CallEvent);
}
public void CallEvent()
{
//Load Parent Control
}
**//On User Control Behind Code**
public delegate void CallfromOtherUserControl();
public event CallfromOtherUserControl EventCallfromOtherUserControl;
protected void Button1_Click(object sender, EventArgs e)
{
if (EventCallfromOtherUserControl != null)
{
EventCallfromOtherUserControl();
}
}
Related
I need to have UpdatePanel with asyncpostback, but in my case it seems that no partial postback happens, but fullpostback. I am new to web forms, please, check the code:
<%# Register TagPrefix="Cust" TagName="CompanyInformationView" Src="~/CustomControls/CompanyInformationView.ascx" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick= "Button1_Click" Text="test" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<Cust:CompanyInformationView ID="CompanyInformationView" runat="server" />
</asp:Content>
So I have Test Button. OnClick it should do "nothing" for test. Also there is custom control on web form. Here is server side code for this form:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// fill in custom control
CompanyInfo c = GetInfo();
CompanyInformationView.Company = c;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
var i = 1;
}
CompanyInformationView is custom control with property "Company". There is no ViewState added for this property (so it cannot be loaded properly if postback is done). When I click on Test Button, the page fails, because "CompanyInformationView.Company" is not set (it is not set, because it cannot be loaded from ViewState, I guess).
Instead, I think that it should not work like this. AsynPostback should deal only with UpdatePanel.
Why it wants to reload custom control? Doesn't it mean that Full postback happen or maybe I do not understand Asyncpostback?
PageLoad and all other events are raised on every get\postback, no matter if it's async or full,
In asyncpostback, the response which the server renders includes only the content inside the updatepanel.
Try to Put <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
Before
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional">
Async post back in web forms still proceeds the whole page life cycle as if it's a traditional post. the difference is only the updated content inside target update panel that will be sent in response. so in this case, async post back will still give you exception unless you populate the custom control no matter get/post.
you need to also put the custom control inside another update panel or in the same update panel as the button in order see partial update to happen.
I am trying to change the text on a label from a custom event. When I update the text through a button click event (Button1_Click), everything works. However, when I try to update the same label in a custom event(ArReceiver_OnArDataUpdate) nothing happens. ArReceiver_OnArDataUpdate is getting triggered properly.
Why does my event react differently than a button click event? What do I need to do to get my event working?
This is an out of the box web project in VS2013. Here's the code to explain better:
Code Behind:
namespace WinReceiver
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArReceiver.OnArDataUpdate += ArReceiver_OnArDataUpdate;
}
void ArReceiver_OnArDataUpdate(object sender, ArEventArgs e)
{
labelVoltage.Text = "Reset";
UpdatePanel1.Update();
}
protected void Button1_Click(object sender, EventArgs e)
{
labelVoltage.Text = "Button Clicked";
UpdatePanel1.Update();
}
}
}
Default.aspx: (The site.master has the ScriptManager in it by default)
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelVoltage" runat="server" Text="..."></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
EDIT:
I did find this question that says it's not possible, but can someone explain why this may not be possible? I'm ok with a page refresh too, but when I tried Respone.Redirect I got exceptions.
If OnArDataUpdate is a static event, it's likely to be fired when the ASP.NET page rendering process is out of scope, and thus the label is not existent because it isn't in scope of the page being processed (since ASP.NET is stateless).
However, if ArReceiver is a control on the page, it should process fine, and the issue could be within the control or something overriding it (depending on when the event fires, it may get overridden by viewstate).
I have three tabs like this:
When I press on the last table, there is a code behind executing to get data from database. I have done all of that.
my problem
in the last tab there is a button, when I press it. it loads the whole page and then returns to the first tab open.
what I want
when I pressing that button in the last tab, I want to get the data and present it, whit out making the page goes to the first tab.
i am sure it is something about post back but i don't know anything about it
Edit
this is my code in c#
protected void BookingForDate_Click(object sender, EventArgs e)
{
//do something in database and fill the table
}
Edit2
protected void Page_Load(object sender, EventArgs e)
{
// go to database and get data to fill it in the first tab
}
Here is a post that should get you started with update panels
http://msdn.microsoft.com/en-us/library/bb386454.aspx
Here is the minimum you need on the page with it writing out the datetime. so you can see it works
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<!-- Other content in the panel. -->
<%=DateTime.Now.ToString() %><br />
<asp:Button ID="Button1" Text="Refresh Panel" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
I have an ASP.NET listbox, lstActivities. To edit an item in the list, users can either click lnkButton or double-click on the listbox. I achieve this with:
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) return;
var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
lstActivities.Attributes.Add("ondblclick", refDblClick);
}
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(lnkButton.UniqueID, "dblClick");
base.Render(writer);
}
I would like to change this so that the postback is asynchronous, using AJAX. At the moment, the listbox and button are in an UpdatePanel so there is an async postback when the button is clicked. But when the listbox is double-clicked, a full postback occurs.
<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
runat="server">
<ContentTemplate>
<asp:ListBox ID="lstActivities" runat="server"></asp:ListBox>
<asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click">
Edit</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
How can I make the double-click refresh only the UpdatePanel?
A few things to try:
<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lstActivities" />
<asp:AsyncPostBackTrigger ControlID="lnkButton" />
</Triggers>
.........
</asp:UpdatePanel>
or
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) return;
var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
lstActivities.Attributes.Add("ondblclick", refDblClick);
ScriptManager1.RegisterAsyncPostBackControl(lstActivities);
}
I just a ran a quick test with the code you provided and I do get "partial postbacks" (for lack of a better term since updatepanels always do full postbacks) on both, the clicking of the button and the double clicking of the list.
If you set other panels in that page to UpdateMode="Conditional" as you are doing with your UpdatePanel "up", then only elements inside "up" will be updated. If you don't specify the update mode on the other panels, then they will always be updated on postback, because, again, update panels ALWAYS do full postbacks; what they really do is partial refreshes of the page.
Linking MSDN documentation regarding UpdatePanel as I think is a very helpful read.
I tried the solutions suggested, but with no luck. It's quite a complicated page with lots of UpdatePanels so hard to work out the exact problem.
In the end I went for jQuery:
$(document).ready(function () {
$(document).delegate('#ctl00_body_lstActivities', 'dblclick', function () {
eval($('#ctl00_body_lnkButton').attr('href'));
});
});
I am writing a card game app using Ajax, c# and .NET 3.5. Due to the nature of the interface I have numerous update panels that Im trying to manage and update across various user action. I'm having problems with one though.
The players current hand is built by binding a list of Card objects to a repeater and then dynamically creating a Card UserControl and adding it to the Controls of a PlaceHolder when each item is databound. The code is roughly as follows:
On the page
<asp:UpdatePanel ID="pnlInHand" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rptInHand" runat="server" onitemdatabound="rptInHand_ItemDataBound">
<ItemTemplate>
<asp:PlaceHolder ID="plcInHandCard" runat="server" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
In code behind
protected void rptInHand_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Card card = (Card)e.Item.DataItem;
PlaceHolder plcCard = (PlaceHolder)e.Item.FindControl("plcInHandCard");
plcCard.Controls.Add(CreateCardControl());
}
private CardControl CreateCardControl()
{
CardControl cardControl = (CardControl)Page.LoadControl("~/UserControls/CardControl.ascx");
//Set control properties here
return cardControl;
}
The Card Control includes a Button. The ClickEvent for this button calls a Method of the Parent Page that needs to update a seperate UpdatePanel as well as remove the card Control from the Panel that it is sitting within.
I have two issues.
When I click the Card Control Button, because it has been created as part of a repeater within an updatePanel, it no longer exists when the page is posted back and so the Click event for the button within the control never fires. I can obviously rebind the repeater on page load, but does this mean I have to essentially do this on every postback?
More importantly I need a way to trigger the update of another updatepanel in the parent page when the Card control's click event is raised. Is there a way of setting a trigger on an update panel that listens out for an event within a dynamicaly loaded UserControl?
Many thanks
Stewart
Sample code from ASP.net site that should address your point 2 problem follows.
I'll leave the translation to your code to you.
I may be misunderstanding what you are trying to do but I believe once you get this working your issue with point 2 is no longer relevant as you'll get the AJAX postback you want from your parent update panel.
Good luck!
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="itemDataBound">
<ItemTemplate>
<mycontrol:user ID="user1" runat="server" OnCausePostBack="user1_CausePostBack" /> <br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
protected void itemDataBound(object sender, RepeaterItemEventArgs e)
{
ModalPopup_WebUserControl mw=(ModalPopup_WebUserControl)e.Item.FindControl("user1");
AsyncPostBackTrigger at = new AsyncPostBackTrigger();
at.ControlID = mw.ID;
at.EventName = "CausePostBack";
UpdatePanel2.Triggers.Add(at);
}
protected void user1_CausePostBack(object sender, EventArgs e)
{
// do something
}
just an idea for point 2 : what about add a property in the cardControl to set a reference to the updatepanel/s ? from there you can add triggers or call panel.update in the button event
for point one yes u will have to do it. u will have to re create the controls
for point 2 a simple updatePanel.update() would do the job insode of any event if you are using an event that was binded to a dynamically created control then u will have to rebind the event on every page postback