Solved

Getting a gridview to refresh to reflect change in parameters

Posted on 2011-09-30
2
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

740 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