1

i am trying to keep Textbox value when i upload image by using hidden fields but after alot of try i am posting this issue that it always refresh textbox and my text box value flushes ??

my code is,

var hv = $('#hidden1').val();
var img = document.getElementById("<%= Btn_Preview.ClientID%>");
img.click();       
$('TxtBxDesignation').val() = hv;

actualy i call this script on change event and also calling .click event of preview button to view image on the same time i upload image

<asp:FileUpload runat="server" ID="FU_Img" onchange="FU_Img_onchange(this);" /><br />
                                    <asp:Button ID="Btn_Preview" runat="server" Text="Preview" BorderColor="Transparent"
                                        BackColor="Transparent" OnClick="Btn_Preview_Click" />

but its not working for me and textbox refresh when image upload and if i freez this line

"var img = document.getElementById("<%= Btn_Preview.ClientID%>");"

it works smoothly but for sure no chnage image ???

hopes for your suggestion thanks

5
  • Is the click() event causing a postback? Commented Aug 29, 2013 at 9:53
  • yes postback is happening Commented Aug 29, 2013 at 9:59
  • is there any way to maintain textbox value ? Commented Aug 29, 2013 at 9:59
  • If a postback is occurring then that's probably the reason why you're losing the value. The HTTP protocol is stateless so every time you refresh the page or a postback occurs, the controls reset back to their default values. Commented Aug 29, 2013 at 10:00
  • yup i got it then how would i retain tex box value ? Commented Aug 29, 2013 at 10:03

3 Answers 3

2

When a file is selected, on PostBack PostedFile property gets initialized with HttpPostedFile object.Since http request can't maintain state, and PostedFile is initialized with HttpPostedFile Object so it looses it's state.

One way to retain state is to store fileupload object in session and in the post back get the values back from it. Use this code in Page_Load.

if (Session["FileUpload1"] == null && FileUpload1.HasFile)  
    { 
        Session["FileUpload1"] = FileUpload1; 
        Label1.Text = FileUpload1.FileName; 
    } 

else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile)) 
{ 
    FileUpload1 = (FileUpload) Session["FileUpload1"]; 
    Label1.Text = FileUpload1.FileName; 
} 

else if (FileUpload1.HasFile) 
{ 
    Session["FileUpload1"] = FileUpload1; 
    Label1.Text = FileUpload1.FileName; 
}

OR

You may also go for AJAX AsyncFileUpload control for AJAX based upload and it retains path. Check below URLs on this:

1.) http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

2.) http://www.aspsnippets.com/Articles/Using-ASP.Net-AJAX-Control-Toolkits-AsyncFileUpload-Control.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

Maintaining the ViewState

When a form is submitted in ASP, all form values are cleared. Suppose you have submitted a form with a lot of information and the server comes back with an error. You will have to go back to the form and correct the information. You click the back button, and what happens.......ALL form values are CLEARED, and you will have to start all over again! The site did not maintain your ViewState.

When a form is submitted in ASP .NET, the form reappears in the browser window together with all form values. How come? This is because ASP .NET maintains your ViewState. The ViewState indicates the status of the page when submitted to the server. The status is defined through a hidden field placed on each page with a control. Maintaining the ViewState is the default setting for ASP.NET Web Forms. If you want to maintain the ViewState, include the directive

      <%@ Page EnableViewState="true" %>

Please visit these VIEWSTATE to learn more about view state

Comments

1

Take a look at this thread for a technique used to maintain state across postbacks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.