StephenT
StephenT

Reputation: 495

Unable to retrieve value of templatefield textbox in a gridview

I'm trying to pull the value populated in a templatefield textbox embedded in a gridview. I've tried to do this a couple of different ways but keep getting error messages. I am able to pull the value from a templatefield dropdown control embedded in the same gridview.

Markup:

<asp:TemplateField HeaderText="Renewal Date" >
     <ItemTemplate>
          <ajaxToolkit:CalendarExtender ID="cexRenewalDate" runat="server" TargetControlID="txtRenewalDate" PopupPosition="TopRight"
              Format="MM/dd/yyyy" PopupButtonID="btnimgCalendar" />
          <asp:TextBox ID="txtRenewalDate" runat="server" CssClass="RenewalDateTextBoxStyle" 
              onKeyPress="javascript: return false;" onPaste="javascript: return false;"/>
          <asp:ImageButton ID="btnimgCalendar" runat="server" ImageUrl="~/Images/Calendar.png" CssClass="CalendarImageButtonStyle"/>
     </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
    <ItemTemplate>
        <asp:DropDownList ID="drpOFActions" runat="server" CssClass="ActionDropDownStyle"/>
        <asp:LinkButton ID="lnkOFCommit" runat="server" Text="  Commit  " OnClick="lnkCommitOF_Click" CssClass="CommitLinkButtonStyle"
            OnClientClick="return confirm('Are you sure you want to complete this action?');"
            CommandArgument='<%# Container.DataItemIndex %>' />
    </ItemTemplate>
</asp:TemplateField>

Code behind:

Dim temp As String = CType(grdGeneratedOrderForms.Rows(rowIndex).FindControl("txtRenewalDate"), TextBox)

This throws a 'System.Web.UI.Control' cannot be converted to 'System.Windows.Forms.TextBox' error.

I also tried using this on someone's suggestion:

Dim temp As String = TryCast(grdGeneratedOrderForms.Rows(rowIndex).FindControl("txtRenewalDate"), System.Web.UI.Control.TextBox)

This throws a Type 'System.Web.UI.Control.TextBox' not defined error.

However when I use the below to pull the value of my dropdown control I have no issues:

Dim Action As Integer = TryCast(grdGeneratedOrderForms.Rows(rowIndex).FindControl("drpOFActions"), DropDownList).SelectedValue

Here are my namespace declarations:

Imports Oracle.DataAccess.Client
Imports System.IO
Imports System.Windows.Forms
Imports Aspose.Words
Imports Aspose.Words.Tables
Imports Ionic.Zip

Any ideas would be greatly appreciated...thanks!

Upvotes: 0

Views: 809

Answers (1)

Kiran Hegde
Kiran Hegde

Reputation: 3681

Try this instead. I think you have a namespace conflict in your using statements

Dim temp As String = CType(grdGeneratedOrderForms.Rows(rowIndex).FindControl("txtRenewalDate"), System.Web.UI.WebControls.TextBox).Text

Upvotes: 1

Related Questions