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="SqlDataSourc e1"
DefaultMode="Edit"
Height="50px"
>
<Fields>
<asp:TemplateField HeaderText="Discharge Date" SortExpression="DischargeD ate">
<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:bedssqlC onnectionS tring %>"
SelectCommand="MySelectPro cedure"
SelectCommandType="StoredP rocedure"
UpdateCommand="MyUpdatePro cedure"
UpdateCommandType="StoredP rocedure">
<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?
<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False"
DataSourceID="SqlDataSourc
DefaultMode="Edit"
Height="50px"
>
<Fields>
<asp:TemplateField HeaderText="Discharge Date" SortExpression="DischargeD
<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:bedssqlC
SelectCommand="MySelectPro
SelectCommandType="StoredP
UpdateCommand="MyUpdatePro
UpdateCommandType="StoredP
<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?
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..?
ASKER
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:LinkBu tton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkBu tton>
</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.
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkBu
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkBu
</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.
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.
ASKER
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.Fin dControl(" TextBoxAct ualDate")) .Text + " " +
((TextBox)DetailsViewActua l.FindCont rol("TextB oxActualTi me")).Text ;
e.NewValues["ActualDischar geDate"] = TheDateTimeString;
}
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
protected void DetailsView1_ItemUpdating(
{
String TheDateTimeString = ((TextBox)DetailsView1.Fin
((TextBox)DetailsViewActua
e.NewValues["ActualDischar
}
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.
I have no problems with closing this question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
= TheDate.Text & TheTime.Text