Solved

Getting a gridview to refresh to reflect change in parameters

Posted on 2011-09-30
2
363 Views
Last Modified: 2013-12-16
I have a gridview that uses an object data source that has parameters linked to two date fields on the page.  

 I set the date values in page load, but when the page first loads, the query in the object data source, GetIssueDetails, says the date is 1/1/0001.  

Then I set the dates on the form and click a button that does a databind for the gridview, and the date in GetIssueDetails is still 1/1/001.  

If is set the dates in GetIssueDetails, the gridview shows the data for that date range.

How come GetIssueDetails isn't seeing the date values I expect?

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetIssueDetails"
        TypeName="Issues">
        <SelectParameters>
            <asp:FormParameter DefaultValue="" FormField="txtStartDate" Name="StartDate"
                Type="DateTime" />
            <asp:FormParameter FormField="txtEndDate" Name="EndDate" Type="DateTime" />
        </SelectParameters>
    </asp:ObjectDataSource>

    <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
        EnableModelValidation="True">
    </asp:GridView>

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtStartDate.Text = DateTime.Today.ToShortDateString();
            this.txtEndDate.Text = DateTime.Today.ToShortDateString();
        }
    }
    protected void btnViewDetails_Click(object sender, EventArgs e)
    {
        this.GridView1.DataBind();
    }

    public static DataSet GetIssueDetails(DateTime StartDate, DateTime EndDate)
    {
        DateTime MinDate = DateTime.Parse("1/1/2011");
        string selectSQL;

        if (DateTime.Compare(StartDate, MinDate) < 0)
        {
            StartDate = DateTime.Today;
            EndDate = StartDate;
        }
0
Comment
Question by:Barry Sweezey
2 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36897007
I usually find that most of my problems with ASP.NET is when I forget about the page life cycle.  Since I don't use the ObjectDataSource, I am not sure when it calls the SelectMethod, but my guess from your testing is that it is before the values are set.

...or...

When the page is first loaded, there will not be a form field, and on post-back, the value is retrieved from the view state.
0
 
LVL 8

Accepted Solution

by:
jagrut_patel earned 500 total points
ID: 36908183
1. Subscribe to the OnSelecting event of the ObjectDataSource

<asp:ObjectDataSource ID="ObjectDataSource1"..... OnSelecting="ObjectDataSource1_Selecting">

Open in new window


2. Implement ObjectDataSource1_Selecting

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["StartDate"] = this.txtStartDate.Text;
    e.InputParameters["EndDate"] = this.txtEndDate.Text;
}

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question