在ASP.Net GridView控件中选择 [英] Selection in ASP.Net GridView control
本文介绍了在ASP.Net GridView控件中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个gridview,其中我有第100行.我使用占位符动态创建了链接按钮.现在,如果我单击第89个链接按钮,则会自动选择第89行并滚动到该行.下面是我的代码.
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();
}
推荐答案
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()">
这篇关于在ASP.Net GridView控件中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文