xy83138
DaybreakTian
采纳率83.3%
2016-10-25 02:08

C#网页问题:未将对象引用到实例

2

我想在.CS当中简单地引用.aspx中的一个TextBox控件,但是就是死活不成功。
下面是源码:
.cs:

     protected void chengji_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.gv_chengji.Rows.Count; i++)
        {
            TextBox box = this.gv_chengji.Rows[i].FindControl("TextBox2") as TextBox;
            string text = this.gv_chengji.Rows[i].Cells[1].Text;
            this.update(text, box.Text);
        }
        TextBox TextBox3 = this.FindControl("TextBox3") as TextBox;
        TextBox TextBox4 = this.FindControl("TextBox4") as TextBox;
        TextBox TextBox5 = this.FindControl("TextBox5") as TextBox;
        string year = TextBox3.Text.ToString();
        string month = TextBox4.Text.ToString();
        string day = TextBox5.Text.ToString();

    }

.aspx:

 <%@ page title="" language="C#" masterpagefile="~/MasterPage/MasterPage.master" autoeventwireup="true" inherits="ResultManage_Resultin, App_Web_resultin.aspx.ce51174c" enableeventvalidation="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

        <table width="800">
            <tr>

                <td width="80" height="30">队别:</td>
                <td width="80">
                    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
                        onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td width="80">
                    <asp:Label ID="Label2" runat="server" Text="比赛项目:"></asp:Label>
                </td>
                <td width="80" align="center" colspan="2">
                    <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
                </td>
                <td width="80" align="center">
                    <asp:Button ID="chengji" runat="server" OnClick="chengji_Click" Text="保存更改" Width="66px" />
                </td>
                <td width="80" colspan="2">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
                        Text="导出成绩表(.xls)" Style="margin-left: 0px" Width="122px" />
                </td>
                <td width="80" colspan="2">
                    <asp:Button ID="Button12" runat="server" OnClick="Button12_Click" 
                        Text="生成成绩表(.doc)" Width="117px" />
                </td>
                <td>
                    <asp:Button ID="Button13" runat="server" OnClick="Button13_Click" 
                        Text="导出成绩表(.doc)" Width="117px" />
                </td>
            </tr>

                <table>
                    <tr height="20">
            <td >录入时间:
                </td>
            <td >
                   <asp:TextBox ID="TextBox3" runat="server" Width="45px" ></asp:TextBox>
                   <asp:Label ID="Label3" runat="server" Text="年" Width="15px"></asp:Label>
            </td>
            <td >
                   <asp:TextBox ID="TextBox4" runat="server" Width="45px" ></asp:TextBox>
                   <asp:Label ID="Label4" runat="server" Text="月" Width="15px"></asp:Label>
            </td>
            <td >
                   <asp:TextBox ID="TextBox5" runat="server" Width="45px" ></asp:TextBox>
                   <asp:Label ID="Label5" runat="server" Text="日" Width="15px"></asp:Label>
            </td>
                        </tr>
                    <table>
            </tr>

            <%--<tr><td colspan ="10" height="20"> <iframe src="../ResultManage/ProgressBar.htm" name="RightFrame" frameborder="no" scrolling="no" height="20" style="width: 383px"></iframe></td></tr>
            <tr>--%>
            <tr>
                <td colspan="12" align="center">
                    <asp:GridView ID="gv_chengji" runat="server" AutoGenerateColumns="False" BackColor="White"
                        BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
                        CellPadding="4" GridLines="Horizontal" AllowPaging="True" OnPageIndexChanging="gv_chengji_PageIndexChanging1" OnRowCommand="gv_chengji_RowCommand">
                        <Columns>
                            <asp:TemplateField HeaderText="序号">
                                <ItemTemplate>
                                    <asp:Literal ID="Literal1" runat="server" Text="<%# Container.DataItemIndex+1 %>"></asp:Literal>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <%--<asp:ImageField DataImageUrlField="score" HeaderText="照片"
                                NullImageUrl="~/Image/red/1402003.jpg">
                                <ControlStyle Height="80px" Width="60px" />
                            </asp:ImageField>--%>
                            <asp:BoundField DataField="ID" HeaderText="学号" ReadOnly="True"
                                SortExpression="学号" />
                            <asp:BoundField DataField="name" HeaderText="姓名" ReadOnly="True"
                                SortExpression="姓名" />
                            <asp:BoundField DataField="sex" HeaderText="性别" ReadOnly="True"
                                SortExpression="性别" />
                            <asp:BoundField DataField="level" HeaderText="层次" ReadOnly="True"
                                SortExpression="层次" />
                            <asp:BoundField DataField="Big_unit" HeaderText="大单位" ReadOnly="True"
                                SortExpression="大单位" />
                            <asp:BoundField DataField="unit" HeaderText="小单位" ReadOnly="True"
                                SortExpression="小单位" />
                            <asp:BoundField DataField="year" HeaderText="入学年份" ReadOnly="True"
                                SortExpression="入学年份" />
                            <%--<asp:BoundField DataField="event_num" HeaderText="竞赛编号" ReadOnly="True" 
                SortExpression="竞赛编号" />--%>
                            <asp:BoundField DataField="compevent" HeaderText="竞赛名称" ReadOnly="True"
                                SortExpression="竞赛名称" />
                            <asp:TemplateField HeaderText="比赛成绩" SortExpression="比赛成绩">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("score") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server"
                                        Text='<%# DataBinder.Eval(Container.DataItem,"score") %>'></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>

                        <PagerTemplate>
                            <br/>
                            <asp:Label ID="lblPage1" runat="Server" Text='<%# "第" +( ((GridView)Container.NamingContainer).PageIndex +1 ) +"页/共"+ (((GridView)Container.NamingContainer).PageCount) +"页" %>'></asp:Label>
                            <asp:LinkButton ID="lbnFirst1" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First"></asp:LinkButton>
                            <asp:LinkButton ID="lbnPrev1" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev"></asp:LinkButton>
                            <asp:LinkButton ID="lbnNext1" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next"></asp:LinkButton>
                            <asp:LinkButton ID="lbnLast1" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last"></asp:LinkButton>
                            到第<asp:TextBox runat="server" ID="inPageNum1" Width="10mm"></asp:TextBox>页
                            <asp:Button ID="Button11" CommandName="go" Text="转到" runat="server" />
                            <br />
                        </PagerTemplate>

                        <FooterStyle BackColor="White" ForeColor="#333333" />
                        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="White" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F7F7F7" />
                        <SortedAscendingHeaderStyle BackColor="#487575" />
                        <SortedDescendingCellStyle BackColor="#E5E5E5" />
                        <SortedDescendingHeaderStyle BackColor="#275353" />
                    </asp:GridView>
                </td>
            </tr>
        </table>


</asp:Content>

已经调试过了,在chengji_click函数中,三条定义string的语句老是报错“未将对象引用到实例”,到底是为什么啊???大神救命啊!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • caozhy 从今以后生命中的每一秒都属于我爱的人 5年前

    先通过母版页找占位符,再找控件

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 5年前
     Master.FindControl("ContentPlaceHolder1").FindControl(TextBox3)
    
    点赞 评论 复制链接分享
  • guwei4037 极简吧 5年前

    因为你外面还有一层Content2,所以你找到的TextBox3、TextBox4、TextBox5都为null。先找到Content2,然后再在里面找TextBox.

    点赞 评论 复制链接分享

相关推荐