如何检索GridView的TextBox值 [英] How do I retrieve TextBox value of a GridView
本文介绍了如何检索GridView的TextBox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码仅在普通WebForm中
The code is just in a Normal WebForm
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Subject" HeaderText="Subject" />
<asp:TemplateField HeaderText="Marks">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这段代码在继承的aspx.cs页面中
This code is in inherited aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = setSubject();
GridView1.DataBind();
}
private DataTable setSubject()
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Subject");
DataRow dr = dt.NewRow();
dr["Subject"] = "English";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Subject"] = "Physics";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Subject"] = "Chemistry";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Subject"] = "Mathematics";
dt.Rows.Add(dr);
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
Double result;
foreach (GridViewRow grdrow in grd.Rows)
{
if (grdrow.RowType == DataControlRowType.DataRow)
{
TextBox textBoxValue = (TextBox)grdrow.FindControl("TextBox1");
result = result + Convert.ToDouble(textBoxValue.Text);
}
}
}
我无法使用此方法检索文本框的经过编辑的值.
但是代码对其他网站(项目)有效.
I can not retrieve the value of after edited value of textbox using this.
But the code is effective for a different website(project).
推荐答案
首先,您使用的是grdrow而不是GridView1.
除此之外,您可以尝试
(TextBox)GridView1.Rows [0] .FindControl("TextBox1");
并且您想要从网格中选择行,然后尝试此
(TextBox)GridView1.SelectedRow.Cells [0] .FindControl("TextBox1");
希望对您有所帮助.
firstly you are using grdrow instead of GridView1.
apart from this you can try
(TextBox)GridView1.Rows[0].FindControl("TextBox1");
and you want to select row from the grid then try this
(TextBox)GridView1.SelectedRow.Cells[0].FindControl("TextBox1");
hope it''ll help.
您可以使用FindControl
方法.
试试类似的东西:
You can useFindControl
method.
Try something like:
// For every row
foreach (GridViewRow item in myGrid.Rows)
{
TextBox myTextBox = (TextBox)item.FindControl("myTextBox");
string text = myTextBox.Text;
}
在绑定数据时尝试添加IsPostBack.
try adding IsPostBack when you bind data.
private void Page_Load()
{
if (!IsPostBack)
{
GridView1.DataSource = setSubject();
GridView1.DataBind();
}
}
这篇关于如何检索GridView的TextBox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文