Solved

ASP.NET C#

Posted on 2011-02-18
4
368 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
[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
  • 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

Technology Partners: 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

628 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