开发者

How do I fill in text on an ASP.NET page from a C# program?

I am attempting to fill in an ASP.NET page textbox with some predefined text so that when it is displayed the value is predefined. I have tried

protected void Page_PreRender ()
{
    mytextbox.Text = somestring;
}

which works fine in the development environment but on the server produces...

System.NullReferenceException: Object reference not set to an instance of an object

The same applies when I try this in Page_Load. As I read the answers to this question, what I am trying should work (in at least one of these places).

Can anyone see what I am doing wrong?

EDIT more code, as suggested. The C# looks like this:-

    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;
        }

    }

and the ASP.NET page like this...

<%@ 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&g开发者_如何学JAVAt;
</html>


Did you publish your site but did the filerefence to the codebehind stay in the aspx page? are you sure the dll in the bin folder?


This should work without complaint. Does the mytextbox control have the runat="server" attribute? You can only access from the codebehind stuff with the runat="server" attribute.


There could be several areas that are causing this problem. How are you sure that you've narrowed it down to the textbox itself? Was this code completely bug-free before adding the textbox message? I'll post your code below with where I think potential null references may be occurring (in comments):

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;
    }

Some possible solutions or safer code:

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;
    }


Run with Antivirus disabled on the Production Server?

Compare .Net versions between Production and Development?


"which works fine in the development environment but on the server produces" - so, permissions or missing files perhaps?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜