Reputation: 18734
I have an ASP.Net application, where I need to present the user with a list of Projects, and next to each project, prove a drop down which has 4 options per drop down. The drop down allows the user to select which projects the selected person can view. The options are: No Access, Read Only, Restricted Access and Full Access.
So, I was thinking a table, with 2 columns: Project Name and Access Level.
And at the end, a Save button.
I then need to return all the projects (Ids?) with the selected value of all the drop down lists, as an Array maybe? And then in the code behind, I will create a List<> and pass that down to the framework code to store in the database.
But, I am unsure how to render the table, so that I can somehow get the project, linked to the drop down, and turn that into something I can use to save the data. The save button needs to save ALL the projects access levels for the user. How can I do this in ASP.Net?
To render the screen, I was going to get a List<ProjectSecurity>
from the database, where ProjectSecurity is a DTO with:
ProjectId
ProjectName
UserId
SecurityLevelId
Where SecurityLevelId is ReadOnly, No Access, etc etc.
I would then iterate through the list, and create a Table, but would need a hidden field for the projectId per row?
And then somehow get it back on Save?
I'd be happy to use a grid, but a grid with inline editing using a drop down box seems like it's pretty tricky to impliment, and a SaveAll button. Single row editing seems easier.
Based on the idea provided below, I have 'hard coded' a dummy column in a grid I currently have, and can get a drop down. But, not sure how to populate the items in the drop down from List<> I can get from my table, and how to set the selected value based on the current row's selected value:
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="AccountManager" HeaderText="AccountManager" />
<asp:BoundField DataField="ProjectCode" HeaderText="ProjectCode" />
<asp:BoundField DataField="DurationText" HeaderText="DurationText" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList runat="server" AutoPostBack="True">
<asp:ListItem
Enabled="True"
Text="Test"
Value="Test" />
<asp:ListItem
Enabled="True"
Text="Test 2"
Value="Tes 2" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Upvotes: 0
Views: 157
Reputation: 411
I have the luxury of telerik for this sort of thing but the structure you are looking for is something like
<asp:GridView>
<Columns>
<asp:BoundField />
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
IIRC there is a item data bound event where you can set the selected value of the drop down to your desired access level
in the case of telerik there is an update event that was very useful for saving edits to the db, you will need something similar
its not simple but after the first time its easy replication.
Bellow is a sample itemDataBound command, though it does use telerik so sadly its not quite copy/paste relevant. Using e.Item.FindControl will allow you to databind a drop down and when firing your update command you can get a list of the rows in editmode in order to update them all. It is my preferred method however I agree its not exactly simple. Hope VB is ok its what I found first.
Private Sub MvrgStreams_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgStreams.ItemDataBound
Dim loRow As DataRowView
If TypeOf e.Item Is Telerik.Web.UI.GridDataItem Then
Dim lrgRow As Telerik.Web.UI.GridDataItem = CType(e.Item, Telerik.Web.UI.GridDataItem)
Dim llbl As Label
loRow = e.Item.DataItem
If Not loRow.Item(1) Is DBNull.Value Then
llbl = e.Item.FindControl("lblStreamName")
llbl.Text = loRow.Item(1)
End If
If Not loRow.Item(0) Is DBNull.Value Then
llbl = e.Item.FindControl("lblStreamId")
llbl.Text = loRow.Item(0)
End If
If Not loRow.Item(2) Is DBNull.Value Then
llbl = e.Item.FindControl("lblAvailFrom")
llbl.Text = loRow.Item(2) '.ToString("MM/dd/yyyy HH:mm")
End If
If Not loRow.Item(3) Is DBNull.Value Then
llbl = e.Item.FindControl("lblLastUp")
llbl.Text = loRow.Item(3) '.ToString("MM/dd/yyyy HH:mm")
End If
If Not loRow.Item(4) Is DBNull.Value Then
llbl = e.Item.FindControl("lblLastEff")
llbl.Text = loRow.Item(4) '.ToString("MM/dd/yyyy HH:mm")
End If
If Not loRow.Item(5) Is DBNull.Value Then
llbl = e.Item.FindControl("lblLastPost")
llbl.Text = loRow.Item(5) '.ToString("MM/dd/yyyy HH:mm")
End If
llbl = e.Item.FindControl("lblType")
If Not loRow.Item(6) Is DBNull.Value Then
llbl.Text = loRow.Item(6)
End If
llbl = e.Item.FindControl("lblUnit")
If Not loRow.Item(7) Is DBNull.Value Then
llbl.Text = loRow.Item(7)
End If
llbl = e.Item.FindControl("lblZone")
If Not loRow.Item(8) Is DBNull.Value Then
llbl.Text = loRow.Item(8)
End If
llbl = e.Item.FindControl("lblZoneTo")
If Not loRow.Item(9) Is DBNull.Value Then
llbl.Text = loRow.Item(9)
End If
llbl = e.Item.FindControl("lblLoc")
If Not loRow.Item(10) Is DBNull.Value Then
llbl.Text = loRow.Item(10)
End If
llbl = e.Item.FindControl("lblCom")
If Not loRow.Item(11) Is DBNull.Value Then
llbl.Text = loRow.Item(11)
End If
llbl = e.Item.FindControl("lblInst")
If Not loRow.Item(12) Is DBNull.Value Then
llbl.Text = loRow.Item(12)
End If
llbl = e.Item.FindControl("lblPlant")
If Not loRow.Item(13) Is DBNull.Value Then
llbl.Text = loRow.Item(13)
End If
End If
End Sub
Upvotes: 1