In ASP. NET, the top and right buttons on the keyboard control the selection and paging of the DataGrid items
The procedure is as follows:
In ASP. NET, the top and right buttons on the keyboard control the selection and paging of the DataGrid items
The procedure is as follows:
1. On the. ASPX page, drag the two control server control IDs: datagrid1 and client control hidden type IDs: tbxindex.
Code As follows:
<Div style = "Z-INDEX: 105; left: 8px; width: 100%; position: absolute; top: 144px; align: Center">
<Asp: DataGrid id = "datagrid1" runat = "server" allowpaging = "true" autogeneratecolumns = "false" width = "100%">
<Columns>
<Asp: boundcolumn headertext = "Index">
<Headerstyle wrap = "false" horizontalalign = "center" width = "36px">
<Itemstyle wrap = "false" horizontalalign = "center"> </itemstyle>
</ASP: boundcolumn>
<Asp: boundcolumn datafield = "CPID" headertext = "Project ID">
<Headerstyle wrap = "false" horizontalalign = "center" width = "72px">
<Itemstyle wrap = "false" horizontalalign = "center"> </itemstyle>
</ASP: boundcolumn>
<Asp: boundcolumn datafield = "cpname" headertext = "project name">
<Headerstyle horizontalalign = "center">
</ASP: boundcolumn>
</Columns>
<Pagerstyle horizontalalign = "right" Wrap = "false" mode = "numericpages"> </pagerstyle>
</ASP: DataGrid>
</Div>
<Input type = "hidden" runat = "server" id = "tbxindex" style = "Z-INDEX: 106; left: 8px; position: absolute; top: 104px; design_time_lock: true"
Design_time_lock = "true">
2. Add the following JS Code in. aspx:
<Script language = "JavaScript">
Function tr_move (ET)
{
Et. style. backgroundcolor = "red ";
VaR dpdnnow = ET. Children. Item (0 );
Please wait Doc ument. getelementbyid ("tbxindex"). value = dpdnnow. innertext;
}
Function tr_mout (ET)
{
Et. style. backgroundcolor = "# ffffff ";
}
Function iskeydown ()
{
VaR objtbrows = window.doc ument. getelementbyid ("datagrid1"). Rows;
VaR objindex = parseint(registry.doc ument. getelementbyid ("tbxindex"). value );
// Keyboard down
If (window. event. keycode = 40)
{
Tablegetbackcolor (1 );
}
// Keyboard up
If (window. event. keycode = 38)
{
Tablegetbackcolor (-1 );
}
// Left paging of the keyboard
If (window. event. keycode = 37)
{
If (objindex =-1)
{
Objindex = 0;
}
VaR pageindex = parseint (objindex)/(window.w.doc ument. getelementbyid ("datagrid1"). Rows. Length-2 ));
If (objindex> = parseint (objtbrows (1 ). cells (0 ). innertext) & pageindex <= parseint (objtbrows. length-2 ). cells (0 ). innertext ))
{
Pageindex = pageindex-1;
}
VaR objtablen = "maid $ _ ctl14 $ _ CTL" + pageindex;
_ Dopostback (objtablen ,'');
}
// Page to the right of the keyboard
If (window. event. keycode = 39)
{
If (objindex =-1)
{
Objindex = 0;
}
VaR pageindex = parseint (objindex + 1)/(window.w.doc ument. getelementbyid ("datagrid1"). Rows. Length-2 ));
VaR objtablen = "maid $ _ ctl14 $ _ CTL" + (pageindex + 1 );
_ Dopostback (objtablen ,'');
}
}
Function tablegetbackcolor (objvalue)
{
VaR nowvalue = invalid argument Doc ument. getelementbyid ("tbxindex"). value;
VaR objtbrows = window.doc ument. getelementbyid ("datagrid1"). Rows;
For (VAR irow = 1; irow <objtbrows. Length-1; irow ++)
{
VaR objrow = objtbrows (irow );
If (parseint (nowvalue) + objvalue = parseint (objrow. cells (0). innertext ))
{
If (objvalue = 1)
{
Tr_move (objrow );
Tr_mout (objtbrows (irow-1 ));
Break;
}
Else if (objvalue =-1)
{
Tr_move (objtbrows (irow ));
Tr_mout (objtbrows (irow + 1 ));
Break;
}
}
}
}
</SCRIPT>
3. Add the following code:
<Body Language = "JavaScript" onkeydown = "iskeydown ()" ms_positioning = "gridlayout">
4. Write the corresponding background code. CS File
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
Datagrid1.currentpageindex = 0;
Binddatagrid ();
This. tbxindex. value = "-1 ";
}
// Place user code here to initialize the page
}
///
// bind the DataGrid
///
private void binddatagrid ()
{< br> string strcon = "workstation id = (local); packet size = 4096; user id = sa; Data Source = (local ); persist Security info = true; initial catalog = cpro; Password = 1234 ";
string strsql =" select Top 100 * From cpro ";
system. data. sqlclient. sqlconnection conn = new sqlconnection (strcon);
system. data. sqlclient. sqldataadapter Ada = new sqldataadapter (strsql, strcon);
dataset DS = new dataset ();
Ada. fill (DS);
maid = Ds;
maid ();
}
private void maid (Object sender, system. web. UI. webcontrols. datagriditemeventargs e)
{< br> If (E. item. itemindex <0)
return;
If (E. item. itemtype = listitemtype. item | E. item. itemtype = listitemtype. alternatingitem |
E. item. itemtype = listitemtype. selecteditem)
{< br> E. item. cells [0]. TEXT = convert. tostring (maid + E. item. itemindex);
E. item. attributes. add ("onmouseover", "tr_move (this)");
E. item. attributes. add ("onmouseout", "tr_mout (this)");
}< BR >}
Private void datagrid1_pageindexchanged (Object source, system. Web. UI. webcontrols. datagridpagechangedeventargs E)
{
Datagrid1.currentpageindex = E. newpageindex;
Binddatagrid ();
This. tbxindex. value = convert. tostring (maid-1 );
}
Run OK.