文本框在当前上下文中不存在(Gridview和TemplateField)

时间:2013-11-19 08:40:40

标签: c# asp.net visual-studio-2012 gridview

我试图通过将值存储到字符串中来记录输入文本,并使用标签文本显示它。

但是,我收到此错误,因为我怀疑文本框位于模板字段中。如果我将文本框放在模板字段之外,它就可以工作。

这是一个错误:

enter image description here

以下是MasterPage.Master.aspx的代码:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="gridviewtemplate.MasterPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

以下是default.aspx的代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="gridviewtemplate._default" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <asp:Label ID="lbl_result" runat="server"></asp:Label>

    <asp:GridView ID="grv_test" runat="server" DataSourceID="sds_store" AutoGenerateColumns="False">

        <Columns>

            <asp:TemplateField HeaderText="Name">

                <ItemTemplate>
                    <asp:TextBox ID="txt_test1" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
                </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField HeaderText="Submit">
                <ItemTemplate>
                    <asp:Button ID="btn_test2" runat="server" Text="Submit Changes" OnClick="btn_test2_Click" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Product ID">

                <ItemTemplate>
                    <asp:Label ID="lbl_test3" runat="server" Text='<%#Eval("product_id")%>'></asp:Label>
                </ItemTemplate>

            </asp:TemplateField>
        </Columns>

    </asp:GridView>
    <asp:SqlDataSource ID="sds_store" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:websiteConnection %>" 
                        SelectCommand="SELECT [name], [product_id] FROM [tbl_cart]">
                    </asp:SqlDataSource>

</asp:Content>

以下是default.aspx.cs的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace gridviewtemplate
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btn_test2_Click(object sender, EventArgs e)
        {
            string test = txt_test1.Text;

            lbl_result.Text = "You have changed the textbox to " + test;
        }


    }
}

我还尝试了其他解决方案

  

protected global :: System.Web.UI.WebControls.TextBox txt_test1;

default.aspx.designer.cs

然而,它提示我一个错误:

enter image description here

还有其他方法可以克服错误吗?

2 个答案:

答案 0 :(得分:4)

第1步:您可以使用FindControl()功能从TextBox获取所需的GridView控件 第2步:将已识别的控件投射到TextBox
第3步:使用Text属性从Control

获取文字

试试这个:

protected void btn_test2_Click(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)grv_test.Rows[grv.SelectedIndex].FindControl("txt_test1");
             string test = txt.Text;

            lbl_result.Text = "You have changed the textbox to " + test;
        }

答案 1 :(得分:2)

您无法使用TextBox直接访问GridViewControl中的ID

使用FindControl方法对GridView项目。

见下面的例子

    protected void btn_test1_Click(object sender, EventArgs e)
      {
          TextBox txt_test1=  grv_test.Rows[0].FindControl("txt_test1") as TextBox;
            if (txt_test1!= null)
            {
                string value = txt_test1.Text;
                //add your code here
            }
        }
     }

   protected void btn_test2_Click(object sender, EventArgs e)
      {
           TextBox txt_test1= grv_test.Rows[1].FindControl("txt_test1") as TextBox;
            if (txt_test1!= null)
            {
                string value = txt_test1.Text;
                //add your code here
            }

     }

   protected void btn_test3_Click(object sender, EventArgs e)
      {
           TextBox txt_test1= grv_test.Rows[2].FindControl("txt_test1") as TextBox;
            if (txt_test1!= null)
            {
                string value = txt_test1.Text;
                //add your code here
            }

     }