如何从C#程序填写ASP.NET页面上的文本?
发布时间:2020-12-16 01:47:02 所属栏目:百科 来源:网络整理
导读:我试图用一些预定义的文本填充ASP.NET页面文本框,以便在显示时预定义值.我试过了 protected void Page_PreRender (){ mytextbox.Text = somestring;} 它在开发环境中工作正常但在服务器上产生… System.NullReferenceException: Object reference not set to
我试图用一些预定义的文本填充ASP.NET页面文本框,以便在显示时预定义值.我试过了
protected void Page_PreRender () { mytextbox.Text = somestring; } 它在开发环境中工作正常但在服务器上产生… System.NullReferenceException: Object reference not set to an instance of an object 当我在Page_Load中尝试此操作时,同样适用.当我阅读this问题的答案时,我正在努力应该工作(至少在其中一个地方). 谁能看到我做错了什么? 按照建议编辑更多代码. C#看起来像这样: – protected void Page_PreRender (Object sender,EventArgs e) { try { string [] file_list; int i = 0; file_list = Directory.GetFiles(MyProg.Common.GetDirectory(),MyProg.Common.GetFileNameRoot() + "*.*"); foreach (string filename in file_list) { string filenameonly = Path.GetFileName (filename); if (filenameonly == MyProg.Common.GetFileNameRoot() + "runlog.log") { nametextbox.Text = filenameonly; } } } catch (Exception ex) { string mystring = ex.ToString(); errorMessage.Text = "Page Load Error : " + mystring; } } 和这样的ASP.NET页面…… <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyDialogue.aspx.cs" Inherits="MyDialogue" %> <%@ Register assembly="ComponentArt.Web.UI" namespace="ComponentArt.Web.UI" tagprefix="ComponentArt" %> <%@ Register assembly="ComponentArt.Web.Visualization.Charting" namespace="ComponentArt.Web.Visualization.Charting" tagprefix="cc1" %> <!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"> </head> <body> <form id="myForm" runat="server"> <div style="visibility:hidden"> <asp:TextBox ID="nametextbox" TextMode="MultiLine" runat="server" Visible="true" /> </div> </form> </body> </html> 解决方法
可能有几个区域导致了这个问题.您如何确定已将其缩小到文本框本身?在添加文本框消息之前,此代码是否完全没有错误?我将在下面发布您的代码,我认为可能会发生潜在的空引用(在评论中):
string [] file_list; int i = 0; file_list = Directory.GetFiles(MyProg.Common.GetDirectory(),MyProg.Common.GetFileNameRoot() + "*.*"); // it is possible that file_list is null // potentially due to an invalid path (missing / perhaps?) foreach (string filename in file_list) { string filenameonly = Path.GetFileName (filename); // It's possible that the MixedZone.Kernel.Common library // is experiencing the null reference exception because it // may not understand what file to get the name root of or // maybe it is not capable of getting the root for some // other reason (permissions perhaps?) if (filenameonly == MixedZone.Kernel.Common.GetFileNameRoot() + "runlog.log") { nametextbox.Text = filenameonly; } 一些可能的解决方案或更安全的代码 string [] file_list; int i = 0; file_list = Directory.GetFiles(MyProg.Common.GetDirectory(),MyProg.Common.GetFileNameRoot() + "*.*"); if (file_list == null) throw new Exception("File List is null. Something is wrong."); foreach (string filename in file_list) { string filenameonly = Path.GetFileName (filename); string fileroot = MixedZone.Kernel.Common.GetFileNameRoot(); if(string.IsNullOrEmpty(fileroot) throw new Exception("MixedZone Library failed."); if (filenameonly.Equals(fileroot + "runlog.log",StringComparison.OrdinalIgnoreCase)) // Choose your own string comparison here { nametextbox.Text = filenameonly; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |