Solved

ASP.NET C#

Posted on 2011-02-18
4
362 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

11 Experts available now in Live!

Get 1:1 Help Now