在 ASP.NET WebForm 中回发后关注 RadioButtonList 中的 ListItem
Focus on an ListItem in RadioButtonList after PostBack in ASP.NET WebForm
我想关注在 asp.net 单选按钮列表中导致回发的同一个 ListItem。但是目前开发的实现只关注第一个列表项。有没有办法专注于导致回发的特定列表项。在我的情况下,我不能使用更新面板。我的 HTML 和 CodeBehind 如下:
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" Text="Name" AssociatedControlID="txtName"></asp:Label>
<asp:TextBox ID="txtName" runat="server" Text="Enter your name"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblPersonType" AssociatedControlID="rdoPersonType" runat="server" Text="Person Type"></asp:Label>
<br />
<asp:RadioButtonList ID="rdoPersonType" RepeatLayout="Flow" AutoPostBack="true" runat="server" OnSelectedIndexChanged="rboPersonType_Selected"></asp:RadioButtonList>
</div>
</form>
</body>
</html>
代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rdoPersonType.Items.Add(new ListItem() { Text = "Student", Value = "1", Selected = true });
rdoPersonType.Items.Add(new ListItem() { Text = "Greencard Holder", Value = "2" });
rdoPersonType.Items.Add(new ListItem() { Text = "Refugee", Value = "3" });
rdoPersonType.Items.Add(new ListItem() { Text = "Asylum", Value = "4" });
}
}
protected void rboPersonType_Selected(object sender, EventArgs e)
{
rdoPersonType.Focus();
}
有几种方法可以做到这一点。您可以从客户端控制整个事情,也可以从服务器端进行控制:
protected void rboPersonType_Selected(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(
typeof(Page),
"SetFocus",
string.Format("document.getElementById('{0}_{1}').focus();", rdoPersonType.ClientID, rdoPersonType.SelectedIndex),
true);
}
我想关注在 asp.net 单选按钮列表中导致回发的同一个 ListItem。但是目前开发的实现只关注第一个列表项。有没有办法专注于导致回发的特定列表项。在我的情况下,我不能使用更新面板。我的 HTML 和 CodeBehind 如下:
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" Text="Name" AssociatedControlID="txtName"></asp:Label>
<asp:TextBox ID="txtName" runat="server" Text="Enter your name"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblPersonType" AssociatedControlID="rdoPersonType" runat="server" Text="Person Type"></asp:Label>
<br />
<asp:RadioButtonList ID="rdoPersonType" RepeatLayout="Flow" AutoPostBack="true" runat="server" OnSelectedIndexChanged="rboPersonType_Selected"></asp:RadioButtonList>
</div>
</form>
</body>
</html>
代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rdoPersonType.Items.Add(new ListItem() { Text = "Student", Value = "1", Selected = true });
rdoPersonType.Items.Add(new ListItem() { Text = "Greencard Holder", Value = "2" });
rdoPersonType.Items.Add(new ListItem() { Text = "Refugee", Value = "3" });
rdoPersonType.Items.Add(new ListItem() { Text = "Asylum", Value = "4" });
}
}
protected void rboPersonType_Selected(object sender, EventArgs e)
{
rdoPersonType.Focus();
}
有几种方法可以做到这一点。您可以从客户端控制整个事情,也可以从服务器端进行控制:
protected void rboPersonType_Selected(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(
typeof(Page),
"SetFocus",
string.Format("document.getElementById('{0}_{1}').focus();", rdoPersonType.ClientID, rdoPersonType.SelectedIndex),
true);
}