locked
Wrong Date RRS feed

  • Question

  • User-1499457942 posted

    Hi

      In Gridview i have below code. In gridview it shows as 01/02/2018. In TextBox it shows as 02/01/2018

    txt_FDate.Text = ((Label)row.FindControl("lblFDate")).Text;

    <asp:templatefield headertext="Fixed Incentive Date">
    <itemtemplate>
    <asp:label runat="server" id="lblFDate" text='<%#Eval("FDate", "{0:dd/MM/yyyy}") %>'></asp:label>
    </itemtemplate>
    </asp:templatefield>

    Saturday, November 17, 2018 3:14 PM

All replies

  • User475983607 posted
    My best guess is you did not format the text box like you did with the label. Of course, you did not show the most important bit of code. Please please please try debugging your code for once...
    Saturday, November 17, 2018 3:21 PM
  • User61956409 posted

    Hi JagjitSingh,

    In gridview it shows as 01/02/2018. In TextBox it shows as 02/01/2018

    txt_FDate.Text = ((Label)row.FindControl("lblFDate")).Text;

    <asp:templatefield headertext="Fixed Incentive Date">
    <itemtemplate>
    <asp:label runat="server" id="lblFDate" text='<%#Eval("FDate", "{0:dd/MM/yyyy}") %>'></asp:label>
    </itemtemplate>
    </asp:templatefield>

    According to your description, I do a test with the following sample on my side, I can not reproduce same issue.

    GridView Columns:

    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id"/>
        <asp:BoundField DataField="FDate" HeaderText="DateIn"/>
        <asp:TemplateField HeaderText="Fixed Incentive Date">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblFDate" Text='<%#Eval("FDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="btnselect" runat="server" Text="Select" CommandName="Select" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

    TextBox "txt_FDate":

    <asp:TextBox ID="txt_FDate" runat="server"></asp:TextBox>

    GridView RowCommand event:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="Select")
        {
    
            GridViewRow row = ((Button)e.CommandSource).NamingContainer as GridViewRow;
                    
            txt_FDate.Text = ((Label)row.FindControl("lblFDate")).Text;
        }
    }

    Test Result:

    To troubleshoot the issue, you could debug your code to check the actual value of ((Label)row.FindControl("lblFDate")).Text before you assign it as Text of your TextBox control.

    Besides, please check if you apply any date mask to your TextBox control.

    With Regards,

    Fei Han

    Tuesday, November 20, 2018 6:45 AM