Error in ASP TextBox: The name 'txtDate' does not exist in the current context

0

I have an ASP TextBox control with ID="txtDate" that captures a date from a CalendarExtender control. When I try to access the value in codebehind of the txtFecha.Text object, the compiler shows me the message: The name 'txtFecha' does not exist in the current context.

<asp:TextBox ID="txtFecha" runat="server" maxlength="10" ReadOnly = "false"></asp:TextBox>

<asp:RequiredFieldValidator ID="rfvFecha" runat="server" ErrorMessage="Campo requerido" Text="*" ControlToValidate = "txtFecha"></asp:RequiredFieldValidator>
<asp:RangeValidator runat="server" ID="rvFecha" Type="Date" ControlToValidate="txtFecha" MaximumValue='01/01/2100' MinimumValue="01/01/1900"
ErrorMessage="Fecha no valida" Display="Dynamic" />
<cc1:CalendarExtender ID = "Calender1" runat = "server" TargetControlID = "txtFecha"></cc1:CalendarExtender>

In codebehind

Session["Fecha"] = txtFecha.Text
    
asked by Popularfan 16.08.2018 в 11:06
source

1 answer

2

The controls in ASP.Net are added dynamically. You can cast the component in the following way (Using an example context called "Form").

Session["Fecha"] = ((TextBox)Formulario.FindControl("txtFecha")).Text;

If you do not have an Id for the container you can use the PlaceHolder of the Page. In this case "ContentPlaceHolder1":

ContentPlaceHolder Formulario =
(ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1")
    
answered by 16.08.2018 / 11:56
source