更新页面时如何在数据库中保留现有文件名?

时间:2011-10-08 05:31:08

标签: c# asp.net sql

我有一个名为Update.aspx的页面,在其中我更新所有值而不是文件名。当我要更新信息时,所有值都从数据库中提取到相应的文本框。但是,如果我在数据库中有abc.jpg这样的文件名,并且我在没有浏览文件和更新的情况下进入更新页面,则会将其插入为null

即使用户没有更新,我如何在数据库中保留相同的文件(即abc.jpg)?

这是我的代码:

 string onlyname = string.Empty;
        string filename = "";
        string path = "";

        if (FileUp.HasFile)
        {
            filename = FileUp.PostedFile.FileName;
            path = Server.MapPath("/clients_images/") + FileUp.FileName;
            onlyname = path.Substring(path.LastIndexOf("\\") + 1);
            FileUp.SaveAs(path);
        }
        else
        {
            onlyname = dr["Client_Logo"].ToString().Trim();
        }

param[4] = new SqlParameter("@Client_Logo", SqlDbType.VarChar, 200);
        param[4].Value = onlyname.ToString().Trim();

 cmd.Parameters.Add(param[4]);

ASPX代码:

<table align="center" cellpadding="3" cellspacing="0" border="0" width="638" class="tableborder">
                                                                        <tr>
                                                                            <td colspan="2" class="mainbg" align="center" height="13">
                                                                                <font class="general">Update Client Detail</font></td>
                                                                        </tr>
                                                                        <tr>
                        <td class="general" width="50%" align="right">
                            <b>Client Name :</b></td>
                        <td align="left">
                            <asp:TextBox ID="txtUpdateclientname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
                            <font class="mandatory">*</font>
                        </td>
                    </tr>

                    <tr>
                        <td class="general" width="50%" align="right">
                            <b>Company Name :</b></td>
                        <td align="left">
                            <asp:TextBox ID="updatecompanyname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
                            <font class="mandatory">*</font>
                        </td>
                    </tr>

                    <tr>
                        <td class="general" width="50%" align="right">
                            <b>Email :</b></td>
                        <td align="left">
                            <asp:TextBox ID="updateemail" Text="" Columns = "30" CssClass="checkbox02" runat="server"></asp:TextBox>
                            <font class="mandatory">*</font>
                        </td>
                    </tr>
                     <tr>
                        <td class="general" width="50%" valign="top" align="right">
                            <b>Logo :</b></td>
                        <td class="general" width="50%" align="left">   
                             <asp:FileUpload ID="FileUp" runat="server" CssClass="checkbox02" />

                           <br />  (Height of image should not be more than 67 And Width of image should not be more than 380)

                        </td>
                    </tr>
                    <tr>
                            <td class="general" width="50%" align="right">
                            <%--<b><font class="bluetext"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Upload&nbsp;: </strong>--%></td>
                            <td align="left">
                                <asp:LinkButton ID="viewImage" Text="View Existing" runat="server" cssClass="general-white" class="link" OnCommand="LinkButton_Command" CommandName="downloadfile" ToolTip="Click to View Existing" Font-Bold="true"></asp:LinkButton>
                            </td>
                 </tr>
                    <tr>
                        <td class="general" width="50%" align="right" valign="top">
                            <b>Project Type :</b></td>
                            <td width="100%" align="left" valign="top" colspan="2">
                                                                                <table cellpadding="0" cellspacing="0" border="0">


                                                                                    <tr>
                                                                                        <td align="right" valign="top" colspan="3">
                                                                                            <asp:ListBox ID="LstUserLeft" Width="100%" Autoscroll="false" SelectionMode="Multiple" Height="150" CssClass="checkbox02"
                                                                                                runat="server">

                                                                                                </asp:ListBox>
                                                                                        </td>
    </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>

                    </tr>



                                                                    </table>

1 个答案:

答案 0 :(得分:0)

您可以使用HiddenFieldViewState来存储Client_logo字段(来自数据库)的文件名。

例如。 在检索结果时将值分配给HiddenField1

HiddenField1.Value=dr["Client_Logo"].ToString().Trim();

并在更新时

if (FileUp.HasFile)
 {
  ...
 }
else
 {
 onlyname = HiddenField1.Value;
 }