1

我目前正在创建一个页面,用户将能够输入信息并在提交时应将此信息保存到文本文件中,但是我似乎无法获取文本框,因为即使设置了 ID,它似乎也未定义关于它,有人可以解释我做错了什么吗?因为它似乎与我的 btnSave 方法正常工作。

后端 C#:

    public partial class Green_FreeShipping : System.Web.UI.Page
{
    private static readonly string FILE_PATH = "~/TextFiles/Notes.txt";

    private void GetNote()
    {
        using (TextReader tr = new StreamReader(MapPath(FILE_PATH)))
        {
            txtNote.Text = tr.ReadToEnd();
        }
    }

    private void SaveNote()
    {
        using (TextWriter tw = new StreamWriter(MapPath(FILE_PATH)))
        {
            tw.Write(txtNote.Text);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.GetNote();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        this.SaveNote();
        this.GetNote();
    }
}

ASP.NET 代码:

<%@ Page Language="C#" MasterPageFile="~/admin/masters/admin.master" autoeventwireup="true" inherits="TextBox_ReadWriteToTextFile" Title="Green & Free shipping amounts" codefile="~/admin/bespoke/Green-FreeShipping.aspx.cs"%>

<%@ Register TagPrefix="web" Assembly="website.Web" Namespace="website.Web" %>
<%@ Register TagPrefix="sales" Assembly="website.site.Web" Namespace="website.site.Web.Sales" %>
<%@ Register TagPrefix="ecom" Namespace="website.site.Web" Assembly="website.site.Web" %>
<asp:Content ID="TitleContent" ContentPlaceHolderID="TitlePlaceHolder" runat="Server">
    <title>Shopfront - Green and Free shipping amounts</title>
</asp:Content>

<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
    <div style="margin-bottom: 20px;">
    <asp:textbox id="txtNote" runat="server" rows="5" textmode="MultiLine" width="200px" />
    </div>
    <asp:button id="btnSave" runat="server" onclick="btnSave_Click" text="Save" />
</asp:content>
4

1 回答 1

4

您的 asp 应该继承“Green_FreeShipping”,以便 c# 可以访问其中包含的控件。

于 2012-11-05T10:01:26.473 回答