如何从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;
    }

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...