代码之家  ›  专栏  ›  技术社区  ›  Budda

在转发器中获取DropDownList值

  •  2
  • Budda  · 技术社区  · 16 年前

    标记代码如下:

    ASPX页

    <asp:Repeater ID="GeneralRepeater" runat="server" 
     OnItemDataBound="GeneralRepeater_OnItemDataBound">
       <ItemTemplate>
         <tr>
          <td>
           Place:
             <asp:DropDownList ID="GeneralDDL" DataTextField="Text" 
                  DataValueField="Arena" runat="server" />
         </td>
         <th>
           <asp:Button ID="GeneralButton" runat="server" 
                Text="Принять запрос" onclick="GeneralButton_Click" />
        </th>
        </tr>
       </ItemTemplate>
    </asp:Repeater>
    

    代码落后

    protected void GeneralRepeater_OnItemDataBound(object sender, 
                                                   RepeaterItemEventArgs e)
    {
         if (e.Item.ItemType == ListItemType.Item || 
             e.Item.ItemType == ListItemType.AlternatingItem)
         {
             DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL");
             myDDL.DataSource = lstArenaSelect;
             myDDL.DataBind();
    
             MyObject obj= (MyObject)e.Item.DataItem;
             Button GeneralButton = (Button)e.Item.FindControl("GeneralButton");
             AcceptGeneralRequestButton.CommandArgument = obj.Id;
         }
    }
    

    这将显示每个 DropDownList 对象列表,行中的每个按钮都链接到行对象。

    GeneralButton_Click 我能得到的方法 ID 绑定到中继器的对象。

    问题

    我如何从 下拉框 位于同一中继器行?

    5 回复  |  直到 16 年前
        1
  •  2
  •   Budda    16 年前

    谢谢大家,我使用了另一种方法:

    Control parent = ((Control)sender).Parent;
    DropDownList GeneralDDL = (DropDownList)parent.FindControl("GeneralDDL");
    

    代码在onclick按钮事件处理程序中调用。

        2
  •  1
  •   John Allers    16 年前

    也许这样的方法会奏效:

    protected void GeneralButton_Click(object sender, EventArgs e)
    {
        Button myGeneralButton = (Button)sender;
        DropDownList myDDL = (DropDownList) myGeneralButton.NamingContainer.FindControl("GeneralDDL");
    
        // myDDL.SelectValue should be what you are looking for.
    }
    
        3
  •  1
  •   kervin    16 年前

    使用“items”成员和提供的项索引。

    看… http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.items.aspx 举个例子。

    基本上…

    DropDownList currDDL = GeneralRepeater.Items[currButtonItemIndex].FindControl('GeneralDDL') as DropDownList;
    

    DropDownList currDDL = GeneralRepeater.Items[e.Item.ItemIndex].FindControl('GeneralDDL') as DropDownList;
    

    对于事件处理程序。

    如果可以的话,用listview代替转发器。

        4
  •  1
  •   PhilPursglove    16 年前

    我认为您需要从repeatercommandeventargs获取repeater行:

    protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        DropDownList myDDL;
    
        myDDL = (DropDownList) e.Item.FindControl("GeneralDDL");
    
        System.Diagnostics.Debug.WriteLine(myDDL.SelectedValue);
    }
    
        5
  •  0
  •   Community Mohan Dere    8 年前

    我用网格做了一些事情,DataGrid有一个行数据绑定事件 How do I data bind a drop down list in a gridview from a database table using VB?

    如果在转发器上使用itemdatabound事件,也可以使用e.item.itemIndex获取索引。

    不过,我认为转发器没有行数据绑定事件。

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events.aspx