Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 3 – ViewState in ASP.NET






2 comments:

  1. Sir, I have a question, you said that the viewstate property for the asp.net server controls is internally enabled, so I want to ask that is it true for just textboxes or for all asp.net server controls?
    Can u just help me with this code because it's not working without using viewstate property

    protected void Button1_Click(object sender, EventArgs e)
    {
    if (FileUpload1.HasFile)
    {
    string filename = Path.GetFileName(FileUpload1.FileName);
    FileUpload1.SaveAs(Server.MapPath("~/") + filename);
    Image1.ImageUrl = Path.GetFileName(FileUpload1.FileName);
    Label1.Text = "File Uploaded Successfully";
    ViewState["abc"] = FileUpload1.FileName;
    }
    else
    {
    Label1.Text = "Error in file upload";
    }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    Image1.ImageUrl = Path.GetFileName(FileUpload1.FileName);

    }

    in this code I want to upload a picture using Fileupload control but want to see it before uploading but when i click the button 2 the fileupload field becomes empty and I can't upload the picture after that on clicking button 1. I hope u will understand my question??

    ReplyDelete
  2. Sir, I want to ask that, is the view state property enabled internally for all asp.net server controls or just for textboxes??
    I tried a code where i want to upload a picture and want to see that picture before uploading it while it's name is in fileupload's field but when i click the show button before upload button the filename is no more there in the fileupload field & therefore upload button doesn't works.. I want to do this without using viewstate property..
    Here is a part of my code

    protected void Button1_Click(object sender, EventArgs e)
    {
    if (FileUpload1.HasFile)
    {
    string filename = Path.GetFileName(FileUpload1.FileName);
    FileUpload1.SaveAs(Server.MapPath("~/") + filename);
    Image1.ImageUrl = Path.GetFileName(FileUpload1.FileName);
    Label1.Text = "File Uploaded Successfully";
    ViewState["abc"] = FileUpload1.FileName;
    }
    else
    {
    Label1.Text = "Error in file upload";
    }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    Image1.ImageUrl = Path.GetFileName(FileUpload1.FileName);

    }

    i hope u will be able to understand my question...

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.