txtName is the first control in the page. When the page loads initially, it is empty. If there is text in the textbox and the user clicks on Refresh, it is not clearing the text.
At debug, i can see the textbox being cleared, but on page it isnt!!!!.
This happens in Firefox. Works fine in IE.
I have tried , Keeping the textbox outside the updatepanel, enveloping another updatepanel for this textbox alone, Enabling the viewstate, and also removing the clientidmode property.
ASPX:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBack ControlID="txtName" EventName="TextChanged"/>
</Triggers>
<ContentTemplate>
<asp:TextBox ID="txtName" runat="server" OnTextChanged="txtName_TextChanged"
EnableviewState="false" ClientIDMode="Static">
</asp:TextBox>
//Some other controls
</ContentTemplate>
</asp:UpdatePanel>
C#:
protected void page_load()
{
if(!IsPostBack)
{
txtName.Text="";
UpdatePanel1.Update();
}
}
protected void page_load()
{
if(!IsPostBack)
{
UpdatePanel1.Update();
}
txtName.Text="";
}
Try this way
Please add txtName.Text=""; outside if condition. Try like this.
protected void page_load()
{
txtName.Text="";
if(!IsPostBack)
{
UpdatePanel1.Update();
}
}
An alternative solution is to set autocomplete=off on your textbox, which will work on FF.
If you do need autocompletion, you can wrap your code into a form. and reset it in the onLoad event
<form id="autoclearform">
//your textboxes
</form>
Now either add onload="autoclearForm.reset();" to your body or do it via javascript.
$(document).ready(function() {
autoclearForm.reset();
});
Related
I have a simple DropDownList on my asp.net webpage:
It looks like this:
<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="FriendList" runat="server"
EnableViewState="False" Height="30px" Width="10%"
OnTextChanged="FriendListSwitch" AutoPostBack="True">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSend" />
</Triggers>
<--!The button is a stub, I actually need to load it from the codebehind-->
</asp:UpdatePanel>
Then I have the codebehind like this:
void OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
{
FriendList.Items.Add(new ListItem(String.Format("{0}", item.Jid.User), ""));
FriendList.DataBind();
}
While debugging I can see that 'FriendList' is being filled, and also that 'btnSend' is being clicked.
Yet my DropDownList is not refilling itself, it just stays empty, why is it not refreshing?
Edit: I'd rather remove the button and replace it with
void OnRosterEnd(object sender)
{
UpdatePanel5.Update();
}
But then I keep getting
The Update method can only be called on UpdatePanel with ID 'X' before Render.
Solved, the DataBind() only worked inside a Page_Load for me.
The button 'btnSend' should be inside the update panel
try using OnSelectedIndexChanged event instead of OnTextChanged
If you need the button to update the dropdown then it needs to be in the update panel
If you need the dropdown list to be the trigger then asyncpostback trigger needs to have the control id of the dropdown list
I have script manager in master page:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptLocalization ="true" EnableScriptGlobalization ="true" EnablePartialRendering="true" >
</asp:ScriptManager>
MyPage.aspx (Placed in contentplaceholder of master page)
<asp:UpdatePanel ID="upMain" runat="server" UpdateMode="Conditional" EnableViewState="true">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRefresh" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:PlaceHolder ID="placeHolder1" runat="server" EnableViewState="true"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
MyPage.aspx script:
$(document).ready(function () {
setInterval(myfun, 20000);
});
function myfun() {
var btn = document.getElementById('<%=btnRefresh.ClientID%>');
btn.click();
}
On the place holder I'm putting dynamically created table containing user controls.
On the page load, I can see the user controls on the page. But when Async postback is called after 20 seconds, I don't see the user controls. With firebug, I see that user controls are there. But they do not get rendered; even though I'm creating them again on async postback. They appears to be empty. Help.
Try this code. You need to create controls on every page postback.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetData(); // get data from database
}
LoadUserControls(); // Create dynamic table containing user controls
}
I think you should read this. Loading UserControl Dynamically in UpdatePanel
I have a master page that contains an image as follow:
<asp:Image ID="imgCompanyLogo" runat="server" ImageUrl="image path" />
and in a child page I want to edit the property ImageUrl as follow:
Image imgCompanyLogo = (Image)Page.Master.FindControl("imgCompanyLogo");
imgCompanyLogo.ImageUrl = ResolveUrl("~/images/CompanyLogo/Logo.png");
and it doesn't give me an exception, but it doesn't change anything.
Note: I have an UpdatePanel in the child page.
Since the image is sitting outside of the UpdatePanel, server side changes will not be executed on the image after a partial postback. Your only option is to inject JavaScript into the page and change the image URL.
Use the ScriptManager.RegisterStartupScript Method to inject JavaScript after the partial postback.
Something like the following will work for you:
C#
protected void btnPostback_Click(object sender, EventArgs e)
{
imgCompanyLogo.ImageUrl = ResolveUrl("~/images/CompanyLogo/Logo.png");
ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "myScript", "ChangeImage('" + ImageUrl + "');",false);
}
JavaScript
function ChangeImage(imgURL) {
//make sure the ID of the image is set correctly
document.getElementById('imgCompanyLogo').src = imgURL;
}
Wrap image by UpdatePanel with UpdateMode="Always"
Master Page:
<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Image runat="server" ID="Image1" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
public void SetImageUrl(string url)
{
Image1.ImageUrl = url;
}
Child Page:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button Text="Click Me" runat="server" OnClick="UpdateImage" />
</ContentTemplate>
</asp:UpdatePanel>
protected void UpdateImage(object sender, EventArgs e)
{
((Main)Master).SetImageUrl("~/Images/0306d95.jpg");
}
The code above works well for me.
Have a look at Sys.WebForms.PageRequestManager Class. Define handler in javascript and may change the image source.
If your code is being run after an async postback (per your UpdatePanel) then changes to anything outside the UpdatePanel will not be rendered. Content in the master page would definitely seem to qualify.
If this is what you're trying to do, this model won't really work. You will need to use some client script to effect changes to already-rendered content when working with this model.
An UpdatePanel is a construct to identify an area that's updated through ajax. The page is not actually reloaded. So an postback can never change content that's outside of the UpdatePanel (or control bound to that panel) that sourced it.
Here's a basic implementation (using jQuery). Add a hidden field to pass the new source to the client. This must be inside the UpdatePanel. Change this value from the server when you want the image to update with new_image_src.Value=ResolveUrl(...);
<asp:HiddenField ClientIdMode="Static" runat="server" id="new_image_src"
value="" EnableViewState="false">
Give your image a static id too to make life easier:
<asp:Image ClientIdMode="Static" runat="server" id="dynamic_image" ImageUrl="..." >
Add javascript to the page (should NOT be in the UpdatePanel):
function updateImage() {
var new_src=$('#new_image_src');
if (new_src) {
$('#dynamic_image').attr('src',new_src);
/// erase it - so it won't try to update on subsequent refreshes
new_src.val('');
}
}
$(document).ready(function() {
/// adds an event handler after page is refreshed from asp.net
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(updateImage);
});
This wouldn't be difficult to do without jquery either but seems more common than not these days.
I'm working on a ASP.NET site with C# code.
Now the trouble starts when I create a custom control programmatically. The control displays in a panel, but when I click one of the buttons of the control it does nothing. If I click them twice, the user control disappears.
Using the debugger, I found that it's doing a postback, which is strange because I tried using buttons and setting the usesubmitbehavior to false; it's still sending postbacks.
Here is where the control is inserted on the default.aspx file
<asp:UpdatePanel runat="server" ID="contentHolderUpdatePanel"
UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel runat="server" ID="contentPanel">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Here is the ASPX from ListadoAuditoria of the control.
<asp:UpdatePanel runat="server" ID="auditorTableUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Table runat="server" ID="auditorTable" BorderWidth="0" Width="100%">
<asp:TableHeaderRow HorizontalAlign="Center">
<asp:TableHeaderCell>Button
</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="formHolderUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label runat="server" ID="testLabel" Text="bbbbbbbbbbbbb" ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
The method that the button invokes should change the text of the label testLabel from "bbbbbbbbbbbbb" to "aaaaaaaaaaa". Obviously I'm doing an auditorTableUpdatePanel.Update() after I modify the text.
The control CS
protected void Page_Load(object sender, EventArgs e)
{
loadAudits();
}
public void loadAudits()
{
for(int i=0;i<10;++i)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
ImageButton deleteButton = new ImageButton();
deleteButton.ImageUrl = "~/image.gif";
deleteButton.Click += generateNewPart;
deleteButton.EnableViewState = true;
deleteButton.ID = i.ToString();
cell1.Controls.Add(deleteButton);
row.Cells.Add(cell1);
auditorTable.Rows.Add(row);
}
}
public void generateNewPart(object sender, EventArgs e)
{
tumadre.Text = "aaaaaaaaaaaa";
formHolderUpdatePanel.Update();
}
And here is the code when I generate the control and insert it into the panel:
Panel panel = (Panel)Page.FindControl("contentPanel");
UpdatePanel updatePanel = (UpdatePanel)Page.FindControl("contentHolderUpdatePanel");
ListadoAuditorias listadoAuditorias = (ListadoAuditorias)LoadControl("~/CargaDeAuditoria/ListadoAuditorias.ascx");
panel.Controls.Add(listadoAuditorias);
updatePanel.Update();
I looked over the Internet and didn't found anything.
I'm not sure where exactly the code to dynamically add the controls is, but it must be called on EVERY postback to re-add the controls. You can't just add it once and forget about it. When you postback, the page will re-render with the markup in your aspx page (which does not have your dynamic controls, obviously). The values from the dynamically added controls will still be in ViewState, but the controls will not be re-rendered.
I'm not entirely sure what I'm looking at; I don't know if the "code of the control" is the code for the ListadoAuditorias control that you're loading. If so, I didn't notice any buttons.
So I could be wrong here, but the first thing that pops out at me is that it looks like you're loading the ListadoAuditorias control and then adding it to a normal Panel control. If one of the controls inside of ListadoAuditorias triggers a postback, and it's not contained within an UpdatePanel, then yes, I'd expect the page to do a postback and reload, unless you have specified the ChildrenAsTriggers and UpdateMode properties to be something other than their default values (I think). So I would just suggest that you take a look at where you are adding your controls. Make sure that they're contained within an UpdatePanel, if that's what your intention is.
Also, note that the UseSubmitBehavior property of the Button control does not prevent the button from initiating a postback. That property only determines whether the button gets rendered as <input type="submit" /> or <input type="button" />. In the latter case (when you set UseSubmitBehavior to false) the control still renders javascript in the HTML element's onclick attribute to cause a postback.
EDIT: I've amended my explanation regarding UpdatePanel control to mention the ChildrenAsTriggers and UpdateMode properties.
I have TextBox (multiline) and Label in an UpdatePanel which I refresh with javascript __doPostBack(upEditReminder,id);
Then I set both Label and TextBox text to current DateTime.
protected void upReminder_Onload(object sender, EventArgs e)
{
lbTest.Text = DateTime.Now.ToString();
tbReminder.Text = DateTime.Now.ToString();
Problem is that Label is updated but TextBox date is updated only once when the page is loaded but not when __doPostBack(upEditReminder,id); is triggered.
I cant figure out what the problem is.
I have also tried textarea runat="server" but still have the same problem.
Your help is much appreciated.
This worked for me... is it different from what you're doing?
aspx code snippet:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
Update
codebehind snippet:
protected void UpdatePanel(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
TextBox1.Text = DateTime.Now.ToString();
}
Clicking the "Update" link triggers the UpdatePanel's postback which refreshes it via ajax and both the label and textarea get the updated timestamp.
Could you try it after setting EnableViewState as false in that textbox?
Are you setting the textbox text anywhere else in your code behind? I'm guessing it's getting overwritten somewhere...
Add a button inside the UpdatePanel. Click the button, does it update both label and textbox?
In addition, the call you are making should have the ClientID of the updatepanel which looks something like this:
__doPostBack("ctrl00_ctrl01_upEditReminder",'');
From one of your comments I noticed that you are trying to update a textbox outside of the updatepanel. The problem here is that you cannot update something outside the updatepanel in the updatepanel's postback. That's one of the drawbacks of using an updatepanel.
If you still want to use an updatepanel, I suggest that you update the other page's elements using javascript and preferrable jQuery after the updatepanel reload. You could use a hidden input field inside the updatepanel to transfer the data. To refresh the textbox after the update, you could use this JavaScript/jQuery code:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
var reminder = $("[id$='hidReminder']").val();
$("[id$='tbReminder']").val(reminder);
});