如何从gridview文本框中读取值 [英] How do I read value from gridview textbox
问题描述
i在现有gridview中通过行数据绑定事件的代码插入文本框
Hi,
i insert textbox in existing gridview by the code below on row databound event
TextBox tx = new TextBox();
tx.ID =txtStore;
tx.Attributes。添加(runat,server);
e.Row.Cells [2] .Controls.Add(tx);
TextBox tx = new TextBox();
tx.ID = "txtStore";
tx.Attributes.Add("runat", "server");
e.Row.Cells[2].Controls.Add(tx);
现在当我我试图找到并读取此文本框的值是提高null异常代码是 -
now when i am trying to find and read the value of this text box is raise null exception code is -
for (int i = 0; i< GvVarientstoredetails.Rows.Count; i ++)
{
TextBox box1 =(TextBox)GvVarientstoredetails.Rows [i] .Cells [2 ] .FindControl(txtStore);
string s = box1.Text;
}
for (int i = 0; i < GvVarientstoredetails.Rows.Count; i++)
{
TextBox box1 = (TextBox)GvVarientstoredetails.Rows[i].Cells[2].FindControl("txtStore");
string s = box1.Text;
}
我尝试了什么:
for(int i = 0; i&l吨; GvVarientstoredetails.Rows.Count; i ++)
{
TextBox box1 =(TextBox)GvVarientstoredetails.Rows [i] .Cells [2]。.FindControl(txtStore);
string s = box1.Text;
}
string CustomerID =((TextBox)GvVarientstoredetails.Rows [0] .FindControl(txtStore))。Text;
请指导...
What I have tried:
for (int i = 0; i < GvVarientstoredetails.Rows.Count; i++)
{
TextBox box1 = (TextBox)GvVarientstoredetails.Rows[i].Cells[2].FindControl("txtStore");
string s = box1.Text;
}
string CustomerID = ((TextBox)GvVarientstoredetails.Rows[0].FindControl("txtStore")).Text;
please guide...
推荐答案
我还没有在RowDataBound事件中看到你的整个代码所以我不知道出了什么问题。您可能希望使用RowCreated事件来确保将创建控件。这是一个简单的例子:
ASPX:
I haven't seen your whole code in RowDataBound event so I can't tell what went wrong. You might want to use the RowCreated event to ensure that the controls will be created. Here's a quick example:
ASPX:
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server" xmlns:asp="#unknown">
<asp:gridview id="GridView1" runat="server" onrowcreated="GridView1_RowCreated">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:placeholder id="PlaceHolder1" runat="server"> </asp:placeholder>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" />
</asp:content>
背后的代码:
CODE BEHIND:
using System;
using System.Web.UI.WebControls;
using System.Data;
namespace WebFormDemo
{
public partial class DynamicControlInGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
BindGridView();
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
TextBox tbox = new TextBox();
tbox.ID = "TextBox1";
PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
p.Controls.Add(tbox);
//you could also add it to the cells collection like
//TextBox tbox = new TextBox();
//tbox.ID = "TextBox1";
//e.Row.Cells[0].Controls.Add(tbox);
}
}
private void BindGridView() {
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
public DataTable CreateDataSource() {
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("ID", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Lastname", typeof(string)));
dr = dt.NewRow();
//add values to each columns
dr["ID"] = 1;
dr["Name"] = "Vincent";
dr["LastName"] = "Durano";
dt.Rows.Add(dr);
return dt;
}
protected void Button1_Click(object sender, EventArgs e) {
//access TextBox controls
foreach (GridViewRow row in GridView1.Rows) {
TextBox tbox = row.FindControl("TextBox1") as TextBox;
if (tbox != null) {
Response.Write("Found TextBox!");
}
}
}
}
}
这篇关于如何从gridview文本框中读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!