[英]How to call postback from TextBox on TextChange event without focus being changed?
[英]how to set focus to specific textbox in current gridview row after postback from textchange event
在 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();
}
問題是因為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>
您在設置焦點之前看到延遲的原因是因為該文本框是您的默認焦點。 如果您不希望出現這種延遲,您可以創建一個帶有默認焦點的隱藏文本框,沒有人看到,他們只會看到正確的文本框獲得焦點。
這應該有效
protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
//Your working code
GridViewRow myRow = ((Control)sender).Parent.Parent as GridViewRow;
myRow.FindControl("txtUnit").Focus();
}
您可以將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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.