为DropDownList ASP和C#传递动态选择

时间:2013-01-23 15:38:06

标签: c# asp.net dynamic drop-down-menu

我有一个Repeater,它会发布一个用户名,他们的权限级别和一个删除按钮。 许可级别假设位于下拉列表中。默认值是用户当前拥有的默认值。

IE

用户A,访客

用户B,小组成员

USER C,Group Admin

更改这些将最终触发sql命令以在数据库中更改它们。但这不是问题,我知道该怎么做。

值来自转发器源。

ASP代码:

  <asp:SqlDataSource ID="User_Repeater_Source" runat="server" ConnectionString="<%$ ConnectionStrings:app_migrationConnectionString  %>"
    SelectCommand="SELECT Membership.*, Users.Email, Users.Last_Name + ', ' + Users.First_Name + ' (' + Membership.User_ID + ')' as User_String FROM Membership INNER JOIN Users ON Users.User_ID = Membership.User_ID INNER JOIN Permission_Levels ON Permission_Levels.PID = Membership.PID WHERE GID = @GID">
    <SelectParameters>
      <asp:ControlParameter ControlID="Group" DefaultValue="-1" Name="GID" PropertyName="SelectedValue"
        Type="String" />
    </SelectParameters>
  </asp:SqlDataSource>
  <asp:SqlDataSource ID="User_PL_Source" runat="server" ConnectionString="<%$ ConnectionStrings:app_migrationConnectionString  %>"
    SelectCommand="SELECT * FROM Permission_Levels"></asp:SqlDataSource>

...

  <td>
    <div class="e_Title">
      Edit Group Permission:</div>
    <br />
    <asp:Repeater ID="User_Repeater" runat="server" DataSourceID="User_Repeater_Source">
      <HeaderTemplate>
        <table class="e_Repeater">
          <tr>
            <th>
              User:
            </th>
            <th>
              Permission Level:
            </th>
            <th>
              Remove User:
            </th>
          </tr>
      </HeaderTemplate>
      <ItemTemplate>
        <tr>
          <td>
            <a href='<%#"mailto:" + DataBinder.Eval(Container.DataItem, "Email") %>'>
              <%#DataBinder.Eval(Container.DataItem, "User_String" %></a>
          </td>
          <td>
            <asp:DropDownList ID="User_PL" runat="server" DataSourceID="User_PL_Source" DataTextField="Name"
              DataValueField="PID" AutoPostBack="true">
            </asp:DropDownList>
          </td>
          <td>
            <asp:Button ID="Submit_User_Remove" runat="server" Text="Remove Permission" OnCommand="Remove_User_Permission"
              CommandArgument='<%# Eval("AID")%>' />
          </td>
        </tr>
      </ItemTemplate>
      <FooterTemplate>
        </table>
      </FooterTemplate>
    </asp:Repeater>
  </td>

我希望将PID传递到Dropdownlist并将其作为选择的选项

1 个答案:

答案 0 :(得分:0)

首先,我相信你不打算标记这个asp-classic,而是asp.net。没什么大不了的。

我建议使用OnItemDataBound。这样的事情可以让你开始:

protected void User_Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RepeaterItem item = (RepeaterItem)e.Item;
    if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
    {
        DropDownList User_PL = (DropDownList)item.FindControl("User_PL");
        ... Set your value here -- consider adding a hidden element with the value you're trying to set it to and get the value the same was as we got the DropDownList
    }
}

或者,我相信您可以使用标记绑定到控件本身:

SelectedValue='<%# Eval("PID") %>'
祝你好运。