For Steven Cheng

Discussion in 'ASP .Net' started by Paul, Jan 10, 2004.

  1. Paul

    Paul Guest

    Hello Steven,
    I saw your code for the onLoad event for a dropdownlist
    in a datagrid, and that was exactly what I needed for the
    problem I had with a checkboxlist.
    However, I have one problem that I've been trying to
    figure out.
    By the way your email address is invalid.

    I have multilple rows in the grid. How do I pass the index
    of that row to the onload event handler. Here's your code
    and mine. The checkbox list have 3 checkbox with values B,
    L and D (Breakfast, Lunch, Dinner).

    Notice the TODO: in the row index.

    My CODE
    Public Sub EditServingType_Load(ByVal source As Object,
    ByVal e As
    EventArgs)
    Dim servingType As CheckBoxList = CType(source,
    CheckBoxList)

    Select Case
    CType(CheckDBNull(Me.RestaurantsDataset.Tables(0).Rows
    (TODO: INDEX SHOULD GO HERE)("ServingTypeCode")),
    String).Trim
    Case "B"
    servingType.Items(0).Selected = True
    Case "L"
    servingType.Items(1).Selected = True
    Case "D"
    servingType.Items(2).Selected = True
    Case "BL"
    servingType.Items(0).Selected = True
    servingType.Items(1).Selected = True
    Case "BD"
    servingType.Items(0).Selected = True
    servingType.Items(2).Selected = True
    Case "LD"
    servingType.Items(1).Selected = True
    servingType.Items(2).Selected = True
    Case "BLD"
    servingType.Items(0).Selected = True
    servingType.Items(1).Selected = True
    servingType.Items(2).Selected = True
    End Select
    End Sub



    Your CODE:

    You can specify a
    "OnLoad" event handler for the DropDownList, for example:
    <asp:TemplateColumn> <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem,"gender") %>
    </ItemTemplate> <EditItemTemplate> <asp:DropDownList
    ID="lstGender" OnLoad="lstGender_Load" Runat="server">
    <asp:ListItem Value="male">Male</asp:ListItem>
    <asp:ListItem Value="female">Female</asp:ListItem>
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>

    Then, implement this handler function("lstGender_Load") in
    the Page Class: protected void lstGender_Load(object
    source, System.EventArgs e) { DropDownList lstGender =
    (DropDownList)source; //get the datasource of the DataGrid
    (you should change it to other
    //if you don't use DataTAble as the datasource)
    DataTable tb = (DataTable)gridTest.DataSource;
    DataRow row = tb.Rows[gridTest.EditItemIndex];

    if(row["gender"].Equals("male"))
    {
    lstGender.SelectedIndex = 0;
    }
    else
    {
    lstGender.SelectedIndex = 1;
    }
    }


    Then, in the DataGrid's EditCommand event, you just need
    to set its
    EditItemIndex and rebind the data:

    private void gridTest_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    gridTest.EditItemIndex = e.Item.ItemIndex;
    gridTest.DataSource = Get_Data();//Get_Data() returns a
    DataTable for
    testing
    gridTest.DataBind();
    }
    Paul, Jan 10, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    2
    Views:
    643
    Steven Cheng[MSFT]
    Jan 28, 2004
  2. VS 2005 & Web User Controls -for Steven Cheng

    , Jun 13, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    101
  3. Terry Holland

    DataGrid (fao Steven Cheng)

    Terry Holland, Aug 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    138
    Steven Cheng[MSFT]
    Aug 15, 2005
Loading...

Share This Page