Select DropDownList Item pressing Number key as Index (Inside a Gridview)

JavaScript Code

<script type=”text/javascript”>

function ddlType_onkeypress(e, ddlName) {

if (e.keyCode == 96 || e.keyCode == 48)
document.getElementById(ddlName).selectedIndex = 0;
if (e.keyCode == 97 || e.keyCode == 49)
document.getElementById(ddlName).selectedIndex = 1;
if (e.keyCode == 98 || e.keyCode == 50)
document.getElementById(ddlName).selectedIndex = 2;
if (e.keyCode == 99 || e.keyCode == 51)
document.getElementById(ddlName).selectedIndex = 3;
if (e.keyCode == 100 || e.keyCode == 52)
document.getElementById(ddlName).selectedIndex = 4;
if (e.keyCode == 101 || e.keyCode == 53)
document.getElementById(ddlName).selectedIndex = 5;
if (e.keyCode == 102 || e.keyCode == 54)
document.getElementById(ddlName).selectedIndex = 6;
if (e.keyCode == 103 || e.keyCode == 55)
document.getElementById(ddlName).selectedIndex = 7;
if (e.keyCode == 104 || e.keyCode == 56)
document.getElementById(ddlName).selectedIndex = 8;
if (e.keyCode == 105 || e.keyCode == 57)
document.getElementById(ddlName).selectedIndex = 9;
}

$(document).ready(function () {
$(‘#<%=gvResultEntry.ClientID %>’).Scrollable();
}
)
</script>

//

.aspx Code

<asp:GridView ID=”gvResultEntry” SkinID=”MegaGrid” runat=”server” AutoGenerateColumns=”False”
EmptyDataText=”No Default Course Found” AllowSorting=”True” OnRowDataBound=”gvResultEntry_RowDataBound”
BackColor=”White” BorderColor=”#000″ BorderStyle=”Solid” BorderWidth=”4px” AlternatingRowStyle-BackColor=”Bisque”>
<Columns>

<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID=”Sub1″ Visible=”true” runat=”server”></asp:Label>
</HeaderTemplate>
<HeaderStyle Width=”80px” HorizontalAlign=”Center” />
<ItemStyle Width=”80px” HorizontalAlign=”Center” />
<ItemTemplate>
<asp:DropDownList ID=”ddlSub1″ SkinID=”VerySortDDL” runat=”server”>
<asp:ListItem>-</asp:ListItem>
<asp:ListItem>A+</asp:ListItem>
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>A-</asp:ListItem>
<asp:ListItem>B+</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>B-</asp:ListItem>
<asp:ListItem>C+</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<FooterStyle BackColor=”White” ForeColor=”#000066″ />
<HeaderStyle BackColor=”#006699″ Font-Bold=”True” ForeColor=”White” BorderStyle=”Solid” />
<PagerStyle BackColor=”White” ForeColor=”#000066″ HorizontalAlign=”Left” />
<RowStyle ForeColor=”#000066″ />
<SelectedRowStyle BackColor=”#669999″ Font-Bold=”True” ForeColor=”White” />
<SortedAscendingCellStyle BackColor=”#F1F1F1″ />
<SortedAscendingHeaderStyle BackColor=”#007DBB” />
<SortedDescendingCellStyle BackColor=”#CAC9C9″ />
<SortedDescendingHeaderStyle BackColor=”#00547E” />
</asp:GridView>

.cs Code

protected void gvResultEntry_RowDataBound(object sender, GridViewRowEventArgs e)
{
String SqlStr = "SELECT CourseCode FROM T_Term_Course_Offer WHERE TermId='" + ddlYearSelect.Text + "'";
DataTable dt = ClsCommon.GetAdhocResult(SqlStr).Tables[0];
Int32 Count = dt.Rows.Count;
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < Count; i++)
{
e.Row.Cells[i + 2].Text = dt.Rows[i][0].ToString();
}
}

//int count = e.Row.Cells.Count - 1;
//for (int I = 0; I <= count; I++)
//{
// if (I % 2 == 0)
// {
// e.Row.Cells[I].BackColor = System.Drawing.Color.AliceBlue;
// }
// else
// {
// e.Row.Cells[I].BackColor = System.Drawing.Color.Chocolate;
// }
//}

////gvResultEntry.AlternatingRowStyle.BackColor = Color.Salmon;
//gvResultEntry.HeaderStyle.BackColor = Color.Bisque;
//gvResultEntry.RowStyle.BorderStyle = BorderStyle.Solid;
//gvResultEntry.RowStyle.BorderColor = Color.BurlyWood;
//gvResultEntry.BorderColor = Color.BurlyWood;

if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlSub1 = (DropDownList)e.Row.FindControl("ddlSub1");
DropDownList ddlSub2 = (DropDownList)e.Row.FindControl("ddlSub2");
DropDownList ddlSub3 = (DropDownList)e.Row.FindControl("ddlSub3");
DropDownList ddlSub4 = (DropDownList)e.Row.FindControl("ddlSub4");
DropDownList ddlSub5 = (DropDownList)e.Row.FindControl("ddlSub5");
DropDownList ddlSub6 = (DropDownList)e.Row.FindControl("ddlSub6");
DropDownList ddlSub7 = (DropDownList)e.Row.FindControl("ddlSub7");
DropDownList ddlSub8 = (DropDownList)e.Row.FindControl("ddlSub8");
DropDownList ddlSub9 = (DropDownList)e.Row.FindControl("ddlSub9");
DropDownList ddlSub10 = (DropDownList)e.Row.FindControl("ddlSub10");
DropDownList ddlSub11 = (DropDownList)e.Row.FindControl("ddlSub11");
DropDownList ddlSub12 = (DropDownList)e.Row.FindControl("ddlSub12");
DropDownList ddlSub13 = (DropDownList)e.Row.FindControl("ddlSub13");
DropDownList ddlSub14 = (DropDownList)e.Row.FindControl("ddlSub14");
DropDownList ddlSub15 = (DropDownList)e.Row.FindControl("ddlSub15");
DropDownList ddlSub16 = (DropDownList)e.Row.FindControl("ddlSub16");
DropDownList ddlSub17 = (DropDownList)e.Row.FindControl("ddlSub17");
DropDownList ddlSub18 = (DropDownList)e.Row.FindControl("ddlSub18");
DropDownList ddlSub19 = (DropDownList)e.Row.FindControl("ddlSub19");
DropDownList ddlSub20 = (DropDownList)e.Row.FindControl("ddlSub20");
DropDownList ddlSub21 = (DropDownList)e.Row.FindControl("ddlSub21");
DropDownList ddlSub22 = (DropDownList)e.Row.FindControl("ddlSub22");
DropDownList ddlSub23 = (DropDownList)e.Row.FindControl("ddlSub23");
DropDownList ddlSub24 = (DropDownList)e.Row.FindControl("ddlSub24");
DropDownList ddlSub25 = (DropDownList)e.Row.FindControl("ddlSub25");
DropDownList ddlSub26 = (DropDownList)e.Row.FindControl("ddlSub26");

ddlSub1.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub1.ClientID + "');");
ddlSub2.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub2.ClientID + "');");
ddlSub3.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub3.ClientID + "');");
ddlSub4.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub4.ClientID + "');");
ddlSub5.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub5.ClientID + "');");
ddlSub6.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub6.ClientID + "');");
ddlSub7.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub7.ClientID + "');");
ddlSub8.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub8.ClientID + "');");
ddlSub9.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub9.ClientID + "');");
ddlSub10.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub10.ClientID + "');");
ddlSub11.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub11.ClientID + "');");
ddlSub12.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub12.ClientID + "');");
ddlSub13.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub13.ClientID + "');");
ddlSub14.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub14.ClientID + "');");
ddlSub15.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub15.ClientID + "');");
ddlSub16.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub16.ClientID + "');");
ddlSub17.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub17.ClientID + "');");
ddlSub18.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub18.ClientID + "');");
ddlSub19.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub19.ClientID + "');");
ddlSub20.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub20.ClientID + "');");
ddlSub21.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub21.ClientID + "');");
ddlSub22.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub22.ClientID + "');");
ddlSub23.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub23.ClientID + "');");
ddlSub24.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub24.ClientID + "');");
ddlSub25.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub25.ClientID + "');");
ddlSub26.Attributes.Add("onkeyup", "javascript:return ddlType_onkeypress(event,'" + ddlSub26.ClientID + "');");
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s