微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从C#程序填写ASP.NET页面上的文本?

我试图用一些预定义的文本填充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;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐