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

xj3cbfub  于 2022-11-26  发布在  .NET
关注(0)|答案(3)|浏览(137)

在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();
}
rpppsulh

rpppsulh1#

这应该行得通

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

ijxebb2r2#

您可以将**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>
htrmnn0y

htrmnn0y3#

这个问题是因为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>

之所以在设置焦点之前会出现延迟,是因为该文本框是默认焦点。如果不希望出现延迟,可以创建一个隐藏的文本框,使其具有默认焦点,这样其他人就不会看到,他们只会看到正确的文本框获得焦点。

相关问题