求教关于ASP.NET(C#)DetailView控件添加FileUpload控件实现图片上传的问题
我在DetailsView里启用了【插入】功能,并添加了一个ImageField然后将它转换成模板,
再在它的InsertItemTemplate里面添加了一个FileUpload控件和一个Button按钮。
我想实现的是当我点击Button按钮时上传选择的图片,并把上传图片的路径(固定为:【~/UserImage/】)
和图片名,即以【~/UserImage/22.jpg】格式赋值到UserImage所对应的TextBox中,
(DetailsView里面当点击新建时弹出的那个插入页面里面各项对应的都是TextBox控件),当点击自带的插入时保存到数据库中。
这个功能该怎么做啊?
我的数据库字段是:Id(主键 int 自增) , UserName , UserImage(存储图片的路径和图片名,即【~/UserImage/22.jpg】格式), Telephone
我的代码如下:
--------------------编程问答-------------------- 上面的问题我找到怎么读取值了 但是怎么才能把值写会去呢?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="148px" Width="387px">
<Fields>
<asp:TemplateField HeaderText="照片显示" ShowHeader="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("UserImage") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:Label ID="Label1" runat="server" Text="插入照片:"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("UserImage") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="UserImage" HeaderText="UserImage"
SortExpression="UserImage" />
<asp:BoundField DataField="Telephone" HeaderText="Telephone"
SortExpression="Telephone" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Users] WHERE [Id] = @original_Id AND (([UserName] = @original_UserName) OR ([UserName] IS NULL AND @original_UserName IS NULL)) AND (([UserImage] = @original_UserImage) OR ([UserImage] IS NULL AND @original_UserImage IS NULL)) AND (([Telephone] = @original_Telephone) OR ([Telephone] IS NULL AND @original_Telephone IS NULL))"
InsertCommand="INSERT INTO [Users] ([UserName], [UserImage], [Telephone]) VALUES (@UserName, @UserImage, @Telephone)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [Users]"
UpdateCommand="UPDATE [Users] SET [UserName] = @UserName, [UserImage] = @UserImage, [Telephone] = @Telephone WHERE [Id] = @original_Id AND (([UserName] = @original_UserName) OR ([UserName] IS NULL AND @original_UserName IS NULL)) AND (([UserImage] = @original_UserImage) OR ([UserImage] IS NULL AND @original_UserImage IS NULL)) AND (([Telephone] = @original_Telephone) OR ([Telephone] IS NULL AND @original_Telephone IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_UserName" Type="String" />
<asp:Parameter Name="original_UserImage" Type="String" />
<asp:Parameter Name="original_Telephone" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="UserImage" Type="String" />
<asp:Parameter Name="Telephone" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="UserImage" Type="String" />
<asp:Parameter Name="Telephone" Type="String" />
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_UserName" Type="String" />
<asp:Parameter Name="original_UserImage" Type="String" />
<asp:Parameter Name="original_Telephone" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
--------------------编程问答-------------------- 这个问题解决了吗 我现在也面临这个问题呢 我的后台管理商品界面上用的detailview绑定的数据,但是用的插入功能 怎么能够结合用fileupload上传图片?我现在是手动修改路径而不是选择路径
protected void Button1_Click(object sender, EventArgs e)
{
string ii = (DetailsView1.FindControl("Label1") as Label).Text;
string ii2 = (DetailsView1.FindControl("FileUpload1") as FileUpload).FileName;
ii = "~/UserImage/"+ii2;
string ii3 = (DetailsView1.FindControl("Label2") as Label).Text;
ii3 = ii;
}
补充:.NET技术 , ASP.NET