terbubbs
terbubbs

Reputation: 1512

OnRowEditing Edit Textbox Focus

What I'm trying to do is, upon the GridView_OnRowEditing event, I want to focus on the textbox that appears in the cell I am editing. From there I will trigger a javascript keyboard, but I need to focus on the textbox first before I can do anything.

Here is my current code:

protected void GridView_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        var row = GridView.Rows[e.NewEditIndex];
        var gridText = (TextBox) row.Cells[5].Controls[0];
        gridText.Focus();
    }

EDIT:

Here's my GridView mark up. I'm not manually setting a textbox. If there is anyway to retrieve an ID of the cell that is converted to the textbox on RowEditing event, please let me know. Thank you.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="RollNumber" DataSourceID="WoSource" HorizontalAlign="Center" 
        OnRowUpdating="GridView1_RowUpdating" AllowSorting="true" Visible="True" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_OnRowEditing" OnRowCancelingEdit="GridView1_OnRowCancelingEdit" Width="100%">
        <Columns>
            <asp:BoundField DataField="WorkOrderNo" HeaderText="WorkOrderNo" ReadOnly="True" SortExpression="WorkOrderNo" Visible="False"/>
            <asp:BoundField DataField="RollNumber" HeaderText="RollNumber" ReadOnly="True" SortExpression="RollNumber" />
            <asp:BoundField DataField="ModelNumber" HeaderText="ModelNumber" ReadOnly="True" SortExpression="ModelNumber" />
            <asp:BoundField DataField="QtyGood" HeaderText="QtyGood" ReadOnly="True" SortExpression="QtyGood" />
            <asp:BoundField DataField="QtyRun" HeaderText="QtyRun" SortExpression="QtyRun"><ControlStyle Width="100%" Height="100%" Font-Size="36px"></ControlStyle></asp:BoundField>
        </Columns>
    </asp:GridView>

Upvotes: 0

Views: 1179

Answers (1)

terbubbs
terbubbs

Reputation: 1512

With some help from @okisinch I discovered I could do this:

        $('#<%=GridView1.ClientID%> :input:enabled:visible:first').focus();
        $('#<%=GridView1.ClientID%> :input:enabled:visible:first').select();

The benefit of this is that I wouldn't need to find an textbox ID to be able to focus.

This link was the resource that helped me.

Edit: Discovered this code works as well. Note that the #insideContainer is a jquery keyboard that was implemented. I figured I would add the triggering that I did.

    $(function () {
        if ($('#<%=GridView1.ClientID%> :input:enabled:visible:first').val()) {
            $('#<%=GridView1.ClientID%> :input:enabled:visible:first').focus();
            $('#<%=GridView1.ClientID%> :input:enabled:visible:first').select();
            $('#insideContainer').show();
        } else {
            $('#insideContainer').hide();
        }
    });

Upvotes: 1

Related Questions