troubleshooting Question

Reference an unbound control in GridView template

Avatar of MCallas
MCallasFlag for United States of America asked on
.NET ProgrammingVisual Basic.NETASP.NET
3 Comments1 Solution669 ViewsLast Modified:
Dear Experts,
I have a GridView bound to a documents table which displays documents in which a user has not trained on yet. I have added template columns with unbound text boxes to capture the training data. I have another template column that has an Insert button that performs the insert. My problem is that I can't seem to access the data in the unbound text boxes from the RowCommand method. Any help would be greatly appreciated.
<asp:GridView ID="gvNewTraining" runat="server" 
                        	<asp:Button runat="server" 
                                CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" 
                                CommandName="cmdAddTrainingRecord" ID="AddTrainingRecord"
                                Text="Add" />
                <asp:BoundField DataField="DCDocID" HeaderText="DocID" Visible="true" ReadOnly="true" />
                <asp:TemplateField HeaderText="Train Date">
                		<asp:TextBox ID="txtTrainDate" runat="server" style="text-align: center;"
                			Text='<%# DateTime.Now.ToShortDateString() %>' Width="80px"   />
                	<HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Right" Width="80px" />
                <asp:TemplateField HeaderText="Trainer">
                        	<asp:TextBox ID="txtTrainer" runat="server" Width="125px" style="text-align: left;" />
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" Width="125px" />
        <FooterStyle BackColor="#ffccff" Font-Bold="True" ForeColor="Plum" />
        <RowStyle BackColor="#F0FFF0" ForeColor="#000000" />
        <PagerStyle BackColor="Crimson" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="White" Font-Bold="True" ForeColor="#548B54" Font-Size="Smaller" BorderStyle="Solid" BorderWidth="1px" />
        <EditRowStyle BackColor="#FFFFE0" ForeColor="Black" />
        <AlternatingRowStyle BackColor="White" ForeColor="#008000" />

Protected Sub gvNewTraining_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvNewTraining.RowCommand

    If (e.CommandName = "cmdAddTrainingRecord") Then
        ' Retrieve the row index stored in the CommandArgument property.
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        ' Retrieve the row that contains the button from the Rows collection.
        Dim row As GridViewRow = gvNewTraining.Rows(index)

        ' Ensure all necessary data has been added.
        Dim iDocID As Integer = row.Cells(1).Text
        'Dim TrainDate As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text
        'Dim TrainDate As String = CType(row.FindControl("txtTrainDate"), TextBox).ToString()
        'Dim TrainDate As String = CType(row.FindControl("txtTrainDate"), TextBox).ToString
        'Dim TrainDate As String = row.FindControl("txtTrainDate").ToString
    End If
End Sub
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros