Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET C#

Posted on 2011-02-18
4
Medium Priority
?
370 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:
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

662 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