2

could not access textbox text in TemplateField

.aspx:

<asp:TemplateField HeaderText="PIN" AccessibleHeaderText="PIN">
<ItemTemplate>
    <asp:TextBox ID="txtPin" runat="server" Width="50px" MaxLength="4"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

code behind:

foreach (GridViewRow gr in grdPin.Rows)
        {
            TextBox lblDate = (TextBox)gr.Cells[0].FindControl("txtPin");
            string x = lblDate.Text;
        }

variable x is null.

4

2 回答 2

3

TemplateFields您必须在非单元格FindControl上使用GridViewRow

TextBox txtPin= (TextBox)gr.FindControl("txtPin");

您始终必须使用要查找的控件FindControlNamingContainer如果控件在 aGridViewRow那么它就是NamingContainer.

于 2013-07-08T07:37:43.837 回答
0

您需要使用 RowDataBound事件

if(e.Row.RowType == DataControlRowType.DataRow)
    {
  // find and edit your control here
 // example 
  Label date = (Label)e.Row.FindControl("ControlID");
    }
于 2013-07-08T07:36:18.010 回答