I realize I may be pointed to different articles but my head is spinning on the right way to do this. I tried some code to simply display the current time, then display the finish load time and then display the async label 5 seconds later. I currently have a website that does a COUNT SQL script on the homepage that displays the total rows in a label and its pausing the home page until it's complete. Obviously for performance reasons I need to get rid of this. I tried simply putting some script inside an update panel and nope.
<asp:Label ID="Label1" runat="server" Text="Label">
First Load is <% =GetTime()%>.
</asp:Label>
<script runat="server">
protected String GetTime()
{
return DateTime.Now.ToString();
}
</script>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<script runat="server">
protected String GetAsyncTime()
{
System.Threading.Thread.Sleep(5000);
return DateTime.Now.ToString();
}
</script>
<asp:Label ID="lblPanel" runat="server" Text="Label"></asp:Label>
Async time is <% =GetAsyncTime()%>.
</ContentTemplate>
</asp:UpdatePanel>
<br />
</form>
<asp:Label ID="Label2" runat="server" Text="Label">
Last Load is <% =GetTime()%>.
</asp:Label>
Related
I have below structure in my asp.net application.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="overlay"></div>
<asp:Panel ID="pnlMain" runat="server" Visible="true">
<asp:UpdatePanel ID="upProfile" runat="server" UpdateMode="Always">
<ContentTemplate>
<div>
<table>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upAttachment" runat="server" UpdateMode="Always">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel ID="pnlSuccessMessage" runat="server" >
<asp:Label ID="lblSuccessMessage" runat="server"></asp:Label>
</asp:Panel>
I have different textfields and dropdowns in the table of upProfile updatepanel and file upload control in upAttachment updatepanel. Now when I select one radio from the same update panel, I am getting below error and the upAttachment does not show up on page.
Uncaught Error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'MainContent_upAttachment'. If it is being updated dynamically then it must be inside another UpdatePanel.
I have searched a lot and tried below different things, but still it is not working
1. Set the upAttachment UpdateMode to Conditional.
- In this case, it does not throw error, but the file upload control which is inside that updatepanel, it does not show up
2. Place the upAttachment updatepanel inside another updatepanel, but that too is not working.
I don't know what to do in this case. Can anyone help me on this?
I'm working on web application based on ASP.Net and C#.
When i click the button in Content page, I want to pass data(string) to Masterpage and show it to the user. I know the technique and codes but the problem is with Ajax.
Since the button in content page is inside of Updatepanel, when i click the button, message will not be appear in masterpage. Because only part of the page wil be refrsh and i do not know, how to refesh master page
Code inside contentpage:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Body_Member" ...>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ... ></asp:TextBox>
<asp:Button ID="ShowText" ... />
</ContentTemplate>
</asp:UpdatePanel>
Code inside masterPage (actually this is a nested masterPage):
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_TopRightSide" runat="server"> ... </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder_Menu" runat="server"> ... </asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder_Body" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder_Body_Member" runat="server"> </asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder_Footer" runat="server">
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<ContentTemplate>
<asp:Label ID="DataFromPage" ... ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Button is insde of "ContentPlaceHolder_Body_Member" and the label is inside of "ContentPlaceHolder_Footer"
To achieve the result that you need you just need to make a little change on your Master page design like the code below:
On your master-page place the content-place-holder inside the update panel first:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="DataFromPage" runat="server" Text="Label"></asp:Label>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Then in your Content page you will have something like this (almost the same as yours)
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Finally in your code-behind on button-click event you could assign your value to the master page's label control like the code shown below:
protected void Button1_Click(object sender, EventArgs e)
{
Label myLabel = Page.Master.FindControl("UpdatePanel1").FindControl("DataFromPage") as Label;
myLabel.Text = "this is a message from my content page";
}
And here is the result :
UPDATE 1
Ok remember if you have a nested update panel then the way you find the control in that one is a bit different also if you need to make changes on the other content page you need to force the update panel to make the changes in your code behind, so your code should be like this:
//your button control's event needs to find the update panel and the label both:
protected void Button1_Click(object sender, EventArgs e)
{
UpdatePanel myUpdatePanel = Page.Master.Master.FindControl("ContentPlaceHolder_Footer").FindControl("UpdatePanel") as UpdatePanel;
Label myLabel = Page.Master.Master.FindControl("ContentPlaceHolder_Footer").FindControl("DataFromPage") as Label;
myLabel.Text = "this is a message from my content page";
myUpdatePanel.Update(); //force the panel to get updated
}
Cheers
I have a page name ask.aspx
I have 4 list views in that page which are binding at the time of page load event.
now when ever i click on the items of list view i get the whole page refreshed.
I have used the update panel but for some reasons it is not working
The code for the same is given below.
aspx page
enter code here
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
<legend><asp:Label ID="Label1" runat="server"></asp:Label></legend>
<ul class="tags">
<asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand" >
<ItemTemplate>
<li><asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Eval("CategoryId") %>'><%# Eval("Name") %></asp:LinkButton></li>
</ItemTemplate>
</asp:ListView>
</ul>
</fieldset>
</ContentTemplate>
<Triggers></Triggers>
</asp:UpdatePanel>
ending the content template and update panel.
similarly for the other three listviews.
at cs page
some calculations are taking place.
Now i dont want the page to refresh every time.
what should be done.
please suggest.
Remove childrenastrigger property and set updatemode=always
All I wish to do is simply click a button and the text in a textbox is automatically added as an item in the listbox. Shouldn't this be straight forward? Whilst debugging, the item is added and I can see the text by watching ListBox1.Items[0], but nothing is displayed in the web page. I had the same problem which i did not solve, in a console application! Can some one please guide me to what I am doing wrong?
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(new ListItem(TextBox1.Text));
}
Many thanks
Edit:
In a past project, I used the DataSource property, which worked perfectly. I have never yet managed to use the add Items! May be there is some sort of refresh or update?
Page code:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="295px"></asp:ListBox>
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
Looks like your listbox is outside of the update panel. Pop it inside the update panel:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="AddItem" />
</ContentTemplate>
</asp:UpdatePanel>
You have to move the ListBox into the UpdatePanel, otherwise it will not be updated.
The reason for that is, that ASP.NET is sending the whole HTML of the UpdatePanel back to the client. Since the ListBox is not part of the UpdatePanel, it won't be updated.
I am having a strange issue associated with AsyncFileUpload control. after the upload, I am updating the page by calling__doPostBack function from ClientUploadComplete event handler. it works fine first time, but next time I try to upload the file, it refreshes the page first before uploading, then does the upload and refreshes the page again. not sure why refresh page is being called twice once before the upload and once after the upload. I have a simplified version of this code which has this bug. any clues please why it is happening?
Markup:
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="AsyncFileUpload1_ClientUploadComplete"
OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Refresh Data" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" EnableViewState="false"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Javascript:
<script type="text/javascript">
function AsyncFileUpload1_ClientUploadComplete() {
var btnRefreshData = $get("<%=Button1.ClientID%>").name;
__doPostBack(btnRefreshData, "");
}
</script>
Code-Behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Upload complete";
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
System.Threading.Thread.Sleep(3000);
}
looks like no one can answer this question. I still haven't figured out why this happens, but I put a workaround by adding a flag in session when the upload is complete and check that flag before refreshing the data on the page. this way the data refresh won't happen before the upload. thanks.