简体   繁体   English

在Gridview混淆中编辑项目模板文本框值

[英]Edit Item Template TextBox value in a Gridview Confusion

I have a gridview with Edit commandfield. 我有一个带有Edit命令域的gridview。 In one of the templatefield of the gridview,I have a textbox inside edit item template. 在gridview的templatefield之一中,我在编辑项目模板内有一个文本框。 In the RowUpdating event, I try to get the value of the textbox. 在RowUpdating事件中,我尝试获取文本框的值。

TextBox text = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtfname");

GridViewRow row =GridView1.Rows[e.RowIndex] as  GridViewRow;

TextBox tFirstName = row.FindControl("txtfname") as TextBox;

TextBox textFName = (TextBox)row.Cells[0].Controls[3];

I tried all three methods.When I put 我尝试了所有三种方法。

if(!IsPostBack)Bind_grid();

I get the textbox.text value as empty.If I remove that condition I get the value which was bound on the control using Bind function in the markup page.I never got the value I am editing in the control. 我得到的textbox.text值是空的。如果删除该条件,我会得到使用标记页面中的Bind函数绑定在控件上的值,但我从未在控件中编辑该值。

Where am I going wrong? 我要去哪里错了? Solution? 解?

I'm guessing that your grid has the paging enabled. 我猜您的网格已启用分页。 So your are getting the index of the row that is displaying in the grid (not the data source). 因此,您将获得显示在网格中的行的索引(而不是数据源)。

For example: You're in the 2 page of your grid, and you have 10 items per page. 例如:您在网格的第2页中,每页有10个项目。 You click on edit on the 3rd row. 您单击第三行上的编辑。 The e.RowIndex will be 2. e.RowIndex将为2。

If you want to map it to you datasource, this should be... ((page - 1) * items) + index; 如果要将其映射到数据源,则应为...(((page-1)* * items)+ index;

((2-1) * 10) + 2 = 12 ((2-1)* 10)+ 2 = 12

So in your datasource you should get the item that is on the position 12. 因此,在您的数据源中,您应该获得位置12上的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM