Solved

Getting a gridview to refresh to reflect change in parameters

Posted on 2011-09-30
2
361 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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now