[RESOLVED] [2005] Using user input as Gridview Parameters
have a web form using Asp.net 2.0 with vb.net. I have two date fields on the form but I would like to validate them before my gridview uses them as parameters. I am using a Dataset for the source of the gridview and two parameters called StartDate and EndDate.
Basically in the code I am setting the values of the fields to the current date at runtime. Also the date fields in the SQL Server table are datetime and I only want the user to have to enter the date and it would grab all records within that date range regardless of the time. When the user enters the two dates and clicks a button on the form I need it to reset the parameters to the enter dates and refresh the data.
Any Ideas how I could get this to work?
Here is the code:
Code:
Partial Class DispatchRequestList
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtStartDate.Text = Today()
txtEndDate.Text = Today()
End Sub
Protected Sub objRequests_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles objRequests.Selecting
Dim paramFrom As New Parameter
Dim paramTo As New Parameter
paramFrom.Name = "@Startdate"
paramTo.Name = "@Enddate"
paramFrom.Type = TypeCode.DateTime
paramTo.Type = TypeCode.DateTime
paramFrom.DefaultValue = txtStartDate.Text
paramTo.DefaultValue = txtEndDate.Text
objRequests.SelectParameters.Add(paramFrom)
objRequests.SelectParameters.Add(paramTo)
End Sub
End Class
Here is the ASP.net page:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DispatchRequestList.aspx.vb" Inherits="DispatchRequestList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body bgcolor="#99ccff">
<form id="form1" runat="server">
<div>
<h1 style="text-align: center">
<span style="color: #3300ff">Dispatch Request List</span></h1>
<p>
</p>
<p>
<span style="color: #3300ff">
<asp:Label ID="Label1" runat="server" Text="Start Date" Width="105px"></asp:Label>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox></span></p>
<p>
<span style="color: #3300ff">
<asp:Label ID="Label2" runat="server" Text="End Date" Width="105px"></asp:Label>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox></span></p>
<p>
<span style="color: #3300ff">
<asp:Button ID="btnRecords" runat="server" Text="Show Records" /></span></p>
<p>
</p>
<p>
<span style="color: #3300ff">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="objRequests" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="RequestDate" HeaderText="Request Date" SortExpression="RequestDate" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:BoundField DataField="EquipmentType" HeaderText="Equip Type" SortExpression="EquipmentType" />
<asp:BoundField DataField="PickupDelivery" HeaderText="Pickup/Delivery" SortExpression="PickupDelivery" />
<asp:BoundField DataField="Job" HeaderText="Job" SortExpression="Job" />
<asp:BoundField DataField="JobSuffix" HeaderText="Suffix" SortExpression="JobSuffix" />
<asp:BoundField DataField="WorkCenter_Desc" HeaderText="Work Ctr" ReadOnly="True"
SortExpression="WorkCenter_Desc" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="AssignedDate" HeaderText="Assigned Date" SortExpression="AssignedDate" />
<asp:BoundField DataField="CompleteDate" HeaderText="Complete Date" SortExpression="CompleteDate" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objRequests" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetRequestsByRequestDate" TypeName="GAI_DataAccessLayerTableAdapters.disp_DispatchRequestTableAdapter">
<SelectParameters>
<asp:ControlParameter ControlID="txtStartDate" Name="Startdate" PropertyName="Text"
Type="DateTime" />
<asp:ControlParameter ControlID="txtEndDate" Name="Enddate" PropertyName="Text" Type="DateTime" />
</SelectParameters>
</asp:ObjectDataSource>
</span>
</p>
</div>
</form>
</body>
</html>
Re: [2005] Using user input as Gridview Parameters
Thread moved to ASP.Net forum
Re: [2005] Using user input as Gridview Parameters
Hey,
Have you had a look at using the Validator Controls that ship with ASP.Net? In your case, I think you should be able to use the CompareValidator to ensure the dates are valid.
Hope that helps!!
Gary
You could use
Re: [2005] Using user input as Gridview Parameters
I can do the validation but that is not the issue. I need to basically refresh the data in the gridview so it uses my 2 textboxes as the new values for my startdate and enddate parameters. txtStartDate and txtEndDate are the fields I initially used as the source for my parameter values. I just need to update the values of the parameters and I am unsure on how to do that.
Re: [2005] Using user input as Gridview Parameters
Hey,
This is a bit of a stab in the dark, but can you not use the InputParameters collection of the ObjectDataSourceSelectingEventArgs to set the value of the parameter used in the query?
Hope that helps!!
Gary
Re: [2005] Using user input as Gridview Parameters
I looked at the link and it seems this and all links talk about adding parameters. When I did that it created two more parameters, when all I want to do is update 2 parameters I have with the updated values when the user clicks a Show Records button.
I was thinking I might be able to remove the parameters and readd them?
Reason I say that is when I write code to add parameters I get this message:
ObjectDataSource 'objRequests' could not find a non-generic method 'GetRequestsByRequestDate' that has parameters: Startdate, Enddate, Startdate1, Enddate1.
Here is the code piece:
Protected Sub objRequests_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles objRequests.Selecting
Dim paramFrom As New Parameter
Dim paramTo As New Parameter
paramFrom.Name = "Startdate"
paramTo.Name = "Enddate"
paramFrom.Type = TypeCode.DateTime
paramTo.Type = TypeCode.DateTime
paramFrom.DefaultValue = Me.txtStartDate.Text
paramTo.DefaultValue = Me.txtEndDate.Text
objRequests.SelectParameters.Add(paramFrom)
objRequests.SelectParameters.Add(paramTo)
End Sub
Re: [2005] Using user input as Gridview Parameters
So have you tried something like the following:
Code:
e.InputParameters("Startdate") = Me.txtStartDate.Text
Another option would be to step through the ObjectDataSource configuration wizard, from the design surface, and link the Parameters to the control id's that you want to use.
Gary
Re: [2005] Using user input as Gridview Parameters
This worked once I put it in the Selecting event of the Data Source Object.
Thanks!
Re: [RESOLVED] [2005] Using user input as Gridview Parameters
Good stuff, glad to hear you got it working!!
Gary