Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have one gridview,in this i have 100rd rows.I created linkbuttons dynamcally using placeholder.now if i click 89th linkbutton automatically select 89th row and scroll to that row.Below is my code.
string constr = ConfigurationManager.ConnectionStrings["narayanaConnectionString"].ConnectionString;
    SqlConnection cn;
    protected void Page_Load(object sender, EventArgs e)
    {
        LinkButton lb;
        if (!IsPostBack)
        {
            
            bind();
            
        }
        
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            lb = new LinkButton();
            
            lb.ID = "LinkButton" + i.ToString();
            lb.Text = ""+GridView1.Rows[i].Cells[0].Text;
            lb.Font.Name = "Verdana";
            PlaceHolder1.Controls.Add(lb);
            lb.Click +=new EventHandler(lb_Click);
        }
    }
    protected void lb_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        int i=Convert.ToInt16 (lb.Text );
        GridView1.SelectedIndex = i - 1;
        GridView1.SelectedRow.Cells[0].Focus();
        
        GridView1.SelectedRow.Cells[0].BackColor = Color.Red;
    }
    public void bind()
    {
        cn = new SqlConnection(constr);
        SqlDataAdapter da = new SqlDataAdapter("select * from student", cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
Posted
Updated 19-Apr-11 22:58pm
v3
Comments
Toniyo Jackson 20-Apr-11 4:55am    
Added pre tag
Prerak Patel 20-Apr-11 4:58am    
What is the problem then?
Sandeep Mewara 20-Apr-11 4:58am    
So, come again... what is the issue?

1 solution

XML
Add following code at client side

<script>
function setscroll()
        {
            var gridCol = document.getElementById("GridView1").getElementsByTagName("td")
            if(gridCol.length > 0)
            {
                var rowCOunt = 0;
                while(rowCOunt < gridCol.length)
                {
                    if(gridCol[rowCOunt].style.backgroundColor=="red")
                    {
                        window.scrollTo(0,gridCol[rowCOunt].offsetTop);
                        break;
                    }
                    rowCOunt +=1;
                }
            }


        }
</script>

ad call this function in page body on load
<body onload="setscroll()">
 
Share this answer
 
Comments
mksharma38 21-Apr-11 1:03am    
hi,
I have tested above code it is working.
For ur help u can follow following steps:
1. Make sure grid cell color is changing on click on link butoon.
2. Add alert(gridCol[rowCOunt].style.backgroundColor)
before line if(gridCol[rowCOunt].style.backgroundColor=="red")
mksharma38 21-Apr-11 1:50am    
when a grid render it convert in a table, you can see the source code of ur web page u will find a table in place of grid.
before line if(gridCol.length > 0)
u can put alert(gridCol.length) and find the number of "td"
mksharma38 21-Apr-11 1:47am    
in alret you will get color name that u have set on grid cell. color name in alret and in line must be same (case sensetive).
Finally u have to check window.scrollTo(0,gridCol[rowCOunt].offsetTop);
is executing or not, which condition is faild.


FInnaly logic to set scroll popsition is:
1. document.getElementById("GridView1").getElementsByTagName("td"), it will get all the coloum of grid "GridView1"
2. while(rowCOunt < gridCol.length, it will loop all the colomn of grid
3. if(gridCol[rowCOunt].style.backgroundColor=="red, it will check the colur which u have set on grid cell.
4. gridCol[rowCOunt].offsetTop, it will get the position of colored cell.
5. and finally window.scrollTo(0,gridCol[rowCOunt].offsetTop);, it will set the position of scroll.
mksharma38 21-Apr-11 1:51am    
where was problem ....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900