|
-
Mar 3rd, 2003, 02:30 PM
#1
Thread Starter
Lively Member
Edit with templates in datagrid
Hi,
I have a page with a datagrid, and inside the data grid I want to edit with templates some columns.
In the templates I use dropdownlist to let the user to select the new value.
My questuion is where and how to set the initial selected value of the dropdownlist so it will match the value that exist for that cell.
I tried with this code, where I can get existing value in lblActive, but I don't have the Edit field (dropdownlist) available to set the initial selected field.
Which will be the proper event to do this?
Sub DataGrid1_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim lblActive As Label
lblActive = e.Item.FindControl("lblActive")
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
Dim oneItem As ListItem
Dim ddlActive As DropDownList
ddlActive = e.Item.FindControl("DropDownListActive")
For Each oneItem In ddlActive.Items
If oneItem.Value.ToLower = lblActive.Text.ToLower Then
oneItem.Selected = True
End If
Next
End Sub
Thank you,
svatasoiu
-
Mar 4th, 2003, 09:47 AM
#2
Hyperactive Member
I handle this type of situation with late-binding.
VB Code:
<EditItemTemplate>
<asp:DropDownList runat="server" SelectedIndex='<%# GetIndex(Container.DataItem("<field>")) %>' id="ddlStates">
</asp:DropDownList>
</EditItemTemplate>
-
Mar 4th, 2003, 01:28 PM
#3
Thread Starter
Lively Member
-
Mar 5th, 2003, 09:55 AM
#4
Thread Starter
Lively Member
GetIndex ?
fungi,
Is GetIndex a custom function used to retrieve the index, or it is a method of some .net object (I can not found which)
Thank you,
svatasoiu
-
Mar 5th, 2003, 10:06 AM
#5
Hyperactive Member
Sorry for not being more specific svata.
My list items are loaded from a SortedList and "GetIndex" is a function that returns the IndexOfKey for the row value.
-
Mar 5th, 2003, 10:49 AM
#6
Thread Starter
Lively Member
fungi,
now make sense.
I tried to implement this, but I'm stuck when I try to load the dropdownlist from the sortedlist.
I try to do this into the aspx page with inline script code but this is not allowed.
Can you tell be where you define the sortedList (I tried in a separate class) and how & where do you load the sorted class into the dropdownlist from the template.
thank you,
svatasoiu
-
Mar 5th, 2003, 11:59 AM
#7
Hyperactive Member
Give me a day or so to find that code and I'll post it...
-
Mar 5th, 2003, 04:33 PM
#8
Hyperactive Member
Here's that code. My dropdown is for a list of state codes.
Declare the list and the function in the WebForm
VB Code:
Public slStates As SortedList
Public Function GetIndex(ByVal s As String) As Long
Return (slStates.IndexOfKey(s))
End Function
Load up the list in the Page_Load:
VB Code:
slStates = New SortedList()
slStates.Add("AL", 0)
slStates.Add("AK", 1)
slStates.Add("AR", 2)
slStates.Add("AZ", 3)
slStates.Add("CA", 4)
...
Load up the dropdown inline in the asp:
VB Code:
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "state") %>' ID="lblState" NAME="lblState"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" SelectedIndex='<%# GetIndex(Container.DataItem("state")) %>' id="edit_State">
<asp:ListItem>AL</asp:ListItem>
<asp:ListItem>AK</asp:ListItem>
<asp:ListItem>AR</asp:ListItem>
<asp:ListItem>AZ</asp:ListItem>
<asp:ListItem>CA</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
Not the best solution but it served my needs at the time...
Good luck!
-
Mar 6th, 2003, 08:33 AM
#9
Thread Starter
Lively Member
fungi,
Thank you very much for this answer.
I was thinking to almost the same solution, but I belived it may be a solution to feed the dropdownbox from the same SortedList.
If you can do that, then any change require only one place to be done.
Thank you,
Svatasoiu
-
Mar 7th, 2003, 08:47 AM
#10
Thread Starter
Lively Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|