在没有行数据绑定事件的网格视图的空模板中查找服务器端控件

时间:2012-05-17 19:40:41

标签: asp.net

我有一个网格视图,其中一个文本框出现在页脚和编辑模板中,用户可以从中更新数据。我在网格的空模板中创建了一个表,并在其上添加了服务器端文本框,以便用户可以在网格中存在记录时在网格中插入数据。 网格旁边有一个服务器端按钮,点击该按钮即可保存用户通过网格插入或更新的数据。现在我面临的问题是在保存数据的同时找出网格空模板中存在的控件。

我的代码如下:

<asp:GridView ID="grwBrandInfo" runat="server" Width="100%" HeaderStyle-CssClass="gridheaderstyle"
                                    GridLines="Vertical" AutoGenerateColumns="false" ShowFooter="True" BackColor="White"
                                    OnRowCommand="grwBrandInfo_RowCommand" OnRowEditing="grwBrandInfo_RowEditing"
                                    OnRowDeleting="grwBrandInfo_RowDeleting" OnRowUpdating="grwBrandInfo_RowUpdating"
                                    OnRowDataBound="grwBrandInfo_RowDataBound" OnRowCancelingEdit="grwBrandInfo_RowCancelingEdit"
                                    BorderStyle="None">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Quarts">
                                            <ItemTemplate>
                                                <%#Eval("QUARTS")%>
                                                <asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='<%#Eval("PERMIT_QUANTITY_ID") %>' />
                                                <asp:HiddenField ID="hdnQuarts" runat="server" />
                                            </ItemTemplate>
                                            <FooterTemplate>
                                                <asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='<%#Eval("PERMIT_QUANTITY_ID") %>' />
                                                <asp:TextBox ID="txtFootQuarts" runat="server" CssClass="txtfields" MaxLength="4"></asp:TextBox>
                                            </FooterTemplate>
                                            <EditItemTemplate>
                                                <asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='<%#Eval("PERMIT_QUANTITY_ID") %>' />
                                                <asp:TextBox ID="txtEditQuarts" runat="server" EnableViewState="false" Text='<%#Eval("QUARTS")%>'
                                                    CssClass="txtfields" MaxLength="4"></asp:TextBox>
                                            </EditItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" />
                                            <ItemStyle HorizontalAlign="Center" />
                                            <FooterStyle HorizontalAlign="Center" />
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <FooterTemplate>
                                                <asp:LinkButton ID="lbtnInsert" runat="server" Text="Insert" CssClass="hypfield"
                                                    CommandName="Insert" ValidationGroup="save"></asp:LinkButton>
                                            </FooterTemplate>
                                            <FooterStyle HorizontalAlign="Center" />
                                            <HeaderStyle HorizontalAlign="Center" />
                                        </asp:TemplateField>
                                        <asp:CommandField ShowEditButton="True" HeaderText="Action" ShowDeleteButton="true"
                                            ValidationGroup="save">
                                            <ItemStyle CssClass="hypfield" HorizontalAlign="Center" VerticalAlign="Top" />
                                            <ControlStyle CssClass="hypfield" />
                                            <HeaderStyle HorizontalAlign="Center" />
                                        </asp:CommandField>
                                    </Columns>
                                    <EmptyDataTemplate>
                                        <table border="1" cellpadding="5" cellspacing="0" width="100%">
                                            <tr align="Left" bgcolor="#726E6D">
                                                <td>
                                                    <b>
                                                        <asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='0' />
                                                        <asp:Label ID="lblQuarts" runat="server" ForeColor="white" Text="Quarts" CssClass="lblfields"></asp:Label></b>
                                                </td>
                                            </tr>
                                            <tr align="left" id="trEntryNotCompleted" runat="server" bgcolor="#FFFFEE">
                                                <td>
                                                    <asp:TextBox ID="txtQuarts" runat="server" CssClass="txtfields5char" MaxLength="4"></asp:TextBox>
                                                </td>
                                                                                           </tr>
                                        </table>
                                    </EmptyDataTemplate>
                                    <FooterStyle BackColor="#CCCC99" />
                                    <RowStyle BackColor="#F7F7DE" />
                                    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                                    <HeaderStyle BackColor="#6B696B" CssClass="gridheaderstyle" Font-Bold="True" ForeColor="White" />
                                    <AlternatingRowStyle BackColor="White" />
                                </asp:GridView> 

   <asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="save" OnClick="btnSave_Click" />

所以问题是我在保存数据时如何找到空模板中存在的txtQuarts的值

1 个答案:

答案 0 :(得分:-1)

  

Dim txtId As String = CType(gridHelpDesk.Controls(0).Controls(0).FindControl(“txtId”),TextBox).Text