Solved

ASP.NET C#

Posted on 2011-02-18
4
364 Views
Last Modified: 2012-05-11
In my code, for the first time, system will automatically select the current year row... (code below)

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (Session["selected_year"] == null)
            {
                Session["selected_year"] = DateTime.Now.Year;
            }

            if (((Label)e.Row.Cells[0].FindControl("lblYear")) != null)
            {
                if (((Label)e.Row.Cells[0].FindControl("lblYear")).Text == Session["selected_year"].ToString())
                {
                    e.Row.RowState = DataControlRowState.Selected;
                }
            }
        }

Open in new window


then when user click the submit button....it will get the selected row of the GridView1....

however, it said the current selected row is null (partial code attached below)

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool hasError = false;
            string selectedYear = "";

            message1.Visible = false;
            message2.Visible = false;


            if (hasError == false)
            {
                GridViewRow row = GridView1.SelectedRow;
                if (row == null)
                {
                    hasError = true;
                    message1.Text = "Please select year";
                    message1.Visible = true;
                }
            }

Open in new window


why?????
0
Comment
Question by:mawingho
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
agarwalrahul earned 500 total points
ID: 34931301
Just Put the if condition to Check wether its a Header Row or Datarow, because every time in Gridview_RowDataBound event Header Controls are first Check then only the Datarow controls are checked.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (Session["selected_year"] == null)
            {
                Session["selected_year"] = DateTime.Now.Year;
            }
if ((e.Row.RowType == DataControlRowType.Header)) {
            if (((Label)e.Row.Cells[0].FindControl("lblYear")) != null)
            {
                if (((Label)e.Row.Cells[0].FindControl("lblYear")).Text == Session["selected_year"].ToString())
                {
                    e.Row.RowState = DataControlRowState.Selected;
                }
            }
        }
}
0
 

Author Comment

by:mawingho
ID: 34931353
It doesn't work.....
When the page first load, it can really select that row...

but after I click submit....(as you can see my submit code in my first post)
it cannot get the GridView1.SelectedRow....
I don't know why....
 pic
if the user actually use the mouse and click the SELECT link button, it works....
but if the user does not click it, it doesn't work, why

My code should allow user no need to select the year if they want to see the current year.....becoz it's already selected for them
0
 
LVL 13

Assisted Solution

by:agarwalrahul
agarwalrahul earned 500 total points
ID: 34941495
I Think the session value is lost after pressing the submit button, because if the session value retained the selected row is always selected. Please check wether the Session value is retains or not.
0
 

Author Closing Comment

by:mawingho
ID: 34977006
thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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