在Ajax Modal弹出扩展器中使用fileupload控件

时间:2010-02-05 10:25:17

标签: c# asp.net

我有一个GridView控件,显示名称,注释,图像列。当我点击图像时,我需要打开一个模态弹出扩展器,它将被分配给一个包含文本框,文件上载控件,上传和取消按钮的面板。

问题是,当我按下更新按钮button_click事件时,我无法在FileUpload控件中找到文件,因为我可以在文本框中找到任何帮助。我的代码如下:

<asp:GridView ID="gdvMngTeam" runat="server" 
    CellPadding="4" 
    AutoGenerateColumns="False"
    ForeColor="#333333" 
    GridLines="None" 
    Width="100%" 
    DataMember="ID" 
    OnRowCommand="gdvMngTeam_RowCommand">
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#EFF3FB" Font-Names="Verdana" Font-Size="Small" />
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" Visible="False" />
        <asp:BoundField HeaderText="Name" DataField="Name" />
        <asp:BoundField HeaderText="Comments" DataField="Comments" />
        <asp:TemplateField HeaderText="Photo">
            <ItemTemplate>
                <asp:ImageButton ID="imgbtnPhoto" ImageUrl= '<%# Eval("Image") %>' runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Photo" />                   
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <EmptyDataTemplate>
        <asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="Small" ForeColor="Red" Text="No Records Found"></asp:Label>
    </EmptyDataTemplate>
    <SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" Font-Bold="True" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Font-Names="Verdana" Font-Size="Medium" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>

<tr>
    <td>
        <asp:Panel ID="Panel1" runat="server">
            <table cellpadding="2" cellspacing="2" border="0">
                <tr>
                    <td>
                        <asp:Label ID="Label8" runat="server" Text="Upload Photo :" Font-Bold="True" Font-Names="Verdana" Font-Size="Small" ForeColor="White"></asp:Label>
                    </td>
                    <td>
                        <asp:FileUpload ID="fumdpop" runat="server" /><br />
                        <asp:TextBox ID="tx" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="line-height: 6px;">&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnUpload" runat="server" Text="Upload"  onclick="btnUpload_Click" />&nbsp;&nbsp;
                        <asp:Button ID="btnfuCancel" runat="server" Text="Cancel" />
                        &nbsp;
                        <asp:Button ID="btnok" runat="server" Text="OK" />
                    </td>
                </tr>
            </table>
        </asp:Panel>
    </td>
</tr>
<tr>
    <asp:Button ID="bt" runat="server" Text="bt" />
    <cc1:ModalPopupExtender ID="mdpextPhoto" BackgroundCssClass="backgroundcolor" runat="server" TargetControlID="bt" PopupControlID="Panel1" OkControlID="btnUpload" CancelControlID="btnfuCancel" >
    </cc1:ModalPopupExtender>
</tr>

代码背后:

protected void gdvMngTeam_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Photo")
    {
        ViewState["ID"] = Convert.ToInt16(e.CommandArgument);

        //AjaxControlToolkit.ModalPopupExtender mpop = (AjaxControlToolkit.ModalPopupExtender)gdvMngTeam.SelectedRow.FindControl(
        mdpextPhoto.Show();
    }
}

protected void btnUpload_Click(object sender, EventArgs e)
{   
    string txt = tx.Text;
    if(fumdpop.HasFile)
    {
        string filename = fumdpop.FileName;
        fumdpop.SaveAs(Server.MapPath("Photos/" + filename));
        objTeam.ID = id;
        objTeam.Image = "photos/" + filename;
        string resutl = TeamManager.ChangeImage(objTeam);

        ds = TeamManager.GetTeamDetails();
        gdvMngTeam.DataSource = ds.Tables[0];
        gdvMngTeam.DataBind();
    }
}

1 个答案:

答案 0 :(得分:3)

您应该在Fileupload

注册btnUploadFullPostBackControl作为ScriptManager

C#:

ScriptManager curObj = ScriptManager.GetCurrent(Page);
(if curObj != null)
      current.RegisterPostBackControl(btnUpload);

并在VB中:

Dim curObj As ScriptManager = ScriptManager.GetCurrent(Page)
If curObj IsNot Nothing Then
      curObj.RegisterPostBackControl(btnUpload)
End If