我有一个带标题和项目模板的转发器。标题包含一个文本框和链接按钮("添加"将在文本框中输入的项目添加到列表中)。我需要的是能够在" Add"之后将焦点设置回文本框。点击。我包括代码和我尝试过的(无济于事)。我包括OnItemDataBound for repeaer,一个javascript来设置焦点(想做这个客户端):
<asp:Repeater
runat="server"
ID="rptExclPBSA"
OnItemDataBound="rptExclPBSA_ItemDataBound"
OnItemCommand="rptExclPBSA_ItemCommand">
<HeaderTemplate>
<table style="width:300px" border="0">
<tr>
<td style="vertical-align:top;width:100px">
<asp:TextBox runat="server" ID="tbExclBox" CssClass="NormalSmall" Width="90" MaxLength="5" />
</td>
<td style="width:200px;">
<asp:LinkButton ID="lbAddExcl" runat="server" CommandName="Add" Text="Add Something" />
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table style="width:300px" border="0">
<tr>
<td style="vertical-align:top;width:100px;text-align:center" class="NormalSmall">
<%# Eval("Box") %>
</td>
<td style="vertical-align:top;width:200px;">
<asp:ImageButton ID="ibRemoveExcl" runat="server" ImageUrl="images/delete.gif" CommandName="Remove" AlternateText="Delete That Thing" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
在代码背后:
protected void rptExclPBSA_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
LinkButton lbAddExcl = e.Item.FindControl("lbAddExcl") as LinkButton;
TextBox tbExclBox = e.Item.FindControl("tbExclBox") as TextBox;
if (null != lbAddExcl && null != tbExclBox)
lbAddExcl.Attributes.Add("onclick", "setFocusPOB('" + tbExclBox.ClientID + "');");
}
}
protected void rptExclPBSA_ItemCommand(object source, RepeaterCommandEventArgs e)
{
TextBox tbExclBox = (TextBox)rptExclPBSA.Controls[0].Controls[0].FindControl("tbExclBox");
do_whatever()
tbExclBox.Focus();
}
使用Javascript:
function setFocusPOB(ctrl_id){
var tbExclBox = document.getElementById(ctrl_id);
if (null != tbExclBox)
tbExclBox.focus();
}
答案 0 :(得分:1)
当您在do_whatever()上重新绑定转发器时,会重新创建文本框,然后您必须再次找到它。
protected void rptExclPBSA_ItemCommand(object source, RepeaterCommandEventArgs e)
{
TextBox tbExclBox = (TextBox)rptExclPBSA.Controls[0].Controls[0].FindControl("tbExclBox");
do_whatever()
tbExclBox = (TextBox)rptExclPBSA.Controls[0].Controls[0].FindControl("tbExclBox");
tbExclBox.Focus();
}
不需要事件ItemDataBound。