帮助在详细信息视图中使用文件上载

时间:2011-01-04 20:48:21

标签: asp.net vb.net insert detailsview

我正在尝试使用fileupload,在此页面上向gridview添加行。这个页面基本上是一个简单的管理面板,我可以将数据添加到我的数据库。文件上载用于获取文件的路径,并将路径上传到数据库而不是文件。问题是每当我运行我的程序选择一个文件,它上传很好,期望路径显示为真而不是路径。我真的很感谢帮助这是我的整个代码。 gridview和details视图都使用相同的sqladapater

alt text

   <form id="form1" runat="server">


        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>MSD</asp:ListItem>
            <asp:ListItem>OPS</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button3" runat="server" Text="Department" />
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="New" />&nbsp;<asp:Button
                        ID="Button4" runat="server" Text="Cancel" />&nbsp;
                     <br />
        <br />
        <div>
          <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

                <ContentTemplate>
                     <center>   <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
                        DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="128px" Width="544px" Visible="False"  CellPadding="4" ForeColor="#333333" GridLines="None" OnModeChanged="Button1_Click"  >
                        <Fields>
                            <asp:TemplateField HeaderText="Order" SortExpression="Order">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Order") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <InsertItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Order") %>'></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Order") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Department" SortExpression="Department">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Department") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <InsertItemTemplate>
                                    <asp:TextBox ID="textboxdepartment" runat="server" Text='<%# Bind("Department") %>'></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="DOC_Type" SortExpression="DOC_Type">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("DOC_Type") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <InsertItemTemplate>
                                    <asp:TextBox ID="textboxtype" runat="server" Text='<%# Bind("DOC_Type") %>'></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("DOC_Type") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                            <asp:TemplateField HeaderText="Revision" SortExpression="Revision">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Revision") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <InsertItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Revision") %>'></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="textboxrevision" runat="server" Text='<%# Bind("Revision") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="DOC">
                                <EditItemTemplate>
                                    <asp:TextBox ID="textboxdate" runat="server" Text='<%# Bind("DOC") %>'></asp:TextBox>
                                </EditItemTemplate>

                                  <InsertItemTemplate>
        <asp:FileUpload ID="FileUploadDOC" runat="server" Width="384px" Enabled='<%# Bind("DOC") %>'  />
    </InsertItemTemplate>


                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("DOC") %>'></asp:Label>
                                </ItemTemplate>


                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Active" SortExpression="Active">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <InsertItemTemplate>
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("Active") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Rev_Date" SortExpression="Rev_Date">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Rev_Date") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <InsertItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Rev_Date") %>'></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Rev_Date") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                                SortExpression="ID" Visible="False" />
                            <asp:CommandField ShowInsertButton="True" ShowCancelButton="False" />
                        </Fields>
                        <FooterStyle BackColor="#5D7B9D" BorderStyle="None" Font-Bold="True" ForeColor="White" />
                        <CommandRowStyle BackColor="#E2DED6" BorderStyle="None" Font-Bold="True" />
                        <RowStyle BackColor="#F7F6F3" BorderStyle="None" ForeColor="#333333" />
                        <FieldHeaderStyle BackColor="#E9ECF1" BorderStyle="None" Font-Bold="True" />
                        <EmptyDataRowStyle BorderStyle="None" />
                        <PagerStyle BackColor="#284775" BorderStyle="None" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#5D7B9D" BorderStyle="None" Font-Bold="True" ForeColor="White" />
                        <InsertRowStyle BorderStyle="None" />
                        <EditRowStyle BackColor="#999999" BorderStyle="None" />
                        <AlternatingRowStyle BackColor="White" BorderStyle="None" ForeColor="#284775" />
                    </asp:DetailsView>
                         &nbsp;
                    <br />
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                        DataSourceID="SqlDataSource1">
                        <Columns>
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                            <asp:BoundField DataField="Order" HeaderText="Order" SortExpression="Order" />
                            <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
                            <asp:BoundField DataField="DOC_Type" HeaderText="DOC_Type" SortExpression="DOC_Type" />
                            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                            <asp:BoundField DataField="Revision" HeaderText="Revision" SortExpression="Revision" />
                            <asp:BoundField DataField="DOC" HeaderText="DOC" SortExpression="DOC" />
                            <asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" />
                            <asp:BoundField DataField="Rev_Date" HeaderText="Rev_Date" SortExpression="Rev_Date" />
                            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                                SortExpression="ID" Visible="False" />
                        </Columns>
                    </asp:GridView>
                                      <br />

                    &nbsp;</ContentTemplate>
            </asp:UpdatePanel>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CABCIntranetConnectionString %>"
                        DeleteCommand="DELETE FROM [sop] WHERE [ID] = @original_ID AND (([Order] = @original_Order) OR ([Order] IS NULL AND @original_Order IS NULL)) AND (([Department] = @original_Department) OR ([Department] IS NULL AND @original_Department IS NULL)) AND (([DOC Type] = @original_DOC_Type) OR ([DOC Type] IS NULL AND @original_DOC_Type IS NULL)) AND (([Title] = @original_Title) OR ([Title] IS NULL AND @original_Title IS NULL)) AND (([Revision] = @original_Revision) OR ([Revision] IS NULL AND @original_Revision IS NULL)) AND (([DOC] = @original_DOC) OR ([DOC] IS NULL AND @original_DOC IS NULL)) AND (([Rev Date] = @original_Rev_Date) OR ([Rev Date] IS NULL AND @original_Rev_Date IS NULL)) AND (([Active] = @original_Active) OR ([Active] IS NULL AND @original_Active IS NULL))" InsertCommand="INSERT INTO [sop] ([Order], [Department], [DOC Type], [Title], [Revision], [DOC], [Rev Date], [Active]) VALUES (@Order, @Department, @DOC_Type, @Title, @Revision, @DOC, @Rev_Date, @Active)"
                        SelectCommand="SELECT [Order], [Department], [DOC Type] AS DOC_Type, [Title], [Revision], [DOC], [Rev Date] AS Rev_Date, [Active], [ID] FROM [sop] WHERE ([Department] = @Department) ORDER BY [Order]" UpdateCommand="UPDATE [sop] SET [Order] = @Order, [Department] = @Department, [DOC Type] = @DOC_Type, [Title] = @Title, [Revision] = @Revision, [DOC] = @DOC, [Rev Date] = @Rev_Date, [Active] = @Active WHERE [ID] = @original_ID AND (([Order] = @original_Order) OR ([Order] IS NULL AND @original_Order IS NULL)) AND (([Department] = @original_Department) OR ([Department] IS NULL AND @original_Department IS NULL)) AND (([DOC Type] = @original_DOC_Type) OR ([DOC Type] IS NULL AND @original_DOC_Type IS NULL)) AND (([Title] = @original_Title) OR ([Title] IS NULL AND @original_Title IS NULL)) AND (([Revision] = @original_Revision) OR ([Revision] IS NULL AND @original_Revision IS NULL)) AND (([DOC] = @original_DOC) OR ([DOC] IS NULL AND @original_DOC IS NULL)) AND (([Rev Date] = @original_Rev_Date) OR ([Rev Date] IS NULL AND @original_Rev_Date IS NULL)) AND (([Active] = @original_Active) OR ([Active] IS NULL AND @original_Active IS NULL))" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
                        <DeleteParameters>
                            <asp:Parameter Name="original_ID" Type="Int32" />
                            <asp:Parameter Name="original_Order" Type="Int32" />
                            <asp:Parameter Name="original_Department" Type="String" />
                            <asp:Parameter Name="original_DOC_Type" Type="String" />
                            <asp:Parameter Name="original_Title" Type="String" />
                            <asp:Parameter Name="original_Revision" Type="Int32" />
                            <asp:Parameter Name="original_DOC" Type="String" />
                            <asp:Parameter Name="original_Rev_Date" Type="String" />
                            <asp:Parameter Name="original_Active" Type="String" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="Order" Type="Int32" />
                            <asp:Parameter Name="Department" Type="String" />
                            <asp:Parameter Name="DOC_Type" Type="String" />
                            <asp:Parameter Name="Title" Type="String" />
                            <asp:Parameter Name="Revision" Type="Int32" />
                            <asp:Parameter Name="DOC" Type="String" />
                            <asp:Parameter Name="Rev_Date" Type="String" />
                            <asp:Parameter Name="Active" Type="String" />
                            <asp:Parameter Name="original_ID" Type="Int32" />
                            <asp:Parameter Name="original_Order" Type="Int32" />
                            <asp:Parameter Name="original_Department" Type="String" />
                            <asp:Parameter Name="original_DOC_Type" Type="String" />
                            <asp:Parameter Name="original_Title" Type="String" />
                            <asp:Parameter Name="original_Revision" Type="Int32" />
                            <asp:Parameter Name="original_DOC" Type="String" />
                            <asp:Parameter Name="original_Rev_Date" Type="String" />
                            <asp:Parameter Name="original_Active" Type="String" />
                        </UpdateParameters>
                        <InsertParameters>
                            <asp:Parameter Name="Order" Type="Int32" />
                            <asp:Parameter Name="Department" Type="String" />
                            <asp:Parameter Name="DOC_Type" Type="String" />
                            <asp:Parameter Name="Title" Type="String" />
                            <asp:Parameter Name="Revision" Type="Int32" />
                            <asp:Parameter Name="DOC" Type="String" />
                            <asp:Parameter Name="Rev_Date" Type="String" />
                            <asp:Parameter Name="Active" Type="String" />
                        </InsertParameters>
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DropDownList1" DefaultValue="MSD" Name="Department"
                                PropertyName="SelectedValue" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
        </div>
    </form>
</body>
</html>




Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        DetailsView1.Visible = True
        DetailsView1.ChangeMode(DetailsViewMode.Insert)


        Dim tbox As TextBox = TryCast(DetailsView1.FindControl("textbox1"), TextBox)
        tbox.Text = DateTime.Now.ToShortDateString()



        Dim tbox1 As TextBox = TryCast(DetailsView1.FindControl("textboxdepartment"), TextBox)
        tbox1.Text = DropDownList1.SelectedItem.Text




        Dim tbox2 As TextBox = TryCast(DetailsView1.FindControl("textbox6"), TextBox)
        tbox2.Text = "True"




        Dim tbox3 As TextBox = TryCast(DetailsView1.FindControl("textboxtype"), TextBox)
        tbox3.Text = "S.O.P"


        Dim tbox4 As TextBox = TryCast(DetailsView1.FindControl("textbox5"), TextBox)
        tbox4.Text = "1"


        Dim tbox5 As TextBox = TryCast(DetailsView1.FindControl("textbox2"), TextBox)
        tbox5.Text = GridView1.Rows.Count + 1



    End Sub
    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        UpdatePanel1.Update()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        DetailsView1.Visible = False
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

您希望这样做的方式在最近的浏览器中不起作用 - 它们在您上传文件时不会发送完整的客户端路径信息。只有早期版本的Internet Explorer和其他一些版本才能执行此操作。

似乎有效的一种方法是使用Java applet来选择文件而不是本机文件上传小部件。由于您只需要文件的路径而不是实际文件本身,这是一个更好的解决方案(无需将文件上传到服务器)。

这是a link讨论和链接到这个工作的演示。请阅读评论。您将不得不查看演示页面的源代码(以及链接的js文件的来源),以了解如何使其工作。

如果该页面消失,我将复制以下相关链接: