Image URL not changing in aspx page - asp.net

I have got the following code snippet in my page
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="testButton" />
</Triggers>
<ContentTemplate>
<asp:Image ID="testImage" runat="server" ImageUrl="~/triangle.png" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button runat="server" ID="testButton" Text="Change Image" OnClick="testButton_Click" />
and have got the following in my code behind
protected void testButton_Click ( object sender , EventArgs e ) {
testImage.ImageUrl = "";
}
When I click on the button, the image is vanished since its URL has been set to empty string in code behind - I understand that. But when I view the page source, I see the original image URL as initialized in the asp page where I badly need URL to be set to empty string.

I have changed the ImageUrl value of your asp image in your markup as below:
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="testButton" />
</Triggers>
<ContentTemplate>
<asp:Image ID="testImage" runat="server" ImageUrl="<%#changeableImageUrl %>" />
</ContentTemplate>
Code Behind:
public string changeableImageUrl;
protected void Page_Load ( object sender , EventArgs e ) {
changeableImageUrl= "~/triangle.png"; // initial image URL
this.DataBind (); // since data have been bound in the page by data binding syntax
// <%#changeableImageUrl %>
}
protected void testButton_Click ( object sender , EventArgs e ) {
changeableImageUrl= ""; // to set html-rendered src attribute of img element to ""
this.DataBind(); //bind data to the page
}
changeableImageUrl must be declared public within the page. Now, you can change changeableImageUrl to change the image URL.

This is caused because the browser does not reload all source of page but if you change parameter in ScriptManager, In IDE, (not runtime). If you set to false EnablePartialRendering = false; this may solve your problem and always have new html code.

Related

ASP Images Refreshing until first cycle

I am working on an ASP project (fairly new to ASP) one weird thing I have noticed is that images appear to refresh until after the first circle of images, my page code is:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Image runat="server" ImageUrl="~/Images/1.png" ID="updateImage"></asp:Image>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="click" />
</Triggers>
</asp:UpdatePanel>
And the code behind is:
protected void Button1_Click(object sender, EventArgs e)
{
if (updateImage.ImageUrl != "~/Images/8.png")
{
//Get Image number
string image = updateImage.ImageUrl.ToString();
char[] file = image.ToCharArray();
int fileName = Convert.ToInt32(file[9].ToString());
fileName++;
updateImage.ImageUrl = "~/Images/" + fileName.ToString() + ".png";
}else
{
updateImage.ImageUrl = "~/Images/1.png";
}
}
When I click the button the first cycle of 8 images looks as though the page is reloading, but after that there is no reload on the page and the images just change smoothly, is this because ASP caches the images after the first cycle? Is there any way to stop it flickering on the first cycle?
Thanks.

How can i refresh the UI using Component Art Callback in C# Asp.net

once the page is loaded if we need to add something on the UI then how we will do it by using callback without loading the whole page again.
You can use UpdatePanel to do this i.e. without refreshing your whole page it can change components of your page.
<asp:UpdatePanel ID="UpdatePanelMain" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="BtnClick" runat="server" OnClick="BtnClick_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnClick" EventName="Click" />
</Triggers>
Now you will get call in code behind whenever "BtnClick" button is clicked. There you can write your code.
protected void BtnClick_Click(object sender, EventArgs e)
{
// Your logic
}

asp.net file upload inside formview always return false (not in updatepanel)

I am using fileupload control inside formview edittemplate
<asp:FileUpload ID="fileup_profilfoto" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Sadece şu formatlar (.jpg, .bmp, .png, .gif)" ValidationExpression="^.*\.(jpg|JPG|png|PNG|bmp|BMP|gif|GIF)$" ControlToValidate="fileup_profilfoto" ForeColor="#00C0CC"></asp:RegularExpressionValidator>
It was working.But I added an updatepanel then it didnt work,and then I remowed update panel.But it's still return false (hasfile)
protected void frmviewProfil_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
try
{
FileUpload fileup_profilfoto = (FileUpload)frmviewProfil.FindControl("fileup_profilfoto");
if (fileup_profilfoto.HasFile)
{
//do something
}
else
{
//do something
}
}
}
always goes else scopes.
hi use triggers to achive that
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
code behind
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Label1.Text = FileUpload1.FileName;
}
}
Did you do anything to the properties of the fileupload control, for example setting the autopost back value to false? Try setting this to true if it is false.
I came across this question when I had this problem and figured I'd post what my problem and solution were as well.
Make sure the file you are trying to upload is larger than 0 bytes. I was trying to upload some blank text files for testing and each file had the FileName property set correctly, but HasFile was always false. Adding some text to the files gave it some content and the file was able to be uploaded successfully.

ASP Update Panel Stopped Working

Language: C#
Compiler: Visual Studio 2012
O/S: Windows 7 Home Premium
I have been using an UpdatePanel on my Content page for a while now, to update an Image based on text in a text box.
It all worked, up until now. Currently, the page does a full reload to display the image, instead of a partial post-back.
.aspx code
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Image ID="imgProfilePicture" runat="server" />
<br />
<asp:TextBox ID="txtImageLocation" runat="server" AutoPostBack="True" MaxLength="1000" CssClass="styleTextBoxCenter" Width="170px" OnTextChanged="txtImageLocation_TextChanged"></asp:TextBox><br />
<div style="padding-left: 4px;">
<asp:Label ID="lblImageUrl" runat="server" Text="Image URL" CssClass="styleLabelWatermark"></asp:Label>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtImageLocation" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
.cs code
protected void txtImageLocation_TextChanged(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(txtImageLocation.Text))
{
imgProfilePicture.ImageUrl = RemoteReaderPlugin.Current.CreateSignedUrl("http://i.minus.com/iNQ7wK2opRJT1.gif",
new ResizeSettings(
"width=183&format=png"));
lblImageUrl.Text = "Image URL";
return;
}
if (!#txtImageLocation.Text.StartsWith("http://"))
{
#txtImageLocation.Text = "http://" + #txtImageLocation.Text;
}
WebRequest request = WebRequest.Create(#txtImageLocation.Text);
try
{
request.GetResponse();
imgProfilePicture.ImageUrl = RemoteReaderPlugin.Current.CreateSignedUrl(#txtImageLocation.Text,
new ResizeSettings(
"width=183&s.roundcorners=10"));
lblImageUrl.Text = "Image Verified";
lblImageUrl.ForeColor = System.Drawing.Color.Green;
}
catch (Exception)
{
imgProfilePicture.ImageUrl =
RemoteReaderPlugin.Current.CreateSignedUrl(
"http://i.minus.com/ibwhXZ9wLo1mOz.jpg",
new ResizeSettings("width=183&s.roundcorners=10"));
lblImageUrl.Text = "Invalid URL";
lblImageUrl.ForeColor = System.Drawing.Color.Red;
txtImageLocation.Focus();
}
}
I cant think of anything that I've changed, and the Master page still has a ScriptManager on.
Turns out, all functionality is lost on an UpdatePanel if you set a "Global" Routein your Global.asax.
The issue started when I added routes.MapPageRoute("", "{address}", "~/{address}.aspx");.
Upon removing, the ajax panels worked.

ASP.NET No postback on asp:ImageButton

I have an ASP page with an asp:DropDownList (with AutoPostBack="true") so that when the user changes it, it reload the appropriate data.
Under that control i have a list of UserControls, that includes a tinymce editor (tied to an asp:TextBox) and an asp:ImageButton to save the data.
When clicking on the ImageButton, the applications send the postback data via ajax to the same page (__EVENTARGUMENT, __EVENTTARGET, etc...). Why does it load that ajax page, and how do i prevent it? I'm updating the value in the DB in the OnClick event handler on the ImageButton, so all I need to do, is get ride of that ajax call.
Any ideas?
Solution 1
<asp:ImageButton ID="btn" runat="server" ImageUrl="~/images/yourimage.jpg"
OnClientClick="return false;" />
OR
Solution 2
<asp:ImageButton ID="btn" runat="server" ImageUrl="~/images/yourimage.jpg"
OnClientClick="yourmethod(); return false;" />
In addition (solution 2), your javascript method may be in this form
<script type="text/javascript">
function yourmethod() {
__doPostBack (__EVENTTARGET,__EVENTARGUMENT); //for example __doPostBack ('idValue',3);
}
</script>
in code behind
protected void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack) {
string eventTarget = this.Request("__EVENTTARGET") == null ? string.Empty : this.Request("__EVENTTARGET");
string eventArgument = this.Request("__EVENTARGUMENT") == null ? string.Empty : this.Request("__EVENTARGUMENT");
}
}
You've not stated you're using an UpdatePanel but this is presumably how you've implemented ajax calls. If so you need to add a trigger to exclude the imagebutton event from ajax:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="ImageButton" />
</Triggers>
<ContentTemplate> </ContentTemplate>
</asp:UpdatePanel>

Resources