我有一个带有标题和项目模板的转发器。标题包含一个文本框和链接按钮(“添加”将在文本框中输入的项目添加到列表中)。我需要的是能够在单击“添加”后将焦点重新设置在文本框上。我包括代码和我尝试过的(无济于事)。我包括用于中继器的 OnItemDataBound,一个用于设置焦点的 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();
}