如何在从textchange事件回发后将焦点设置到当前gridview行中的特定文本框

时间:2015-05-27 13:09:49

标签: c# asp.net gridview

在GridView中获得了一系列启用了回发的文本框。当我在其中一个txtQuantity中键入一些文本并移动到txtUnit文本框时,它会触发OnTextChanged事件并进行回发以计算一些总计。但是txtUnit控件失去了焦点,我必须用鼠标重新选择它。有必要这样做有点烦人,有没有办法在回发期间保持对这些控件的关注

ASPX页面

<asp:gridview ID="grdOrder" CssClass="table table-hover" GridLines="None" 
            runat="server" ShowFooter="true" 
        AutoGenerateColumns="false"  ClientIDMode="Static"
        onrowdatabound="grdOrder_RowDataBound" HeaderStyle-CssClass="gridheader">

       <Columns>

        <asp:TemplateField HeaderText="Product">

            <ItemTemplate>

            <asp:Label ID="lblProductId" runat="server" Text='<%# Eval("INVENTORY_ITEM") %>' Visible = "false" />
                <asp:DropDownList ID="ddlProduct" runat="server" 
                    ClientIDMode="Static" class="form-control input-sm"
                     AutoPostBack="true" onselectedindexchanged="ddlProduct_SelectedIndexChanged">
                </asp:DropDownList>

            </ItemTemplate>

        </asp:TemplateField>

        <asp:TemplateField HeaderText="Qty">

            <ItemTemplate>

                <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("QUANTITY")%>'  class="form-control input-sm" Style=" text-align:right;"
                     ClientIDMode="Static" onkeypress = "return IsDecimal(this);" ontextchanged="txtQuantity_TextChanged" AutoPostBack="true"></asp:TextBox>

            </ItemTemplate>
]
        </asp:TemplateField>

        </asp:TemplateField>

        <asp:TemplateField HeaderText="Unit">

            <ItemTemplate>
                 <asp:TextBox ID="txtUnit" runat="server" class="form-control input-sm" AutoPostBack="true" Text='<%#Eval("UNIT_NAME")%>' 
                 ontextchanged="txtUnit_TextChanged" ClientIDMode="Static"></asp:TextBox>
            </ItemTemplate>

        </asp:TemplateField>

        </Columns>

</asp:gridview>

代码背后:

protected void txtQuantity_TextChanged(object sender, EventArgs e)
{               
    TextBox txtQuantity = (TextBox)sender;
    GridViewRow gridViewRow = (GridViewRow)txtQuantity.NamingContainer;
    DropDownList ddlProduct = (DropDownList)gridViewRow.FindControl("ddlProduct");
    Label lblGrossQuantity = (Label)gridViewRow.FindControl("lblGrossQuantity");
    TextBox txtUnit = (TextBox)gridViewRow.FindControl("txtUnit");

    //gridViewRow.Cells[3].FindControl("txtUnit").Focus();
    txtUnit.Focus();
}

3 个答案:

答案 0 :(得分:0)

问题是因为PostBack在您的_TextChanged EventHandler 之后发生。因此,您需要在那里捕获一些内容,并将其传递给PreRender。使用Session变量控制焦点PostBack并在Page_PreRender()中访问它。

protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
     TextBox txtQuantity = (TextBox)sender;
     GridViewRow gridViewRow = (GridViewRow)txtQuantity.NamingContainer;
     DropDownList ddlProduct = (DropDownList)gridViewRow.FindControl("ddlProduct");
     Label lblGrossQuantity = (Label)gridViewRow.FindControl("lblGrossQuantity");
     TextBox txtUnit = (TextBox)gridViewRow.FindControl("txtUnit");

     //gridViewRow.Cells[3].FindControl("txtUnit").Focus();
     Session["event_control"] = ((TextBox)gridViewRow.FindControl("txtUnit"));
}

protected void Page_PreRender(object sender, EventArgs e)
{
    try
    {
        if (Session["event_control"] != null)
        { 
            TextBox control = (TextBox) Session["event_control"];
            control.Focus(); 
        }
    }
    catch (InvalidCastException inEx)
    {
    }        
} 

或者,您可以使用 javascript 代替 Page_PreRender

<body onload='setFocusToTextBox()'>

<script> 
    function setFocusToTextBox(){ 
         document.getElementById('<%= Session["event_control"] %>').focus();
    } 
</script>

您在设置焦点之前看到延迟的原因是因为该文本框是您的默认焦点。如果您不想延迟,可以使用默认焦点创建隐藏文本框,无人看到,他们只会看到正确的文本框获得焦点。

答案 1 :(得分:0)

这应该有效

protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
    //Your working code
    GridViewRow myRow = ((Control)sender).Parent.Parent as GridViewRow;
    myRow.FindControl("txtUnit").Focus();
}

答案 2 :(得分:0)

您可以将 TabIndex="0" 提供给您想要首先关注的gridview中的第一个文本框以及要移动光标的 TabIndex="1" 回发后自动

更改

 <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("QUANTITY")%>'  class="form-control input-sm" Style=" text-align:right;"
                     ClientIDMode="Static" onkeypress = "return IsDecimal(this);" ontextchanged="txtQuantity_TextChanged" AutoPostBack="true"></asp:TextBox>

经由

 <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("QUANTITY")%>'  class="form-control input-sm" Style=" text-align:right;"
                     ClientIDMode="Static" onkeypress = "return IsDecimal(this);" ontextchanged="txtQuantity_TextChanged" AutoPostBack="true" TabIndex="0"></asp:TextBox>

更改

<asp:TextBox ID="txtUnit" runat="server" class="form-control input-sm" AutoPostBack="true" Text='<%#Eval("UNIT_NAME")%>' 
                 ontextchanged="txtUnit_TextChanged" ClientIDMode="Static"></asp:TextBox>

<asp:TextBox ID="txtUnit" runat="server" class="form-control input-sm" AutoPostBack="true" Text='<%#Eval("UNIT_NAME")%>' 
                 ontextchanged="txtUnit_TextChanged" ClientIDMode="Static" TabIndex="1"></asp:TextBox>