问题:添加复选框或其他非Label ItemTemplate字段会导致FindControl方法返回null。
正常行为:通常,当用户点击编辑时,EditItemTemplate字段变为可用,并且我可以使用FindControl方法获取控件。简单吧?
protected void grdOppProducts_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if (e.Row.RowState == DataControlRowState.Edit) {
//only enters edit mode where checkbox control isn't available
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
}
...
原因:现在,在混合中添加一个复选框ItemTemplate字段时,gridView rowState永远不会达到编辑模式,因此EditItemTemplate控件永远不可用,从而导致空值。
gridView控件定义如下(checkBox字段,cbxSelect,导致问题)
<asp:GridView runat="server" id="oppProdgrdView" gridlines="None" AllowPaging="False" selectedrowstyle LightCyan="backcolor"
autogeneratecolumns="false"
autoGenerateEditButton="false"
autoGenerateDeleteButton="true"
OnRowDataBound="grdOppProducts_RowDataBound"
OnRowCommand="grdOppProducts_RowCommand"
OnRowUpdating="grdOppProducts_RowUpdating"
OnRowUpdated="grdOppProducts_RowUpdated"
OnRowCancelingEdit="grdOppProducts_RowCancelingEdit"
OnRowEditing="grdOppProducts_RowEditing"
OnRowDeleting="grdOppProducts_RowDeleting"
cellpadding="1" cssclass="datagrid" pagerstyle-cssclass="gridPager"
alternatingrowstyle-cssclass="rowdk" rowstyle-cssclass="rowlt" selectedrowstyle-cssclass="rowSelected" showemptytable="True" enableviewstate="false"
emptytablerowtext="<%$ resources:dgOppProducts.EmptyTableRowText %>" expandablerows="True" resizablecolumns="True" currentsortdirection="Ascending" currentsortexpression=""
datasourceid="SqlDataSource2" datakeynames="OPPPRODUCTID" showsorticon="False" useslxpagertemplate="True" AllowSorting="true" PageSize="16">
<Columns>
<asp:commandfield showeditbutton="true" causesvalidation="false" headertext=""/>
<asp:BoundField DataField="OPPPRODUCTID" ItemStyle-CssClass="hidden-column" HeaderStyle-CssClass="hidden-column"
HeaderText="OppProductId">
</asp:BoundField>
<asp:BoundField DataField="PRODUCTID" ItemStyle-CssClass="hidden-column" HeaderStyle-CssClass="hidden-column"
HeaderText="ProductId">
</asp:BoundField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:CheckBox ID="cbxSelect" style="width: 10px;" runat="server" AutoPostBack="false" onClick="getCheckBoxValue($(this));" />
</ItemTemplate>
</asp:TemplateField>**
<asp:TemplateField HeaderText="Line #" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtSort" style="width: 25px;" runat="server" AutoPostBack="false" Text='<%# Bind("SORT") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSort" runat="server" Text='<%# Bind("SORT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("PRODUCTNAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Version">
<ItemTemplate>
<asp:Label Text='<%# ACDGetVersion(Eval("PRODUCTID").ToString())%>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Prorate">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPeriodAdjustment" style="width: 25px;" autopostback="false" Text='<%#ACDGetPeriodAdjustment(Eval("OPPPRODUCTID").ToString()) %>'
runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtPer2" Text='<%#ACDGetPeriodAdjustment(Eval("OPPPRODUCTID").ToString()) %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" style="width: 25px;" runat="server" autopostback="false" Text='<%# Bind("QUANTITY") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text='<%# Bind("QUANTITY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LicType" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtUnit" style="width: 100px;" runat="server" AutoPostBack="false" Text='<%# Bind("UNIT") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblUnit" runat="server" Text='<%# Bind("UNIT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UnitList" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtUnitListPrice" style="width: 80px;" runat="server" autopostback="false" Text='<%# Bind("UNITLISTPRICE") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblUnitListPrice" runat="server" Text='<%# Bind("UNITLISTPRICE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ExtList" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtExtendedListPrice" style="width: 80px;" runat="server" autopostback="false" Text='<%# Bind("EXTENDEDLISTPRICE") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblExtendedListPrice" runat="server" Text='<%# Bind("EXTENDEDLISTPRICE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AcadDisc" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtExtendedAcademicDiscAmt" style="width: 50px;" runat="server" ReadOnly="true" autopostback="false" Text='<%# Bind("EXTENDEDACADEMICDISCAMT") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblExtendedAcademicDiscAmt" runat="server" Text='<%# Bind("EXTENDEDACADEMICDISCAMT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="%AddlDisc" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtApproxDiscretionDiscPct" style="width: 30px;" runat="server" readonly="true" autopostback="false" Text='<%# Bind("APPROXDISCRETIONDISCPCT") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblApproxDiscretionDiscPct" runat="server" Text='<%# Bind("APPROXDISCRETIONDISCPCT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AddlDisc" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtExtendedDiscretionDiscAmt" style="width: 50px;" runat="server" autopostback="false" Text='<%# Bind("EXTENDEDDISCRETIONDISCAMT") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblExtendedDiscretionDiscAmt" runat="server" Text='<%# Bind("EXTENDEDDISCRETIONDISCAMT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UnitNet" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtUnitNetPrice" style="width: 70px;" runat="server" ReadOnly="true" Text='<%# Bind("UNITNETPRICE") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UNITNETPRICE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ExtNet" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtExtendedNetPrice" style="width: 70px;" runat="server" ReadOnly="true" Text='<%# Bind("EXTENDEDNETPRICE") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblExtendedNetPrice" runat="server" Text='<%# Bind("EXTENDEDNETPRICE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
注意如果我使用标签作为itemTemplate创建字段,则此方法有效,但我需要能够在不切换到编辑模式的情况下选择多个复选框:
<asp:TemplateField HeaderText="Line test" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:CheckBox ID="TextBox2" style="width: 25px;" runat="server" AutoPostBack="false"></asp:CheckBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" style="width: 25px;" runat="server" Text="test"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
有什么想法吗?提前谢谢!