ASP.NET C#更新面板,文件上载控制以及维护和保存回发信息

时间:2015-06-11 10:09:42

标签: c# asp.net ajax updatepanel

我正在设计一个名为“myprofile”的asp.net C#中的网页。结构如下从上到下:文件上传控件(用于标识),下拉(用于酒店类型),富文本框(用于联系信息 - 额外:粗体,斜体,超链接等功能),下拉(用于位置)。

我的问题是在回发时保持文件上传控件和富文本框的值。此外,我无法在数据库中插入值,因为没有发生任何事情。我尝试了很多东西。最后,我将设计设置如下:

<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>

我的代码就在这里: -

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!";
    }
}

请建议我同样的。

1 个答案:

答案 0 :(得分:0)

我找到的最简单的解决方案是保留控件,在回发时丢失值,在更新面板之外,并将所有其他控件保留在更新面板中。

这真的对我有用!