Link to home
Start Free TrialLog in
Avatar of cjmackenzie
cjmackenzie

asked on

How to combine date and time in DetailsView

I have 2 fields in a DetailsView bound to the same date field:

    <asp:DetailsView ID="DetailsView1" runat="server"
        AutoGenerateRows="False"
        DataSourceID="SqlDataSource1"
        DefaultMode="Edit"
        Height="50px"
        >
        <Fields>
            <asp:TemplateField HeaderText="Discharge Date" SortExpression="DischargeDate">
                <EditItemTemplate>
                    <asp:TextBox ID="TheDate" runat="server" MaxLength="8" Text='<%# Bind("ActualDate", "{0:dd/MM/yyyy}") %>'
                        Width="90px"></asp:TextBox>
                    <asp:TextBox ID="TheTime" runat="server" MaxLength="5" Text='<%# Bind("ActualDate", "{0:t}") %>'
                        Width="40px"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>            
        </Fields>
    </asp:DetailsView>

And I have a SqlDataSource that has an update statement:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bedssqlConnectionString %>"
        SelectCommand="MySelectProcedure"
        SelectCommandType="StoredProcedure"
        UpdateCommand="MyUpdateProcedure"
        UpdateCommandType="StoredProcedure">
        <SelectParameters>
            <asp:QueryStringParameter Name="TheID" QueryStringField="myid" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:QueryStringParameter Name="theid" QueryStringField="myid" />
            <asp:Parameter Name="ActualDate" />
        </UpdateParameters>
    </asp:SqlDataSource>

I am used to ASP.NET setting the value of the update parameter for me when I click on submit. However, because I have split the DateTime into Date and Time, in this case only the time portion is passed back to the database. How can combine the "ActualDate" fields and assign them to the parameter used by the stored procedure?
Avatar of sirbounty
sirbounty
Flag of United States of America image

Have you tried with

= TheDate.Text & TheTime.Text
Avatar of cjmackenzie
cjmackenzie

ASKER

My problem is I don't know how to retrieve the parameters that ASP.NET automatically creates for me in order to assign values to them.
Sorry - not sure I follow you there..?
In my DetailsView i also have an Update button:

            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Update"></asp:LinkButton>
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>

When I click on this, as I understand it, ASP.NET automatically assigns the value in the TextBox assigned to the "ActualDate" field to the "ActualDate" parameter. (You have to get the naming correct). Problem is, there are 2 TextBoxes bound to "ActualDate", so it appears that the 2nd overwrites the 1st, and I just get the time.
> My problem is I don't know how to retrieve the parameters...

That is the heart of the matter. Perhaps it is time to throw off the shackles of Wizards and E-Z-Progammable Widgets and learn how to roll your own code, code that you understand, code that does what you want it to do.

While this technically isn't an answer, and I'm not looking for points, it is something for you to ponder.
OK I found out how it works from here:
http://msdn2.microsoft.com/en-us/library/ms228051(VS.80).aspx

The DetailsView passes the values that the SqlDatSurce uses to create the parameters in the NewValues collection, and that can be accessed in the DetailsView updating event from the DetailsViewUpdateEventArgs as follows:

    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        String TheDateTimeString = ((TextBox)DetailsView1.FindControl("TextBoxActualDate")).Text + " " +
                            ((TextBox)DetailsViewActual.FindControl("TextBoxActualTime")).Text;
        e.NewValues["ActualDischargeDate"] = TheDateTimeString;
    }
cjmackenzie: Excellent work! Taking the initiative and figuring out your problem is the best way to learn. You will discover other things that make you scratch your head in wonder, but this won't be one of them! Good job!

I have no problems with closing this question.
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial