无法在ItemTemplate中找到TextBox控件

时间:2012-01-03 04:48:07

标签: c# asp.net .net-3.5

我无法在gridview中找到文本框控件。以下代码始终返回NULL

HTML

<asp:TemplateField>
    <HeaderTemplate>
        <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                margin-left: 0px">
            <tr>
                <td>
                    &#160;&#160;
                </td>
            </tr>
            <tr>
                <td align="center">
                    SkillColumn1
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table cellpadding="0" cellspacing="0" style="width: 100%">
            <tr style="width: 100%">
                <td style="width: 25%">
                    <asp:TextBox  ID="txt_Skill1" runat="server" Text='<%# Eval("Skill1_value") %>'
                            CssClass="tms-secondaryRowTextBoxNew" Width="35%" />
                </td>
            </tr>
        </table>
    </ItemTemplate>

整个标记

<asp:GridView ID="gvLocView" runat="server" AutoGenerateColumns="False" CellPadding="0"
                Width="100%" BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" 
                EmptyDataText="No Data Available" 
                onrowdatabound="gvLocView_RowDataBound">
                <Columns>                      
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                <tr>
                                    <td align="center">
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        Location
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                    <td style="width: 50%">
                                        <asp:Label ID="lblLocation" runat="server" Text='<%# Eval("Location") %>' CssClass="tms-secondaryRowTextBoxNew" />
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="14%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        Quarter
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                    <td style="width: 100%">
                                        <asp:Label ID="lblQuarter" runat="server" Text='<%# Eval("Quarter") %>' CssClass="tms-secondaryRowTextBoxNew" />
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="13%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                  <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        SkillColumn1
                                    </td>
                                </tr>
                                </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                    <td style="width: 25%">
                                    <asp:TextBox  ID="txt_Skill1" runat="server" Text='<%# Eval("Skill1_value") %>'
                                            CssClass="tms-secondaryRowTextBoxNew" Width="35%" />                                           
                                    </td>
                                  </tr>
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="9%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                    <asp:TemplateField>  <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        SkillColumn2
                                    </td>
                                </tr>
                                </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">                                     
                                    <td style="width: 25%">
                                        <asp:TextBox ID="txt_Skill2" runat="server" Text='<%# Eval("Skill2_value") %>'
                                            CssClass="tms-secondaryRowTextBox" Width="35%" />
                                    </td>
                                  </tr>
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="9%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                       </asp:TemplateField>
                    <asp:TemplateField>
                    <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                 <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        SkillColumn3
                                    </td>
                                </tr>
                                </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                    <td style="width: 25%">
                                        <asp:TextBox ID="txt_Skill3" runat="server" Text='<%# Eval("Skill3_value") %>'
                                            CssClass="tms-secondaryRowTextBox" Width="35%" />
                                    </td>
                                  </tr>  
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="9%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                 <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        SkillColumn4
                                    </td>
                                </tr>
                                </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                    <td style="width: 25%">
                                        <asp:TextBox ID="txt_Skill4" runat="server" Text='<%# Eval("Skill4_value") %>'
                                            CssClass="tms-secondaryRowTextBox" Width="35%" />
                                    </td>
                                 </tr>   
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="9%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                    <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                 <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        SkillColumn5
                                    </td>
                                </tr>
                                </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                    <td style="width: 25%">
                                        <asp:TextBox ID="txt_Skill5" runat="server" Text='<%# Eval("Skill5_value") %>'
                                            CssClass="tms-secondaryRowTextBox" Width="35%" />
                                    </td>
                                 </tr>   
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="9%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                    <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                 <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        SkillColumn6
                                    </td>
                                </tr>
                                </table>
                        </HeaderTemplate>
                        <ItemTemplate>                               
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                   <td style="width: 25%">
                                        <asp:TextBox ID="txt_Skill6" runat="server" Text='<%# Eval("Others") %>'
                                            CssClass="tms-secondaryRowTextBox" Width="35%" />
                                    </td>                                       
                                </tr>
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="9%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                    <HeaderTemplate>
                            <table cellpadding="0" cellspacing="0" style="padding-left: 0px; padding-right: 0px;
                                margin-left: 0px">
                                 <tr>
                                    <td>
                                        &#160;&#160;
                                    </td>
                                </tr>
                                <tr>
                                   <td align="center">
                                        Total
                                    </td>
                                </tr>
                                </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" style="width: 100%">
                                <tr style="width: 100%">
                                    <td style="width: 100%">
                                        <asp:Label ID="lblTotal" runat="server" Text='<%# Eval("Total") %>' CssClass="tms-secondaryRowTextBoxNew" />
                                    </td>
                                 </tr>   
                            </table>
                        </ItemTemplate>
                        <HeaderStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" Width="9%" />
                        <ItemStyle BorderColor="#013963" BorderStyle="Solid" BorderWidth="1px" />
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle CssClass="tms-primaryRow_new" />
                <HeaderStyle Font-Bold="True" ForeColor="White" CssClass="tms-gridViewHeader_fy11"
                    HorizontalAlign="Center" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            </asp:GridView>

代码隐藏

if (e.Row.RowType == DataControlRowType.Header)
    {
        int counter=0;

        for (int j = 1,col=2; j <= 7; j++,col++)
        {
            TextBox t = (TextBox)e.Row.Cells[col].FindControl("txt_Skill" + j.ToString());

            if (t != null)
            {
                t = e.Row.FindControl("txt_Skill" + j.ToString()) as TextBox;
                if (!string.IsNullOrEmpty(strSkillArray[counter]))
                    t.Text = strSkillArray[counter].ToString();
            }                
        }
    }

异常

  

System.Web.HttpException未被用户代码
处理   Message =“DataBinding:'System.Data.DataRowView'不包含   名为'Skill1_value'的属性。“Source =”System.Web“
  ErrorCode = -2147467259 StackTrace:          在System.Web.UI.DataBinder.GetPropertyValue(Object container,String propName)          在System.Web.UI.DataBinder.Eval(Object container,String [] expressionParts)          在System.Web.UI.DataBinder.Eval(对象容器,字符串表达式)          在System.Web.UI.TemplateControl.Eval(String表达式)          位于d:\ ABC \ Location.aspx中的ASP.location_aspx ._ DataBinding _control53(Object sender,EventArgs e):第251行          在System.Web.UI.Control.OnDataBinding(EventArgs e)          在System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)          在System.Web.UI.Control.DataBind()          在System.Web.UI.Control.DataBindChildren()          在System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)          在System.Web.UI.Control.DataBind()          在System.Web.UI.Control.DataBindChildren()          在System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)          在System.Web.UI.Control.DataBind()          在System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex,Int32 dataSourceIndex,DataControlRowType rowType,DataControlRowState)   rowState,Boolean dataBind,Object dataItem,DataControlField []   fields,TableRowCollection行,PagedDataSource pagedDataSource)          在System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable   dataSource,Boolean dataBinding)          在System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable)   数据)          在System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable   数据)          在System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable)   数据)          在System.Web.UI.DataSourceView.Select(DataSourceSelectArguments   参数,DataSourceViewSelectCallback回调)          在System.Web.UI.WebControls.DataBoundControl.PerformSelect()          在System.Web.UI.WebControls.BaseDataBoundControl.DataBind()          在System.Web.UI.WebControls.GridView.DataBind()          位于d:\ ABC \ Location.aspx.cs中的Location.UpgradeAccount():第368行          位于d:\ ABC \ Location.aspx.cs中的Location.Page_Load(Object sender,EventArgs e):第70行          在System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e)          在System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,   EventArgs e)          在System.Web.UI.Control.OnLoad(EventArgs e)          在System.Web.UI.Control.LoadRecursive()          在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
  的InnerException:

3 个答案:

答案 0 :(得分:0)

HeaderTemplate中没有任何内容。您必须使用DataControlRowType.DataRow来查找行类型。

if (e.Row.RowType == DataControlRowType.DataRow)
{
    int counter=0;

    for (int j = 1,col=2; col <= 7; j++,col++,counter++)
    {
        TextBox t = (TextBox)e.Row.Cells[col].FindControl("txt_Skill" + j.ToString());
        if (t != null)
        {
            if (!string.IsNullOrEmpty(strSkillArray[counter]))
                t.Text = strSkillArray[counter].ToString();
        }                
    }
}

答案 1 :(得分:0)

您的例外与您发布的代码隐藏无关。该页面无法将您的网格绑定到您提供的任何DataSource,因为它无法在您的DataSource中找到Skill1_value。

答案 2 :(得分:-1)

你的texbox在Itemptemplate中,你在标题模板中搜索这就是为什么

请按照以下

更新您的代码

如果条件改变

 if (e.Row.RowType == DataControlRowType.DataRow)

查找文本框的代码

 TextBox txt_Skill= (TextBox)e.Row.FindControl("txt_Skill");