Hi,
To solve your problem, let's using javascript with c#:
In html code type the javascript below in your
head tag.
This code will fire when you press the key in the textbox.
The javascript code will find a text box element, save the value and fire a button in the C # code-behind.
<script type="text/javascript">
function KeyPressText(id_row, text, button)
{
document.getElementById(text).value = id_row;
document.getElementById(button).click();
}
</script>
Below your gridview, add two elements, 1 textbox and 1 button. This component will stay hidden in your page.
<asp:TextBox ID="txtIndexRow" runat="server" style="visibility:hidden;"></asp:TextBox>
<asp:Button ID="btnOcIndex" runat="server" Text="FindRow" OnClick="btnOcIndex_Click" style="visibility:hidden;"/>
Create the
OnClick event for your hidden button and add the code where you read the index of the grid.
protected void btnOcIndex_Click(object sender, EventArgs e)
{
string indexrow = this.txtIndexRow.Text;
}
In your gridvew add the
OnRowCreated event and add the code below.
This code add the attribute
onkeypress in the textbox, calling the javascript function, for each line in the grid.
protected void grd_result_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
((TextBox)e.Row.FindControl("TextBox1")).Attributes["onkeypress"] = "KeyPressText('" + e.Row.RowIndex.ToString() + "', '" + txtIndexRow.ClientID + "', '" + this.btnOcIndex.ClientID + "');";
}
}
Here! The complete code.
Aspx Page
<head runat="server">
<title></title>
<script type="text/javascript">
function KeyPressText(id_row, text, button)
{
document.getElementById(text).value = id_row;
document.getElementById(button).click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grd_result" OnRowCreated="grd_result_RowCreated">
<Columns>
<asp:TemplateField HeaderText="Destination">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="txtIndexRow" runat="server" style="visibility:hidden;"></asp:TextBox>
<asp:Button ID="btnOcIndex" runat="server" Text="FindRow" OnClick="btnOcIndex_Click" style="visibility:hidden;"/>
</div>
<br />
</form>
</body>
Code-behind
protected void grd_result_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
((TextBox)e.Row.FindControl("TextBox1")).Attributes["onkeypress"] = "KeyPressText('" + e.Row.RowIndex.ToString() + "', '" + txtIndexRow.ClientID + "', '" + this.btnOcIndex.ClientID + "');";
}
}
protected void btnOcIndex_Click(object sender, EventArgs e)
{
string indexrow = this.txtIndexRow.Text;
}