6
Answers

File Upload data getting cleared after a textbox changed event fired

Photo of Sandeep Kumar

Sandeep Kumar

Jun 05
516
1

 <td style="border: 1px solid #1a1a1a; padding: 5px 8px;" align="center">
                                    <asp:HiddenField ID="hdnFilePath1" runat="server" />
                                    <asp:HiddenField ID="hdnFilename1" runat="server" />
                                    <asp:HiddenField ID="hdnFileUniqueName1" runat="server" />
                                    <asp:FileUpload ID="fuquationFile1" runat="server" AllowMultiple="true" />

                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator25" runat="server" Text="*" ControlToValidate="txtOfferedPrice_P1"
                                        ErrorMessage="*" SetFocusOnError="true" ValidationGroup="ValDept" InitialValue="">
                                    </asp:RequiredFieldValidator>
                                </td>

 

-----------------------------------

    <asp:TextBox ID="txtBasicValueGst18Per" runat="server" onkeypress="return alertBox(this,event);" autocomplete="off"
                    TextMode="SingleLine" TabIndex="6" Width="150" MaxLength="10" AutoPostBack="true" CssClass="form-control" OnTextChanged="txtBasicValueGst18Per_TextChanged"></asp:TextBox>

Answers (6)

4
Photo of Jayraj Chhaya
298 6k 94.2k Jun 06

When a postback occurs due to the TextChanged event of the txtBasicValueGst18Per TextBox, the entire page lifecycle is executed again, which includes re-rendering the page controls and resetting their values to their original states.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Check if file data is stored in session state
        if (Session["FileData"] != null)
        {
            // Retrieve file data from session state and reassign it to FileUpload control
            var fileData = (FileData)Session["FileData"];
            fuquationFile1.FileName = fileData.FileName;
            // Assign other file data properties as needed
        }
    }
}

protected void txtBasicValueGst18Per_TextChanged(object sender, EventArgs e)
{
    // Your event handler logic here
    
    // Store file data in session state
    var fileData = new FileData
    {
        FileName = fuquationFile1.FileName,
        // Set other file data properties as needed
    };
    Session["FileData"] = fileData;
}

// Define a class to represent file data
public class FileData
{
    public string FileName { get; set; }
    // Add other file data properties as needed
}
3
Photo of Jignesh Kumar
29 39.5k 2.9m Jun 07

Hello Sandeep,

you can use the update panel below and handle partial postback,

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
         <asp:TextBox ID="txtBasicValueGst18Per" runat="server" onkeypress="return alertBox(this,event);" autocomplete="off"
                    TextMode="SingleLine" TabIndex="6" Width="150" MaxLength="10" AutoPostBack="true" CssClass="form-control" OnTextChanged="txtBasicValueGst18Per_TextChanged"></asp:TextBox>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="txtBasicValueGst18Per" EventName="TextChanged" />
    </Triggers>
</asp:UpdatePanel>

Servcer side as below,

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
        // Additional logic for handling the uploaded file
    }
}

protected void txtBasicValueGst18Per_TextChanged(object sender, EventArgs e)
{
    // Handle text changed event
}
3
Photo of Sandeep Kumar
826 689 69.4k Jun 06

Can i do this using Update Pannel id yes thn how

3
Photo of Dwarkesh Vajjala
768 833 117.1k Jun 05

It seems like you might be using .NET WebForms. Could you provide the fileName.aspx.cs file?

3
Photo of Sandeep Kumar
826 689 69.4k Jun 05

Any One help me

2
Photo of Sandeep Kumar
826 689 69.4k Jun 06

Simple I have Web form page (
aspx)   In Existing Page I have added file upload control where there were  many textbox  changedevent  and dropdown onselectindexchanged event fired but when Fileupload attaching a file then file getting cleared when textchanged event fired

Please Help m,e