单击按钮时,FileUpload 始终返回 null 值

FileUpload always return null value when button is clicked

提问人:user3803889 提问时间:11/7/2023 最后编辑:Behtashuser3803889 更新时间:11/7/2023 访问量:40

问:

我有这个带有FileUpload元素的页面。但是当我尝试在代码后面获取值时,它总是返回“NULL”值。它只发生在一个页面中。其他页面运行良好。似乎有什么问题?我尝试搜索,但没有任何效果。比如使用 UpdatePanel、Triggers 等。

以下是我的代码:

ASPX公司

<telerik:RadWindow ID="showImportDialog" runat="server" Skin="<%$ appSettings:Skin %>" EnableEmbeddedSkins="false" 
Width="350" Height="200" Behaviors="Close" Modal="true" VisibleTitlebar="true" Title="<%$ Resources:Master, Import%>">
<ContentTemplate>
    <div class="inputField">
        <p style="font-weight: bold; padding-bottom: 3px;"><asp:Literal runat="server" Text="<%$ Resources:Master, SelectImportFileLbl %>" /></p>
        <div class="uploadFile">
              <span class="glyphicon glyphicon-import"></span>
              <span class="filename fontSize13"><asp:Literal runat="server" Text="<%$ Resources:Master, ImportFieldText %>" /></span>
              <asp:FileUpload ID="ImportFile" runat="server" CssClass="fontSize13" ClientIDMode="static"/>
        </div>
        <p id="errorMsg" style="color: #FF0000; font-size:11px; display: none; padding-bottom: 5px; padding-top: 5px;">
            <asp:Label ID="Label5" ClientIDMode="static" runat="server" Text=""/>
        </p>
    </div>
    <div id="importGroup" class="delete-confirm-button" style="padding-top: 20px;">  
        <asp:Button ID="BtnImport" runat="server" AutoPostBack="false" enabled="false"
                    EnableEmbeddedSkins="false" 
                    Text="<%$ Resources:Master, Import %>" ClientIDMode="static" />
        <asp:Button ID="BtnImportCancel" runat="server" AutoPostBack="false" enabled="true"
                    EnableEmbeddedSkins="false" OnClientClick="btnCancel_Click('showImportDialog'); return false;"
                    Text="<%$ Resources:Master, Cancel %>" ClientIDMode="static" />
    </div>
</ContentTemplate>
</telerik:RadWindow>

VB型

Protected Sub BtnImport_Click(sender As Object, ByVal e As EventArgs) Handles BtnImport.Click
    Dim fileName As String = String.Concat(Path.GetFileNameWithoutExtension(ImportFile.PostedFile.FileName), "_", DateTime.Now.ToString("HHmmssff"), ".", Path.GetExtension(ImportFile.PostedFile.FileName))
    Dim filePath As String = System.IO.Path.Combine("DataImport", fileName)

    Dim fInfo As System.IO.FileInfo = New System.IO.FileInfo(filePath)
    fInfo.Directory.Create()

    ImportFile.SaveAs(filePath)
End Sub
asp.net vb.net 文件上传 导入

评论


答:

0赞 Sekhar 11/7/2023 #1

检查您的方法,我认为控件正在那里重新初始化。包装您不想重新初始化的代码Page_LoadIf Not Page.IsPostBack Then

如果你里面的原始代码是这样的..Page_Load

Sub Page_Load(..)
Line1 
Line2
...
End Sub

然后将其更改为以下内容:

Sub Page_Load(..)
    If Not Page.IsPostBack Then     
        'Regular flow, keep the code
        Line1 
        Line2
        ...
    Else 
        'PostBack code, do not reinitialize 
    End If 
End Sub

评论

0赞 user3803889 11/7/2023
谢谢你的回答。我已经检查了Page_Load。单击导入按钮时,它将转到 IsPostBack。这是否意味着它正在那里重新初始化?
0赞 Sekhar 11/7/2023
你可以试试这个...如果您在 Page_Load 中现有的代码是 ,则只需将其移动到 If Not Page.IsPostBack 方法中... '''If Not Page.IsPostBack Then x y z Else '将此 else 部分留空 End Ifx,y,z..
0赞 user3803889 11/7/2023
我尝试了你的建议,但仍然没有运气。
0赞 Sekhar 11/7/2023
请看我编辑的答案...
0赞 user3803889 11/7/2023
它没有用。:(