Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Asp.net 3.5 web Application,C#

how can i click a gridview row without using mouse

I have a gridview when i click a row then im able to use up & down Arrow keys on the gridview

I want that when the gridview is populated then the first row automatically get clicked, so that i dont have to click it again to go up and down using arrow keys

Please help

Thanks
Faiz
Posted

What you are seeking is termed as setting focus on first row such that keyboard works as per you want.

Try:
C#
// GridView1 is the name of your GridView
// After grid databind, set focus to 1st Row
GridView1.Rows[0].Focus(); 
 
Share this answer
 
Hi ,
I'm not sure if i get you right but hope it help you
Maybe it will Give you idea for what you looking for .
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.Button2_Click(this, new EventArgs());

    }
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
    Response.Write("<script>alert('Button 2')</script>");
}


Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Faiz_Khan 23-Jun-12 2:32am    
Following is the code im using fo navigating Gridview

var SelectedRow = null;
var SelectedRowIndex = null;
var UpperBound = null;
var LowerBound = null;

window.onload = function()
{
//UpperBound = 8;
LowerBound = 0;
SelectedRowIndex = -1;
}
function SelectRow(CurrentRow, RowIndex)
{
if(SelectedRow == CurrentRow || RowIndex > UpperBound || RowIndex < LowerBound)
return;

if(SelectedRow != null)
{
SelectedRow.style.backgroundColor = SelectedRow.originalBackgroundColor;
SelectedRow.style.color = SelectedRow.originalForeColor;
}

if(CurrentRow != null)
{
CurrentRow.originalBackgroundColor = CurrentRow.style.backgroundColor;
CurrentRow.originalForeColor = CurrentRow.style.color;
CurrentRow.style.backgroundColor = '#ff9933';
CurrentRow.style.color = 'Black';
}

SelectedRow = CurrentRow;
SelectedRowIndex = RowIndex;
if(SelectedRow !=null && SelectedRow != '' && SelectedRow != '0')
{
setTimeout("SelectedRow.focus();",0);
}
}

function SelectSibling(e)
{
if(SelectedRow !=null && SelectedRow != '' && SelectedRow != '0')
{
var e = e ? e : window.event;
var KeyCode = e.which ? e.which : e.keyCode;
UpperBound = parseInt(document.getElementById('<%=NoOfRow.ClientID %>').value) - 1;
if(KeyCode == 40)
SelectRow(SelectedRow.nextSibling, SelectedRowIndex + 1);
else if(KeyCode == 38)
SelectRow(SelectedRow.previousSibling, SelectedRowIndex - 1);
else if(KeyCode == 13)
{
var control = '<%=CurRow.ClientID %>';
document.getElementById(control).value = SelectedRowIndex;
document.getElementById('<%= btnAddFromGrid.ClientID %>').click();
}
return false;
}
}


protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
e.Row.TabIndex = -1;
e.Row.Attributes["onclick"] = string.Format("javascript:SelectRow(this, {0});", e.Row.RowIndex);
e.Row.Attributes["onkeydown"] = "javascript:return SelectSibling(event);";
e.Row.Attributes["onselectstart"] = "javascript:return false;";
}
}
private void FillGridBuyer(string pm)
{
Regex r = new Regex("[0-9]");
if (r.Match(pm).Success)
SqlQry = "SELECT Buyer_Id, Buyer_Name, Contact_Person, Branch FROM Buyer_Master WHERE (Branch = '" + Session["BRANCH"].ToString() + "') and Buyer_code like '%" + pm + "%'";

OF.FillGrid_Without_SP(GVBUY, SqlQry);
NoOfRow.Value = GVBUY.Rows.Count.ToString();
HFNameOfGrid.Value = "GVBUY";
}
Now the Problem is unless i click the Gridview the SelectedRow and SelectedRow.nextSibling will not get.

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