2017-04-05 63 views
0

我想關注導致在asp.net radiobuttonlist中回發的相同ListItem。但是目前開發的實現只關注第一個列表項。有沒有辦法專注於導致回發的特定listitem?我無法在我的情況下使用更新面板。我的HTML代碼隱藏和是如下:在ASP.NET WebForm中回傳後重點放在RadioButtonList中的ListItem

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

回答

1

有幾種方法可以做到這一點。您可以從客戶端控制整個事情,也可以從服務器端執行此操作:

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); 
} 
相關問題