0

I'm working on a asp.net web form that requires the user to select a date. The asp page has a MultiView inside an UpdatePanel.

My issue is that when I have a TextBox with textmode="Date", the textbox will always be null on submit and trigger the requiredfield validator. If I change the textmode to plain text and type the date, the form will submit correctly. Also, if I remove the update panel, textmode="Date" will correctly select the date

<asp:UpdatePanel ID="upDetails" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>

<asp:MultiView ID="mvFormSteps" runat="server" ActiveViewIndex="0">    
    <asp:View ID="vwCoreDetails" runat="server">

//other elements...

<asp:TextBox 
    ID="txtSingleDate" 
    runat="server" 
    CssClass="form-control 
    form-control-sm border-dark-subtle" 
    TextMode="Date" 
    format="yyyy-MM-dd"/>                                            
<asp:RequiredFieldValidator 
    ID="txtSingleDate_RequiredFieldValidator" 
    runat="server" 
    ControlToValidate="txtSingleDate" 
    Display="Dynamic"
    ErrorMessage="Select the date of your event" 
    InitialValue=" " 
    ValidationGroup="insertValidation" 
    SetFocusOnError="true" 
    CssClass="text-danger" />

// other elements

<asp:LinkButton 
    ID="btnStep1Next" 
    runat="server" 
    CssClass="btn btn-primary float-end" 
    OnClick="btnStep1Next_Click" Visible="true" 
    CausesValidation="true" 
    ValidationGroup="insertValidation">
Next
</asp:LinkButton>

Any insights or workarounds would be appreciated. Thanks!

1
  • your sample works correctly (VS 2022 17.14.16, .NET Framework 4.7.2). If you are suspecting culture, what culture do you use? Also, what's the code of btnStep1Next_Click? This simple line of code works correctly: Debug.WriteLine(txtSingleDate.Text). Maybe you want to isolate your sample in an empty project and see if it works or not. If it does then something else in your project prohibids your web form from running as expected. Commented Nov 8 at 19:32

1 Answer 1

0

Are you sure the code picking up the text box?

This works for me:

<asp:FormView ID="FormView1" runat="server" 
    DataKeyNames="ID" >
    <ItemTemplate>
        ID:
        <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
        <br />
        FirstName:
        <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>' />
        <br />
        LastName:
        <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>' />
        <br />
        HotelName:
        <asp:TextBox ID="HotelNameTextBox" runat="server" Text='<%# Bind("HotelName") %>' />
        <br />
        BookingDate:
        <asp:TextBox ID="BookingDateTextBox" runat="server" 
            Text='<%# Bind("BookingDate", "{0:d}") %>' 
            TextMode="Date"
            />
        <br />
        <asp:Button ID="cmdSave" runat="server" Text="Save"
            OnClick="cmdSave_Click"
            />
    </ItemTemplate>
</asp:FormView>

And code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadData
    End If
End Sub

Sub LoadData()
    Dim strSQL As String =
        "SELECT * FROM tblHotelsA WHERE ID IN(82)"

    FormView1.DataSource = MyRst(strSQL)
    FormView1.DataBind()

End Sub

Protected Sub cmdSave_Click(sender As Object, e As EventArgs)

    Dim txtBooking As TextBox = FormView1.Row.FindControl("BookingDateTextBox")

    Debug.Print(txtBooking.Text)


End Sub

Note of course that you MUST set the date format when binding the text box:

eg:

                BookingDate:
                <asp:TextBox ID="BookingDateTextBox" runat="server" 
                    Text='<%# Bind("BookingDate", "{0:d}") %>' 
                    TextMode="Date"
                    />

So, in above, it works regardless if I use TextMode=date or not.

I suspect that the current row may well be your issue (and it not clear how/when you setting the current row).

So, you could try this code:

    Dim btn As Button = sender
    Dim fRow As FormView = btn.NamingContainer

    Dim txtBooking As TextBox = fRow.FindControl("BookingDateTextBox")

    Debug.Print(txtBooking.Text)

If that view of one record doesn't have rows or navigation? I would probably just dump the use of the FormView, and use markup + a button anyway.

However, adding TextMode does not affect the above code, so there must be additional details occurring here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment. It's definitely not a current row issue, I can ToString the TextBox element, find the string if the textbox is not a date and I also have the same issue when I move the textbox out of the FormView and directly reference it. The only thing I can think of is that this is an issue caused by culture setting and date format.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.