2013-07-25 70 views
0

我正在向頁面動態添加單選按鈕列表的列表,並且按鈕單擊我想存儲選定的值。下面是此示例代碼。 如何檢索這些動態創建的RadioButtonLists的selectedIndexes?我花了很多時間嘗試從網上其他類似問題的各種修復,但沒有運氣。 如果我使用「檢查元素」在Chrome中,我能看到RadioButtonLists在他們所需的位置顯然是與我已分配(RBQuestion_1,RBQuestion_2等)的ID,從動態生成的單選按鈕列表中檢索值

Sub BindForm() 

Dim tblStars As New Table() 
    Dim rb As New RadioButtonList() 
    rb.ID = RBQuestion_" & row("Id") 
    Dim tc As New TableCell() 
    Dim tr As New TableRow() 
    tc.Controls.Add(rb) 
    tr.cells.Add(tc) 

    tblStars.Rows.Add(tr) 

    form1.Controls.Add(tblStars) 
Next 
end sub 


Protected Sub btnSave_click(ByVal sender As Object, ByVal e As EventArgs) 

For Each ctrl As Control In Page.FindControl(RBQuestion_" & row("Id")) 
      If TypeOf ctrl Is RadioButtonList Then 
       Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList) 
       For i As Integer = 0 To rbl.Items.Count - 1 
        If rbl.Items(i).Selected Then 
         'get the value of the selected radio button' 
         Dim value As String = rbl.SelectedItem.Value 
        End If 
       Next 
      End If 
     Next 
end sub 

回答

1

您正在尋找內頁的控制列表。

您的單選按鈕將不在那裏。它們位於您添加它們的TableCell的控制列表中。

您需要找到您的表格,然後遍歷每一行,併爲每一行找到單元格,然後在單元格內找到單選按鈕。

或者,您可以嘗試將按鈕添加到創建它們的RadioButton類型的對象範圍通用列表控件中。然後直接從您的btnSave_Click事件處理程序中引用它們。 專業版:消除了導航控件層次結構的需要。 Con:距離實際底層實現的代碼距離。這有可能會誤導這些控件的真實位置,當開發人員在將來查看時......在您有時間忘記您所做的事情和原因之後,可能會包含您自己的一組內容。對我而言,通常在一個愉快的一天約4個小時。

因此,我更喜歡瀏覽層次結構的方法,因爲它更真實的如何事實上佈局。但我很確定通用列表方法也可以工作。


編輯

我覺得這塊你缺少的是重寫CreateChildControls方法的難題。

我承認你的問題在VB中表達過。但是我對VB很不滿意,所以我改用C#來代替。希望這對你有用是有意義的。

證實了在ASP.NET開發服務器中託管時,VS 2010中按預期工作。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication3 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected override void CreateChildControls() 
     { 
      base.CreateChildControls(); 

      this.CreateRadioButtons(); 
     } 

     private void CreateRadioButtons() 
     { 
      var tblStars = new Table(); 
      tblStars.ID = "tblStars"; 

      ListItem opt1 = new ListItem(); 
      opt1.Text = "I like red"; 
      opt1.Value = "Red"; 

      ListItem opt2 = new ListItem(); 
      opt2.Text = "I like green"; 
      opt2.Value = "Green"; 

      ListItem opt3 = new ListItem(); 
      opt3.Text = "I like blue"; 
      opt3.Value = "Blue"; 

      var rb = new RadioButtonList(); 
      rb.ID = "RBQuestion_1"; 

      rb.Items.Add(opt1); 
      rb.Items.Add(opt2); 
      rb.Items.Add(opt3); 

      var tc = new TableCell(); 
      var tr = new TableRow(); 
      tc.Controls.Add(rb); 
      tr.Cells.Add(tc); 
      tblStars.Rows.Add(tr); 

      form1.Controls.Add(tblStars); 
     } 

     protected void btnSave_Click(object sender, EventArgs e) 
     { 
      var tblStars = this.form1.FindControl("tblStars") as Table; 
      if (tblStars == null) 
       return; 

      foreach (TableRow row in tblStars.Rows) 
      { 
       foreach (TableCell cell in row.Cells) 
       { 
        var rb = cell.FindControl("RBQuestion_1") as RadioButtonList; 
        if (rb == null) 
         continue; 

        var selectedValue = rb.SelectedValue; 
       } 
      } 
     } 
    } 
} 

我對應頁面的標記:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /> 
    </div> 
    </form> 
</body> 
</html> 
+0

感謝您的reply..if我創建上面bindForm方法動態表,我如何可以訪問表中保存按鈕事件處理程序。它是無法訪問保存事件處理程序 – adward

+0

您需要重寫CreateChildControls方法。編輯回答以提供示例。 –

+0

CreateChildControls方法是否適合您? –

相關問題