FindControl 返回空文本对象
本文关键字:对象 文本 返回 FindControl | 更新日期: 2023-09-27 18:35:32
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BindGridView._Default" %>
<!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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Name1">
<ItemTemplate>
<asp:Label ID="lblName1" runat="server" Text='<%# Eval("Name1")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUName1" runat="server" Text='<%# Eval("Name1")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name2" >
<ItemTemplate>
<asp:Label ID="lblName2" runat="server" Text='<%# Eval("Name2")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUName2" runat="server" Text='<%# Eval("Name2")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name3" >
<ItemTemplate>
<asp:Label ID="lblName3" runat="server" Text='<%# Eval("Name3")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUName3" runat="server" Text='<%# Eval("Name3")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnCountry" runat="server" Value='<%#Eval("country") %>' />
<%-- <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country")%>' />--%>
<asp:DropDownList ID="DrpDownList1" runat="server" >
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>United States</asp:ListItem>
<asp:ListItem>France</asp:ListItem>
<asp:ListItem>Russia</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" CommandName="Update"/>
<asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
前端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace BindGridView
{
public partial class _Default : System.Web.UI.Page
{
public List<Employee> lstEmp = new List<Employee>();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
List<Employee> lstEmp = ReturnList();
GridView1.DataSource = lstEmp;
GridView1.DataBind();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = 0;
BindEdit(Session["Name"].ToString());
}
protected void OnCancel(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
private void BindEdit(string id)
{
List<Employee> lstEmp = ReturnList();
lstEmp = new List<Employee>(lstEmp.Where(x => x.Id.ToString() == id).ToList());
GridView1.DataSource = lstEmp;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Edit")//redirect to MangeRulepage and set query string Mode variable to 'EDIT'
{
//BindEdit(e.CommandArgument.ToString());
Session["Name"] = e.CommandArgument.ToString();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DrpDownList1");
HiddenField hdnval = (HiddenField)e.Row.FindControl("hdnCountry");
if (ddl != null)
{
ddl.Items.FindByValue(hdnval.Value).Selected = true;
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//GridView1.EditIndex = -1;
TextBox tb = (TextBox)GridView1.FindControl("txtUName1");// Not getting object *tb* in case of update
if (tb != null)
{
string text = tb.Text;
}
}
public class Employee
{
public int Id { get; set; }
public string Name1 { get; set; }
public string Name2 { get; set; }
public string Name3 { get; set; }
public string Country { get; set; }
}
private List<Employee> ReturnList()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "1,2,3", "United States");
dt.Rows.Add(2, "", "India");
dt.Rows.Add(3, "4,5,6", "France");
dt.Rows.Add(4, "", "Russia");
for (int i = 0; i < dt.Rows.Count; i++)
{
Employee emp = new Employee();
emp.Id = Convert.ToInt32(dt.Rows[i]["Id"]);
emp.Name1 = dt.Rows[i]["Name"].ToString();
emp.Country = dt.Rows[i]["Country"].ToString();
lstEmp.Add(emp);
}
return lstEmp;
}
}
}
长时间后 ASP.Net GridView,我无法在EditItemTemplate的GridView中找到文本框对象。从过去几个小时开始谷歌搜索,但无法找到我的问题的解决方案。
请帮忙。
尝试使用以下代码查找文本框
GridViewRow row = TaskGridView.Rows[e.RowIndex];
TextBox tb = (TextBox)(row.Cells[0].Controls[0]);
或
TextBox tb = (TextBox)(row.FindControl("txtUName1"));
希望这对你有帮助