Results 1 to 10 of 10

Thread: Edit with templates in datagrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    MA, US
    Posts
    78

    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

  2. #2
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    I handle this type of situation with late-binding.

    VB Code:
    1. <EditItemTemplate>
    2.     <asp:DropDownList runat="server" SelectedIndex='<%# GetIndex(Container.DataItem("<field>")) %>' id="ddlStates">
    3.     </asp:DropDownList>
    4. </EditItemTemplate>

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    MA, US
    Posts
    78
    fungi,

    Thank you for your answer.
    when I tried in the way you propesed, I get an error related to GetIndex
    =========================================
    <EditItemTemplate>
    <aspropDownList id="DropDownListActive" runat="server" SelectedIndex='<%# GetIndex(Container.DataItem("Active")) %>'>
    <asp:ListItem Value="False">False</asp:ListItem>
    <asp:ListItem Value="True">True</asp:ListItem>
    </aspropDownList>
    </EditItemTemplate>
    I get this error:
    Compiler Error Message: BC30451: Name 'GetIndex' is not declared.
    =======================================
    When I tried the basic:
    <EditItemTemplate>
    <aspropDownList id="DropDownListActive" runat="server" SelectedIndex='1'>
    <asp:ListItem Value="False">False</asp:ListItem>
    <asp:ListItem Value="True">True</asp:ListItem>
    </aspropDownList>
    </EditItemTemplate>
    I get this error:
    Parser Error Message: The 'SelectedIndex' property is set only by the runtime. It cannot be declared.
    ========================================
    If the example you send does work, I'm sure I'm missing something. Any ideea?

    Thank you,

    svatasoiu

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    MA, US
    Posts
    78

    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

  5. #5
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    MA, US
    Posts
    78
    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

  7. #7
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    Give me a day or so to find that code and I'll post it...

  8. #8
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    Here's that code. My dropdown is for a list of state codes.

    Declare the list and the function in the WebForm

    VB Code:
    1. Public slStates As SortedList
    2.  
    3. Public Function GetIndex(ByVal s As String) As Long
    4.     Return (slStates.IndexOfKey(s))
    5. End Function
    Load up the list in the Page_Load:
    VB Code:
    1. slStates = New SortedList()
    2. slStates.Add("AL", 0)
    3. slStates.Add("AK", 1)
    4. slStates.Add("AR", 2)
    5. slStates.Add("AZ", 3)
    6. slStates.Add("CA", 4)
    7. ...
    Load up the dropdown inline in the asp:
    VB Code:
    1. <asp:TemplateColumn HeaderText="State">
    2.   <ItemTemplate>
    3.     <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "state") %>' ID="lblState" NAME="lblState"/>
    4.   </ItemTemplate>
    5.   <EditItemTemplate>
    6.     <asp:DropDownList runat="server" SelectedIndex='<%# GetIndex(Container.DataItem("state")) %>' id="edit_State">
    7.       <asp:ListItem>AL</asp:ListItem>
    8.       <asp:ListItem>AK</asp:ListItem>
    9.       <asp:ListItem>AR</asp:ListItem>
    10.       <asp:ListItem>AZ</asp:ListItem>
    11.       <asp:ListItem>CA</asp:ListItem>
    12.     </asp:DropDownList>
    13.   </EditItemTemplate>
    14. </asp:TemplateColumn>

    Not the best solution but it served my needs at the time...
    Good luck!

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    MA, US
    Posts
    78

    Thumbs up

    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

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    MA, US
    Posts
    78

    Smile

    Fungi,

    This is the code I finally implemented: it is a combination of your ex + a datatable that allow me to build the dropdown list from the same sorted list. In this way I have only one place where I describe the values from the dropdown list

    Thank you,

    svatasoiu

    <EditItemTemplate>
    <aspropDownList id="ddlExpMonth" runat="server" DataSource='<%# GetDDL_Month()%>' DataTextField="Month" DataValueField ="MonthValue" SelectedIndex='<%# GetIndexMonth(left(Container.DataItem("CCExp"),2)) %>'>
    </aspropDownList>
    </EditItemTemplate>


    Public Function GetDDL_Month() As DataTable
    Dim dt As New DataTable()
    Dim dr As DataRow
    dt.Columns.Add("Month", GetType(String))
    dt.Columns.Add("MonthValue", GetType(String))
    Dim i As Integer
    For i = 0 To slMonth.Count - 1
    dr = dt.NewRow
    dr("Month") = slMonth.GetKey(i)
    dr("MonthValue") = slMonth.GetKey(i)
    dt.Rows.Add(dr)
    Next
    Return dt
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width