Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET C#

Posted on 2011-02-18
4
Medium Priority
?
371 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:
Rahul Agarwal earned 2000 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:Rahul Agarwal
Rahul Agarwal earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

773 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