在Listview asp.net中使用fileupload上传图像

时间:2013-10-04 21:55:14

标签: asp.net listview file-upload

我正在与球员和篮球队一起创建一个项目表格。我使用listview(asp.net)。

我想在edititeminsertitem上发布一个上传活动。我想上传图片 ~/images/,文件名为"SURNAME" (Column from databse) + ".jpg。 只有那个

来自players.asp文件的我的ASP代码是:

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID,IDOTEAM,IDOCOUNTRY" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">
        <AlternatingItemTemplate>
            <tr style="background-color: #fff7f1; color: #800000;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Διαγραφή" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Επεξεργασία" />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="NOLabel" runat="server" Text='<%# Eval("NO") %>' />
                </td>
                <td>
                    <asp:Label ID="SURNAMELabel" runat="server" Text='<%# Eval("SURNAME") %>' />
                </td>
                <td>
                    <asp:Label ID="FIRSTNAMELabel" runat="server" Text='<%# Eval("FIRSTNAME") %>' />
                </td>
                <td>
                    <asp:Label ID="POSITIONLabel" runat="server" Text='<%# Eval("POSITION") %>' />
                </td>
                <td>
                    <asp:Label ID="HEIGHTLabel" runat="server" Text='<%# Eval("HEIGHT") %>' />
                </td>
                <td>
                    <asp:Label ID="BORNLabel" runat="server" Text='<%# Eval("BORN") %>' />
                </td>
                <td>
                    <asp:Image ID="ImagePlayer" runat="server" Height="100px" Width="100px" ImageUrl='<%# "images/players/" + Eval("IMAGE") %>' />
                </td>                    
                <td>
                    <asp:Label ID="TEAMLabel" runat="server" Text='<%# Eval("TEAM") %>' />
                </td>

                <td>
                    <asp:Label ID="COUNTRYLabel" runat="server" Text='<%# Eval("COUNTRY") %>' />
                </td>

            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="background-color: #999999;">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" ValidationGroup="EDIT" CommandName="Update" Text="Αλλαγή" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Ακύρωση" />
                </td>
                <td>
                    <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:DropDownList ID="DropDownListEditNO" runat="server" DataSource='<%# Enumerable.Range(0, 100) %>' SelectedValue='<%# Bind("NO") %>'>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="SURNAMETextBox" runat="server" Text='<%# Bind("SURNAME") %>' />
                    <asp:RequiredFieldValidator ID="EditSurenameValidate" runat="server" ValidationGroup="EDIT" ErrorMessage="Το πεδίο SURENAME είναι υποχρεωτικό"
                        ControlToValidate="SURNAMETextBox" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:TextBox ID="FIRSTNAMETextBox" runat="server" Text='<%# Bind("FIRSTNAME") %>' />
                    <asp:RequiredFieldValidator ID="EditFirstnameValidate" runat="server" ValidationGroup="EDIT" ErrorMessage="Το πεδίο FIRSTNAME είναι υποχρεωτικό"
                        ControlToValidate="FIRSTNAMETextBox" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("POSITION") %>'>
                        <asp:ListItem>Guard</asp:ListItem>
                        <asp:ListItem>Forward</asp:ListItem>
                        <asp:ListItem>Center</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownListHeight" runat="server" DataSource='<%# Enumerable.Range(130, 120).Select(x => x / 100f) %>' SelectedValue='<%# Bind("HEIGHT") %>'>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownListBorn" runat="server" DataSource='<%# Enumerable.Range(1910, 100) %>' SelectedValue='<%# Bind("BORN") %>'>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList7" runat="server"
                        DataSourceID="SqlDataSource3"
                        DataTextField="NAME" DataValueField="ID" SelectedValue='<%# Bind("TEAM_ID") %>'>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList4" runat="server"
                        DataSourceID="SqlDataSource2"
                        DataTextField="NAME" DataValueField="ID" SelectedValue='<%# Bind("COUNTRY_ID") %>'>
                    </asp:DropDownList>

                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;">
                <tr>
                    <td>No data was returned.</td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" ValidationGroup="INSERT" CommandName="Insert" Text="Εισαγωγή" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Καθαρισμός" />
                </td>
                <td>&nbsp;</td>
                <td>
                    <asp:DropDownList ID="DropDownListInsNo" runat="server" AppendDataBoundItems="true" DataSource='<%# Enumerable.Range(0, 100) %>' SelectedValue='<%# Bind("NO") %>'>
                        <asp:ListItem>ΕΠΙΛΕΞΤΕ ΝΟΥΜΕΡΟ</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="InsNoValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Παρακαλώ Επιλέξτε Νούμερο"
                        ControlToValidate="DropDownListInsNo" Text="*" InitialValue="ΕΠΙΛΕΞΤΕ ΝΟΥΜΕΡΟ" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:TextBox ID="SURENAMETextBox" runat="server" Text='<%# Bind("SURNAME") %>' />
                    <asp:RequiredFieldValidator ID="InsertSurenameValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Το πεδίο SURENAME είναι υποχρεωτικό"
                        ControlToValidate="SURENAMETextBox" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:TextBox ID="FIRSTNAMETextBox" runat="server" Text='<%# Bind("FIRSTNAME") %>' />
                    <asp:RequiredFieldValidator ID="InsertFirstnameValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Το πεδίο FIRSTNAME είναι υποχρεωτικό"
                        ControlToValidate="FIRSTNAMETextBox" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownListposIns" runat="server" SelectedValue='<%# Bind("POSITION") %>'>
                        <asp:ListItem>ΕΠΙΛΕΞΤΕ ΘΕΣΗ</asp:ListItem>
                        <asp:ListItem>Guard</asp:ListItem>
                        <asp:ListItem>Forward</asp:ListItem>
                        <asp:ListItem>Center</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="InsertPositionValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Παρακαλώ Επιλέξτε Θέση"
                        ControlToValidate="DropDownListposIns" Text="*" InitialValue="ΕΠΙΛΕΞΤΕ ΘΕΣΗ" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownListInsertHeight" runat="server" AppendDataBoundItems="true" DataSource='<%# Enumerable.Range(130 , 120).Select(x => x / 100f) %>' SelectedValue='<%# Bind("HEIGHT") %>'>
                        <asp:ListItem>ΕΠΙΛΕΞΤΕ ΥΨΟΣ</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="InsertHeightValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Παρακαλώ Επιλέξτε Ύψος"
                        ControlToValidate="DropDownListInsertHeight" Text="*" InitialValue="ΕΠΙΛΕΞΤΕ ΥΨΟΣ" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownListInsertBorn" runat="server" AppendDataBoundItems="true" DataSource='<%# Enumerable.Range(1910, 100) %>' SelectedValue='<%# Bind("BORN") %>'>
                        <asp:ListItem>ΕΠΙΛΕΞΤΕ ΕΤΟΣ ΓΕΝΝΗΣΗΣ</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="InsertBornValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Παρακαλώ Επιλέξτε Έτος Γέννησης"
                        ControlToValidate="DropDownListInsertBorn" Text="*" InitialValue="ΕΠΙΛΕΞΤΕ ΕΤΟΣ ΓΕΝΝΗΣΗΣ" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload2" runat="server" />
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList6" runat="server"
                        DataSourceID="SqlDataSource3"
                        DataTextField="NAME" DataValueField="ID" SelectedValue='<%# Bind("TEAM_ID") %>' AppendDataBoundItems="true">
                        <asp:ListItem Text="ΕΠΙΛΕΞΤΕ ΟΜΑΔΑ" />
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="InsertTeam_idValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Παρακαλώ Επιλέξτε Ομάδα"
                        ControlToValidate="DropDownList6" Text="*" InitialValue="ΕΠΙΛΕΞΤΕ ΟΜΑΔΑ" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList5" runat="server"
                        DataSourceID="SqlDataSource2"
                        DataTextField="NAME" DataValueField="ID" SelectedValue='<%# Bind("COUNTRY_ID") %>' AppendDataBoundItems="True">
                        <asp:ListItem Text="ΕΠΙΛΕΞΤΕ ΧΩΡΑ" />
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="InsertCountry_IDValidate" runat="server" ValidationGroup="INSERT" ErrorMessage="Παρακαλώ Επιλέξτε Χώρα"
                        ControlToValidate="DropDownList5" Text="*" InitialValue="ΕΠΙΛΕΞΤΕ ΧΩΡΑ" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr style="background-color: #f8c083; color: #333333;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Διαγραφή" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Επεξεργασία" />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="NOLabel" runat="server" Text='<%# Eval("NO") %>' />
                </td>
                <td>
                    <asp:Label ID="SURNAMELabel" runat="server" Text='<%# Eval("SURNAME") %>' />
                </td>
                <td>
                    <asp:Label ID="FIRSTNAMELabel" runat="server" Text='<%# Eval("FIRSTNAME") %>' />
                </td>
                <td>
                    <asp:Label ID="POSITIONLabel" runat="server" Text='<%# Eval("POSITION") %>' />
                </td>
                <td>
                    <asp:Label ID="HEIGHTLabel" runat="server" Text='<%# Eval("HEIGHT") %>' />
                </td>
                <td>
                    <asp:Label ID="BORNLabel" runat="server" Text='<%# Eval("BORN") %>' />
                </td>
                <td>
                    <asp:Image ID="ImagePlayer" runat="server" Height="100px" Width="100px" ImageUrl='<%# "images/players/" + Eval("IMAGE") %>' />
                </td>
                <td>
                    <asp:Label ID="TEAMLabel" runat="server" Text='<%# Eval("TEAM") %>' />
                </td>
                <td>
                    <asp:Label ID="COUNTRYLabel" runat="server" Text='<%# Eval("COUNTRY") %>' />
                </td>

            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;">
                            <tr runat="server" style="background-color: #DCDCDC; color: #333333;">
                                <th runat="server"></th>
                                <th runat="server">ID</th>
                                <th runat="server">NO</th>
                                <th runat="server">SURNAME</th>
                                <th runat="server">FIRSTNAME</th>
                                <th runat="server">POSITION</th>
                                <th runat="server">HEIGHT</th>
                                <th runat="server">BORN</th>
                                <th runat="server">IMAGE</th>
                                <th runat="server">TEAM</th>
                                <th runat="server">COUNTRY</th>
                            </tr>
                            <tr id="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style="text-align: center; background-color: #5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif; color: #FFFFFF">
                        <asp:DataPager ID="DataPager1" runat="server">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                <asp:NumericPagerField />
                                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <SelectedItemTemplate>
            <tr style="background-color: #E2DED6; font-weight: bold; color: #333333;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Διαγραφή" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Επεξεργασία" />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="NOLabel" runat="server" Text='<%# Eval("NO") %>' />
                </td>
                <td>
                    <asp:Label ID="SURNAMELabel" runat="server" Text='<%# Eval("SURNAME") %>' />
                </td>
                <td>
                    <asp:Label ID="FIRSTNAMELabel" runat="server" Text='<%# Eval("FIRSTNAME") %>' />
                </td>
                <td>
                    <asp:Label ID="POSITIONLabel" runat="server" Text='<%# Eval("POSITION") %>' />
                </td>
                <td>
                    <asp:Label ID="HEIGHTLabel" runat="server" Text='<%# Eval("HEIGHT") %>' />
                </td>
                <td>
                    <asp:Label ID="BORNLabel" runat="server" Text='<%# Eval("BORN") %>' />
                </td>
                <td>
                    <asp:Label ID="IMAGELabel" runat="server" Text='<%# Eval("IMAGE") %>' />
                </td>
                <td>
                    <asp:Label ID="TEAMLabel" runat="server" Text='<%# Eval("TEAM") %>' />
                </td>
                <td>
                    <asp:Label ID="COUNTRYLabel" runat="server" Text='<%# Eval("COUNTRY") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>
    <asp:ValidationSummary ID="ValidationSummaryEdit" ValidationGroup="EDIT" ForeColor="Red" runat="server" />
    <asp:ValidationSummary ID="ValidationSummaryInsert" ValidationGroup="INSERT" ForeColor="Red" runat="server" />
    <asp:Label ID="ErrorLabel1" runat="server"></asp:Label>
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BasketballConnectionString1 %>" DeleteCommand="DELETE FROM [PLAYERS] WHERE [ID] = @ID" InsertCommand="INSERT INTO [PLAYERS] ([NO], [SURNAME], [FIRSTNAME], [POSITION], [TEAM_ID], [COUNTRY_ID], [HEIGHT], [BORN], [IMAGE]) VALUES (@NO, @SURNAME, @FIRSTNAME, @POSITION, @TEAM_ID, @COUNTRY_ID, @HEIGHT, @BORN, (@FIRSTNAME + '-' + @SURNAME + '.jpg'))" SelectCommand="SELECT PLAYERS.ID, PLAYERS.NO, PLAYERS.SURNAME, PLAYERS.FIRSTNAME, PLAYERS.POSITION, PLAYERS.TEAM_ID, PLAYERS.COUNTRY_ID, PLAYERS.HEIGHT, PLAYERS.BORN, PLAYERS.IMAGE, TEAMS.ID AS IDOTEAM, TEAMS.NAME AS TEAM, COUNTRY.ID AS IDOCOUNTRY, COUNTRY.NAME AS COUNTRY FROM PLAYERS INNER JOIN TEAMS ON PLAYERS.TEAM_ID = TEAMS.ID INNER JOIN COUNTRY ON PLAYERS.COUNTRY_ID = COUNTRY.ID ORDER BY TEAM_ID" UpdateCommand="UPDATE [PLAYERS] SET [NO] = @NO, [SURNAME] = @SURNAME, [FIRSTNAME] = @FIRSTNAME, [POSITION] = @POSITION, [TEAM_ID] = @TEAM_ID, [COUNTRY_ID] = @COUNTRY_ID, [HEIGHT] = @HEIGHT, [BORN] = @BORN, [IMAGE] = (@FIRSTNAME + '-' + @SURNAME +'.jpg') WHERE [ID] = @ID">

        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="NO" Type="Int32" />
            <asp:Parameter Name="SURNAME" Type="String" />
            <asp:Parameter Name="FIRSTNAME" Type="String" />
            <asp:Parameter Name="POSITION" Type="String" />
            <asp:Parameter Name="TEAM_ID" Type="Int32" />
            <asp:Parameter Name="COUNTRY_ID" Type="Int32" />
            <asp:Parameter Name="HEIGHT" Type="Double" />
            <asp:Parameter Name="BORN" Type="Int32" />
            <asp:Parameter Name="IMAGE" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="NO" Type="Int32" />
            <asp:Parameter Name="SURNAME" Type="String" />
            <asp:Parameter Name="FIRSTNAME" Type="String" />
            <asp:Parameter Name="POSITION" Type="String" />
            <asp:Parameter Name="TEAM_ID" Type="Int32" />
            <asp:Parameter Name="COUNTRY_ID" Type="Int32" />
            <asp:Parameter Name="HEIGHT" Type="Double" />
            <asp:Parameter Name="BORN" Type="Int32" />
            <asp:Parameter Name="IMAGE" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:BasketballConnectionString2 %>" DeleteCommand="DELETE FROM [TEAMS] WHERE [ID] = @ID" InsertCommand="INSERT INTO [TEAMS] ([NAME], [COUNTRY_ID], [CITY], [ARENA_ID], [PRESIDENT], [LOGO]) VALUES (@NAME, @COUNTRY_ID, @CITY, @ARENA_ID, @PRESIDENT, @LOGO)" SelectCommand="SELECT [ID], [NAME], [COUNTRY_ID], [CITY], [ARENA_ID], [PRESIDENT], [LOGO] FROM [TEAMS]" UpdateCommand="UPDATE [TEAMS] SET [NAME] = @NAME, [COUNTRY_ID] = @COUNTRY_ID, [CITY] = @CITY, [ARENA_ID] = @ARENA_ID, [PRESIDENT] = @PRESIDENT, [LOGO] = @LOGO WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="NAME" Type="String" />
            <asp:Parameter Name="COUNTRY_ID" Type="Int32" />
            <asp:Parameter Name="CITY" Type="String" />
            <asp:Parameter Name="ARENA_ID" Type="Int32" />
            <asp:Parameter Name="PRESIDENT" Type="String" />
            <asp:Parameter Name="LOGO" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="NAME" Type="String" />
            <asp:Parameter Name="COUNTRY_ID" Type="Int32" />
            <asp:Parameter Name="CITY" Type="String" />
            <asp:Parameter Name="ARENA_ID" Type="Int32" />
            <asp:Parameter Name="PRESIDENT" Type="String" />
            <asp:Parameter Name="LOGO" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BasketballConnectionString2 %>" DeleteCommand="DELETE FROM [COUNTRY] WHERE [ID] = @ID" InsertCommand="INSERT INTO [COUNTRY] ([NAME]) VALUES (@NAME)" SelectCommand="SELECT [ID], [NAME] FROM [COUNTRY]" UpdateCommand="UPDATE [COUNTRY] SET [NAME] = @NAME WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="NAME" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="NAME" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

我在文件players.cs中的cs代码是:

enter code here

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AddPlayers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

protected void ListView1_ItemCommand(object sender,ListViewInsertEventArgs e)
{


                FileUpload fu2 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload2");
                if (fu2.HasFile)
                {
                    string aut = "pol.jpg";
                    fu2.SaveAs(Server.MapPath("~/images/" + aut.ToString()  ));
                    ErrorLabel1.Text = "ok";

                }
                else
                {
                                ErrorLabel1.Text = "no";

                }


    }

}

fileuploader无法正常工作。我不想获取上传的每个图像的文件名。我只想获取"SURNAME"字符串并将文件另存为"SURNAME" +".jpg" 非常感谢你

2 个答案:

答案 0 :(得分:2)

我会在标记中附加ItemCommand事件:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID,IDOTEAM,IDOCOUNTRY" 
        DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" 
        OnItemCommand="ListView1_ItemCommand" >

在代码中: (已编辑以反映所请求的更改)

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "Update")
    {
        TextBox txtSurName = (TextBox)ListView1.InsertItem.FindControl("SURNAMETextBox");
        TextBox txtFirstName = (TextBox)ListView1.InsertItem.FindControl("FIRSTNAMETextBox");
        FileUpload fu1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        if (fu1.HasFile)
        {
            string aut = String.Format("{0}{1}{3}", txtFirstName.Text, txtFirstName.Text, ".jpg");
            fu1.SaveAs(Server.MapPath("~/images/" + aut));
            ErrorLabel1.Text = "ok";

        }
        else
        {
            ErrorLabel1.Text = "no";

        }

    }
    else if (e.CommandName == "INSERT")
    {
        TextBox txtSurName = (TextBox)ListView1.InsertItem.FindControl("SURENAMETextBox");
        TextBox txtFirstName = (TextBox)ListView1.InsertItem.FindControl("TextBox1");
        FileUpload fu2 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload2");
        if (fu2.HasFile)
        {
            string aut = String.Format("{0}{1}{3}", txtFirstName.Text, txtFirstName.Text, ".jpg");
            fu2.SaveAs(Server.MapPath("~/images/" + aut));
            ErrorLabel1.Text = "ok";

        }
        else
        {
            ErrorLabel1.Text = "no";

        }
    }
}

我还强烈建议遵循一些命名对话:What are the naming guidelines for ASP.NET controls?

答案 1 :(得分:0)

此代码有错误。您必须确保在处理EditItem时必须使用

TextBox txtSurName = (TextBox)ListView1.EditItem.FindControl("SURENAMETextBox");

不是

TextBox txtSurName = (TextBox)ListView1.InsertItem.FindControl("SURENAMETextBox");