gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:11:50 27 4
gpt4 key购买 nike

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

但是,我收到此错误是因为我怀疑文本框位于模板字段内。如果我将文本框放在模板字段之外,它会起作用。

这是一个错误:

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

有没有其他方法可以克服这个错误?

最佳答案

第 1 步: 您可以使用 FindControl() 函数从 GridView< br/>Step2 : 将识别出的控件转换为TextBox
第三步:使用Text属性从Control

获取Text

试试这个:

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

关于c# - 当前上下文中不存在文本框(Gridview 和 TemplateField),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066789/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com