Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:29:47+00:00 2026-06-09T16:29:47+00:00

I want to set the Image URL of Image control using client side code

  • 0

I want to set the Image URL of Image control using client side code and save the image to the server using C# code. Here is what i have implemented:

<asp:Button ID="btnImageUpload" OnClick="btnImageUpload_Click"  runat="server" Text="Preview" CausesValidation="false" OnClientClick="Image_View();"/>

C# Code:

protected void btnImageUpload_Click(object sender, EventArgs e)
{
    if (Directory.Exists(@"C:\\Images"))
        SaveImage_Server();
    else
    {
        Directory.CreateDirectory(@"C:\\Images");
        SaveImage_Server();
    }
}
    public void SaveImage_Server()
    {
        try
        {
            if (FlUpldImage.PostedFile.ContentLength > 0)
            {
                String fn = Convert.ToString(DateTime.Now) + Path.GetFileName(FlUpldImage.FileName);
                if (fn.Contains('/'))
                {
                    fn = fn.Replace("/", "");
                }
                if (fn.Contains(':'))
                {
                    fn = fn.Replace(":", "");
                }
                if (fn.Contains(" "))
                {
                    fn = fn.Replace(" ", "");
                }
                String Saved_ImagePath = @"C://Images/" + fn; // making the path with created dynamically folder name
                FlUpldImage.SaveAs(Saved_ImagePath);
                HidnLocalImageURL.Value = Saved_ImagePath;

            }
        }
        catch (Exception re)
        { 
        }
    }

JavaScript

    function Image_View() {
//        __doPostBack('<%= btnImageUpload.ClientID %>', '');

//        var clickButton = document.getElementById("<%= btnImageUpload.ClientID %>");
//        clickButton.click()

        var idFlUpload = '<%= FlUpldImage.ClientID %>';
        var fu1 = document.getElementById(idFlUpload);
        var idImgCntrl = '<%= imgCorrect.ClientID %>';
        var ImgCntrl = document.getElementById(idImgCntrl);

        alert("You selected " + fu1.value);
        ImgCntrl.setAttribute('src', fu1.value);
    }

Now my issue is that once the server side code is executed the page gets refreshed and the link set to Image control using JS gets reset to default value.

How can i get this working wherein the image also gets saved and Image URL property also gets set through JS.

If there is any other way to implement this than please let me know. Thanks in Advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:29:48+00:00Added an answer on June 9, 2026 at 4:29 pm

    You have to set it on server too. You can use hidden field to save the url and access that hidden field on server to get the url to set mgCntrl.ImageUrl

    In html

    <input type="hidden" runat="server" id="hdnImageSrc" />
    

    On Client javascript

    hdnImageSrc = document.getElementById('<%= hdnImageSrc.ClientID %>');
    mgCntrl.setAttribute('src', fu1.value);
    hdnImageSrc.value = fu1.value;
    

    On server side code

    mgCntrl.ImageUrl = hdnImageSrc.Value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set an image control with some url but the image should
I have an Image control on my .aspx page and I want to set
i have certain element in a asp.net control: <a href=# title= style=background-image: url('../i/foo/image.png')> but
I have one control named thumbviewer inside repeater. I want to set its imageurl
i want have an image and i want to set it as a background
I have an image with height 173px and width 157px .I want to set
I have a url that when requested will return an image. I want the
I have a Image, FileUpload and a Button controls. I want to save the
I want to set an image as my background at runtime, so I created
I want to set background image to right and top of my span because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.