如何从gridview中的模板字段获取数据

时间:2012-01-30 10:34:04

标签: asp.net gridview

目前我正在研究我的最后一年项目。      在一个在gridview中加载数据的网页中,每一行都有一个按钮,它会弹出一个窗口并要求在**文本框内写入错误**并在服务器上提交该错误。      在服务器端我需要两个值,第一个是该行的主键和** textbox **中写的错误。很容易获得主键值,但我无法在文本框中获得价值。      我附上了.aspx文件的代码:

<asp:GridView ID="gvPODetails" runat="server" DataSourceID="Inspection_SqlDataSource"
            EnableModelValidation="True" AllowSorting="True" AutoGenerateColumns="False"
            BackColor="#CCCCCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" ForeColor="Black" CellSpacing="2">
            <Columns>
                <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" SortExpression="ProductCode" />
                <asp:BoundField DataField="MaterialCode" HeaderText="MaterialCode" SortExpression="MaterialCode" />
                <asp:TemplateField>
                    <ItemStyle BorderStyle="None" BorderColor="Transparent" BorderWidth="0px" />
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkbtnOk" OnClick="Ok_Click" CommandArgument='<%# Eval("Identity")%>'
                            runat="server" Text="Ok"></asp:LinkButton>
                        <asp:LinkButton ID="lnkbtnReject" runat="server" Text="Reject"></asp:LinkButton>                        
                        <asp:Panel ID="popUp_Data" runat="server" CssClass="modelPopup" Style="display: none;">
                            <table style="padding: 10px 10px 10px 10px; width: 100%;">
                                <tr>
                                    <td>
                                    </td>
                                    <td align="right">
                                        <input id="close_popup" type="image" src="../Images/closebox.gif" />
                                    </td>
                                </tr>
                                <tr valign="top">
                                    <td align="right">
                                        <asp:Label ID="lblError" Text="Error" runat="server" CssClass="fontStyle"></asp:Label>
                                    </td>
                                    <td align="left">
                                        <asp:TextBox ID="txtError" runat="server" CssClass="ta"
                                            TextMode="MultiLine"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                    </td>
                                    <td align="left">
                                        <asp:Button ID="btnSubmit" runat="server" CssClass="sbmt" Text="Reject Item" OnClick="Reject_Click"
                                            CommandArgument='<%# Eval("Identity")%>' />
                                    </td>
                                </tr>
                            </table>
                        </asp:Panel>                               
                        <asp:ModalPopupExtender ID="popUp_Data_ModalPopupExtender" runat="server" DynamicServicePath=""
                            Enabled="True" BackgroundCssClass="modelBackground" PopupControlID="popUp_Data"
                            TargetControlID="lnkbtnReject" CancelControlID="close_popup">
                        </asp:ModalPopupExtender>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                <asp:Label runat="server" ID="lblNoDataFound" Text="No Machine Found."></asp:Label>
            </EmptyDataTemplate>
        </asp:GridView>

<asp:GridView ID="gvPODetails" runat="server" DataSourceID="Inspection_SqlDataSource" EnableModelValidation="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" ForeColor="Black" CellSpacing="2"> <Columns> <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" SortExpression="ProductCode" /> <asp:BoundField DataField="MaterialCode" HeaderText="MaterialCode" SortExpression="MaterialCode" /> <asp:TemplateField> <ItemStyle BorderStyle="None" BorderColor="Transparent" BorderWidth="0px" /> <ItemTemplate> <asp:LinkButton ID="lnkbtnOk" OnClick="Ok_Click" CommandArgument='<%# Eval("Identity")%>' runat="server" Text="Ok"></asp:LinkButton> <asp:LinkButton ID="lnkbtnReject" runat="server" Text="Reject"></asp:LinkButton> <asp:Panel ID="popUp_Data" runat="server" CssClass="modelPopup" Style="display: none;"> <table style="padding: 10px 10px 10px 10px; width: 100%;"> <tr> <td> </td> <td align="right"> <input id="close_popup" type="image" src="../Images/closebox.gif" /> </td> </tr> <tr valign="top"> <td align="right"> <asp:Label ID="lblError" Text="Error" runat="server" CssClass="fontStyle"></asp:Label> </td> <td align="left"> <asp:TextBox ID="txtError" runat="server" CssClass="ta" TextMode="MultiLine"></asp:TextBox> </td> </tr> <tr> <td> </td> <td align="left"> <asp:Button ID="btnSubmit" runat="server" CssClass="sbmt" Text="Reject Item" OnClick="Reject_Click" CommandArgument='<%# Eval("Identity")%>' /> </td> </tr> </table> </asp:Panel> <asp:ModalPopupExtender ID="popUp_Data_ModalPopupExtender" runat="server" DynamicServicePath="" Enabled="True" BackgroundCssClass="modelBackground" PopupControlID="popUp_Data" TargetControlID="lnkbtnReject" CancelControlID="close_popup"> </asp:ModalPopupExtender> </ItemTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate> <asp:Label runat="server" ID="lblNoDataFound" Text="No Machine Found."></asp:Label> </EmptyDataTemplate> </asp:GridView>         我想要的是文本框中id为 txtError 的数据。

1 个答案:

答案 0 :(得分:2)

由于它是重复项,因此您无法使用其ID直接访问它。您可以使用FindControl代替,如下所示:

var textBoxAux = gridView.Rows[index].FindControl("txtError") as TextBox;

您需要在处理事件中找到行的索引

编辑:找到索引......

如果适用,请将DataItemIndex传递给CommandArgument(而不是身份)。

CommandArgument='<%# Container.DataItemIndex %>'

参考:ASP.NET GridView RowIndex As CommandArgument