Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a drop down list in a repeater

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
Greetings all. This question is two-fold. I've got a repeater which lists staff members names, current security role, and a dropdown of available security roles. Each person is only allowed one role at a time. The purpose of the page is to allow an admin to change the roles of the staff members....which is where my questions come in.

1st - As you can see in the code below, I'm trying to display the dropdown lists with the person's current security role as the default listitem. I'm clearly not doing this correctly b/c I'm getting an error saying "Literal content ('<asp:listitem Value="') is not allowed within a 'System.Web.UI.WebControls.ListItemCollection'". -- Anyone know how I might handle this?

2nd - What I'd really like to do is let the admin go through all the staff members, select the desired security roles from the drop downs, then click a submit button which updates the 'access_level' field for each member in the 'staff' table. -- Can anyone show me how to do this?

Here's the complete code for the page:

<%@ Page Language="vb" Debug="true" %>

<%@ import Namespace="System.Data" %>

<%@ import Namespace="System.Data.SqlClient" %>

<script runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)



Dim DS As DataSet

Dim MyCommand As SqlDataAdapter

Dim strConnect As String = ConfigurationSettings.AppSettings("ConnectionString")



MyCommand = New SqlDataAdapter("SELECT Firstname, Lastname, id, access_level FROM staff", strConnect)



DS = New DataSet()

MyCommand.Fill(DS, "staff")



MyRepeater.DataSource = DS.Tables("staff").DefaultView

MyRepeater.DataBind()

End Sub

</script>



<form runat="server">

<ASP:Repeater id="MyRepeater" runat="server"> <HeaderTemplate>

<table width="65%" border="0" cellpadding="1" cellspacing="0" bgcolor="#333333">

<tr>

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr bgcolor="cccccc">

<td>Name</td>

<td>Current Access Level</td>

<td>Access Level Options</td>

</tr></HeaderTemplate>

<ItemTemplate>

<tr bgcolor="efefef">

<td><%# DataBinder.Eval(Container.DataItem, "firstname") %> <%# DataBinder.Eval(Container.DataItem, "lastname") %></td>

<td><font color="#999999"><%# DataBinder.Eval(Container.DataItem, "access_level") %></font></td>

<td>

<asp:DropDownList id="DropDownList1" runat="server">

<asp:listitem Value="<%# DataBinder.Eval(Container.DataItem, "id") %>" Text="<%# DataBinder.Eval(Container.DataItem, "access_level") %>" />

<asp:ListItem Value="4" Text="No Access" />

<asp:ListItem Value="1" Text="Contributor" />

<asp:ListItem Value="2" Text="Power User" />

<asp:ListItem Value="3" Text="Site Administrator" />

</asp:DropDownList>

</td>

</tr>

</ItemTemplate>

<FooterTemplate>

<tr>

<td colspan="3" bgcolor="#FFFFFF">&nbsp;</td>

</tr>

<tr>

<td colspan="3" align="right"><input type="submit" name="update" value="Save Changes"></td>

</tr>

</table>

</td>

</tr>

</table></FooterTemplate>

</ASP:Repeater>

</form>
 
Try something like this for your dropdown. I've never done it, but it should work.

<asp:DropDownList ID="DropDownList1" runat="server" SelectedIndex='<%# GetAccessLevel(Container.DataItem("id")) %>' DataSource='<%# GetRateTypeList()%>' DataTextField="access_level" DataValueField="id">
</asp:DropDownList>

You'll need to create a Function that returns an object that implemnents IEnumerable for GetRateTypeList(). A table would work.

You'll need another function to return the selected index based on the ID. Probable something that would go into the Table Created in GetRateTypeList and returns an integer of the index of the access level.

In order to save this information you have to loop throw the items created in the repeater, find the dropdown and set the value back into your dataset so you can save it.

This example loops through a datagrid, but should work for a repeater.

Dim myCurrentDropDownList As New DropDownList()
Dim myDataGridItem As DataGridItem


For Each myDataGridItem In client.Items
myCurrentCheckBox = CType(myDataGridItem.FindControl("DropDownList1"), DropDownList)

''set the value in the dataset


Next
'update dataset


Good luck

You might consider using a datagrid. This post would work for that too
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top