ASP.NET C# Update Panel, File Upload Control and maintaining and saving information on postback

613 views Asked by At

I am designing a web page in asp.net C# named "myprofile". The structure is as follows from top to bottom: file upload control(for logo), drop down (for hotel type), a rich text box (for contact information - extra: bold, italics, hyperlink etc features), drop down (for location).

My problem is maintaining the values of file upload control and rich text box on postback. Also I am unable to insert the values in database as nothing is happening. I tried many things. Finally, I set my design as follows:

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
 <fieldset style="width:30%">
 <legend>Update Panel-1</legend>

 <br />(Type of Hotel)<br /><br />
 <asp:DropDownList ID="DropDownTypeOfHotel" runat="server" CssClass="cssdropdown"></asp:DropDownList>


 <br />(Contact Information Here)<br /><br />
 <asp:TextBox ID="RichTextBoxContactInfo" runat="server"></asp:TextBox>

 <br />(Location)<br /><br />
 <asp:DropDownList ID="DropDownListLocation" runat="server" CssClass="cssdropdown"></asp:DropDownList>


 </fieldset>
 </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
 <fieldset style="width:30%">
 <legend>Update Panel-2</legend>

 <asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label>
 <br /><br />
 <asp:Button ID="btnUpdate2" runat="server" Text="Update Both Panels" OnClick="btnUpdate2_Click" />

 </fieldset>
 </ContentTemplate>
</asp:UpdatePanel>

My code for the same lies here:-

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        loadHotelType();
    }
    Page.Form.Attributes.Add("enctype", "multipart/form-data");

}

protected void btnUpdate2_Click(object sender, EventArgs e)
{
    if (DropDownTypeOfHotel.SelectedIndex > 0)
    {            
            if (DropDownListLocation.SelectedIndex > 0)
            {
                if (!string.IsNullOrEmpty(RichTextBoxContactInfo.Text))
                {
                    Label1.Text = "Cool!";
                    insert();
                }
                else
                {
                    Label1.Text = "Kindly add contact info!";
                }
            }
            else
            {
                Label1.Text = "Kindly select location!";
            }
    }
    else
    {
        Label1.Text = "Kindly select type of hotel!";
    }
}


public void loadHotelType()
{
    DropDownTypeOfHotel.Items.Insert(0, "--Select Type of Hotel/Supplier--");
    DropDownTypeOfHotel.Items.Insert(1, "2 star");
    DropDownTypeOfHotel.Items.Insert(2, "3 star");
    DropDownTypeOfHotel.Items.Insert(3, "5 star");

    DropDownListLocation.Items.Insert(0, "--Select Location of Hotel/Supplier--");
    DropDownListLocation.Items.Insert(1, "Canada");
    DropDownListLocation.Items.Insert(2, "USA");
    DropDownListLocation.Items.Insert(3, "LA");
}

public void insert()
{
    int img_logo = 0;
    static byte[] btlogo_img;

    if (FileUpload1.PostedFile.ContentLength != 0)
    {
        btlogo_img = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile up1 = FileUpload1.PostedFile;
        up1.InputStream.Read(btlogo_img, 0, (int)FileUpload1.PostedFile.ContentLength);
        img_logo = 1;
    }

    if (img_logo == 1)
    {
        con1.Close();
        con1.Open();
        SqlCommand cmd = new SqlCommand("insert into file values('" + FileUpload1.PostedFile.FileName + "','" + btlogo_img + "')", con1);
        cmd.ExecuteNonQuery();
        con1.Close();
    }
    else
    {
        Label1.Text = "Kindly select logo!";
    }
}

Kindly suggest me with the same.

1

There are 1 answers

0
Karishma On

The easiest solution I found was to keep the controls, that loses value on postback, outside update panel and keep all other controls inside update panel.

It really worked for me!