Programmatically load dropdownlist into datagrid

Discussion in 'ASP .Net' started by Julia Hu, Aug 21, 2004.

  1. Julia Hu

    Julia Hu Guest

    Hi,
    I have a datagrid, and in different rows I need to
    programmatically bind different type of controls and load
    data into these controls. For example,in the first row I
    need to bind data into a textbox, and in the second row I
    need to bind data into a dropdownlist...It all depends on
    the data I select from the database.

    I cannot use TemplateColumn because it has to be the same
    type of control for one column.

    Thanks a lot,

    Julia
     
    Julia Hu, Aug 21, 2004
    #1
    1. Advertising

  2. Julia Hu

    Karl Guest

    Julia,
    Try something like this:

    <asp:TemplateColumn HeaderText="Hotel Type">
    <ItemTemplate>
    <asp:placeHolder ID="plc" Runat="server" />
    </ItemTemplate>
    </asp:TemplateColumn>

    I'm not sure if you are using c# or vb.net, and what you are binding too,
    but assuming you are in vb.net and binding to a dataset/table, your
    ItemDatabound event should look something like:

    Private Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As
    DataGridItemEventArgs) Handles grid.ItemDataBound
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
    ListItemType.AlternatingItem Then
    Dim plc As PlaceHolder = CType(e.Item.FindControl("plc"), PlaceHolder)
    If Not plc Is Nothing Then
    Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
    Select Case CInt(dr("type"))
    Case 1
    Dim txt As New TextBox
    txt.Text = CStr(dr("value"))
    plc.Controls.Add(txt)
    Case 2
    Dim ddl As New DropDownList
    plc.Controls.Add(ddl)
    End Select
    End If
    End If
    End Sub

    As you can see, we try to find the placeholder contorl, if it exists, we get
    a reference to the current row we are using, we can use the values in the
    row (here, I used a mythical "Type" column) to figure out what type of
    control goes into the placeholder.

    Karl

    "Julia Hu" <> wrote in message
    news:a8cd01c48786$4044e1c0$...
    > Hi,
    > I have a datagrid, and in different rows I need to
    > programmatically bind different type of controls and load
    > data into these controls. For example,in the first row I
    > need to bind data into a textbox, and in the second row I
    > need to bind data into a dropdownlist...It all depends on
    > the data I select from the database.
    >
    > I cannot use TemplateColumn because it has to be the same
    > type of control for one column.
    >
    > Thanks a lot,
    >
    > Julia
     
    Karl, Aug 21, 2004
    #2
    1. Advertising

  3. Julia Hu

    Julia Hu Guest

    Karl,

    Thank you for your quick response and appreciate your help!
    I will try it and let you know if it works.

    Julia


    >-----Original Message-----
    >Julia,
    >Try something like this:
    >
    ><asp:TemplateColumn HeaderText="Hotel Type">
    > <ItemTemplate>
    > <asp:placeHolder ID="plc" Runat="server" />
    > </ItemTemplate>
    ></asp:TemplateColumn>
    >
    >I'm not sure if you are using c# or vb.net, and what you

    are binding too,
    >but assuming you are in vb.net and binding to a

    dataset/table, your
    >ItemDatabound event should look something like:
    >
    > Private Sub Grid_ItemDataBound(ByVal sender As Object,

    ByVal e As
    >DataGridItemEventArgs) Handles grid.ItemDataBound
    > If e.Item.ItemType = ListItemType.Item OrElse

    e.Item.ItemType =
    >ListItemType.AlternatingItem Then
    > Dim plc As PlaceHolder = CType(e.Item.FindControl

    ("plc"), PlaceHolder)
    > If Not plc Is Nothing Then
    > Dim dr As DataRowView = CType

    (e.Item.DataItem, DataRowView)
    > Select Case CInt(dr("type"))
    > Case 1
    > Dim txt As New TextBox
    > txt.Text = CStr(dr("value"))
    > plc.Controls.Add(txt)
    > Case 2
    > Dim ddl As New DropDownList
    > plc.Controls.Add(ddl)
    > End Select
    > End If
    > End If
    > End Sub
    >
    >As you can see, we try to find the placeholder contorl,

    if it exists, we get
    >a reference to the current row we are using, we can use

    the values in the
    >row (here, I used a mythical "Type" column) to figure out

    what type of
    >control goes into the placeholder.
    >
    >Karl
    >
    >"Julia Hu" <> wrote in message
    >news:a8cd01c48786$4044e1c0$...
    >> Hi,
    >> I have a datagrid, and in different rows I need to
    >> programmatically bind different type of controls and

    load
    >> data into these controls. For example,in the first row I
    >> need to bind data into a textbox, and in the second row

    I
    >> need to bind data into a dropdownlist...It all depends

    on
    >> the data I select from the database.
    >>
    >> I cannot use TemplateColumn because it has to be the

    same
    >> type of control for one column.
    >>
    >> Thanks a lot,
    >>
    >> Julia

    >
    >
    >.
    >
     
    Julia Hu, Aug 21, 2004
    #3
  4. Julia Hu

    Julia Hu Guest

    Karl,
    The code works. And now I need to retrieve the data in
    the dropdownlist which embeded in the placeholder
    control. The following is my code does this, however it
    returns nothing.

    plcAI = CType(grid.Items(i).Cells(1).FindControl
    ("plcAI"), PlaceHolder)
    Dim ddl As DropDownList = CType(plcAI.FindControl
    ("ddlAI"), DropDownList)

    thanks again

    >-----Original Message-----
    >Julia,
    >Try something like this:
    >
    ><asp:TemplateColumn HeaderText="Hotel Type">
    > <ItemTemplate>
    > <asp:placeHolder ID="plc" Runat="server" />
    > </ItemTemplate>
    ></asp:TemplateColumn>
    >
    >I'm not sure if you are using c# or vb.net, and what you

    are binding too,
    >but assuming you are in vb.net and binding to a

    dataset/table, your
    >ItemDatabound event should look something like:
    >
    > Private Sub Grid_ItemDataBound(ByVal sender As

    Object, ByVal e As
    >DataGridItemEventArgs) Handles grid.ItemDataBound
    > If e.Item.ItemType = ListItemType.Item OrElse

    e.Item.ItemType =
    >ListItemType.AlternatingItem Then
    > Dim plc As PlaceHolder = CType(e.Item.FindControl

    ("plc"), PlaceHolder)
    > If Not plc Is Nothing Then
    > Dim dr As DataRowView = CType

    (e.Item.DataItem, DataRowView)
    > Select Case CInt(dr("type"))
    > Case 1
    > Dim txt As New TextBox
    > txt.Text = CStr(dr("value"))
    > plc.Controls.Add(txt)
    > Case 2
    > Dim ddl As New DropDownList
    > plc.Controls.Add(ddl)
    > End Select
    > End If
    > End If
    > End Sub
    >
    >As you can see, we try to find the placeholder contorl,

    if it exists, we get
    >a reference to the current row we are using, we can use

    the values in the
    >row (here, I used a mythical "Type" column) to figure

    out what type of
    >control goes into the placeholder.
    >
    >Karl
    >
    >"Julia Hu" <> wrote in message
    >news:a8cd01c48786$4044e1c0$...
    >> Hi,
    >> I have a datagrid, and in different rows I need to
    >> programmatically bind different type of controls and

    load
    >> data into these controls. For example,in the first row

    I
    >> need to bind data into a textbox, and in the second

    row I
    >> need to bind data into a dropdownlist...It all depends

    on
    >> the data I select from the database.
    >>
    >> I cannot use TemplateColumn because it has to be the

    same
    >> type of control for one column.
    >>
    >> Thanks a lot,
    >>
    >> Julia

    >
    >
    >.
    >
     
    Julia Hu, Aug 23, 2004
    #4
  5. Julia Hu

    Karl Guest

    What's null, plcAI or ddl?

    Karl

    "Julia Hu" <> wrote in message
    news:ba9401c48927$777a4c50$...
    > Karl,
    > The code works. And now I need to retrieve the data in
    > the dropdownlist which embeded in the placeholder
    > control. The following is my code does this, however it
    > returns nothing.
    >
    > plcAI = CType(grid.Items(i).Cells(1).FindControl
    > ("plcAI"), PlaceHolder)
    > Dim ddl As DropDownList = CType(plcAI.FindControl
    > ("ddlAI"), DropDownList)
    >
    > thanks again
    >
    > >-----Original Message-----
    > >Julia,
    > >Try something like this:
    > >
    > ><asp:TemplateColumn HeaderText="Hotel Type">
    > > <ItemTemplate>
    > > <asp:placeHolder ID="plc" Runat="server" />
    > > </ItemTemplate>
    > ></asp:TemplateColumn>
    > >
    > >I'm not sure if you are using c# or vb.net, and what you

    > are binding too,
    > >but assuming you are in vb.net and binding to a

    > dataset/table, your
    > >ItemDatabound event should look something like:
    > >
    > > Private Sub Grid_ItemDataBound(ByVal sender As

    > Object, ByVal e As
    > >DataGridItemEventArgs) Handles grid.ItemDataBound
    > > If e.Item.ItemType = ListItemType.Item OrElse

    > e.Item.ItemType =
    > >ListItemType.AlternatingItem Then
    > > Dim plc As PlaceHolder = CType(e.Item.FindControl

    > ("plc"), PlaceHolder)
    > > If Not plc Is Nothing Then
    > > Dim dr As DataRowView = CType

    > (e.Item.DataItem, DataRowView)
    > > Select Case CInt(dr("type"))
    > > Case 1
    > > Dim txt As New TextBox
    > > txt.Text = CStr(dr("value"))
    > > plc.Controls.Add(txt)
    > > Case 2
    > > Dim ddl As New DropDownList
    > > plc.Controls.Add(ddl)
    > > End Select
    > > End If
    > > End If
    > > End Sub
    > >
    > >As you can see, we try to find the placeholder contorl,

    > if it exists, we get
    > >a reference to the current row we are using, we can use

    > the values in the
    > >row (here, I used a mythical "Type" column) to figure

    > out what type of
    > >control goes into the placeholder.
    > >
    > >Karl
    > >
    > >"Julia Hu" <> wrote in message
    > >news:a8cd01c48786$4044e1c0$...
    > >> Hi,
    > >> I have a datagrid, and in different rows I need to
    > >> programmatically bind different type of controls and

    > load
    > >> data into these controls. For example,in the first row

    > I
    > >> need to bind data into a textbox, and in the second

    > row I
    > >> need to bind data into a dropdownlist...It all depends

    > on
    > >> the data I select from the database.
    > >>
    > >> I cannot use TemplateColumn because it has to be the

    > same
    > >> type of control for one column.
    > >>
    > >> Thanks a lot,
    > >>
    > >> Julia

    > >
    > >
    > >.
    > >
     
    Karl, Aug 23, 2004
    #5
  6. Julia Hu

    Julia Hu Guest

    plc is not null,but the ddl returns me Nothing!!
    thanks

    >-----Original Message-----
    >What's null, plcAI or ddl?
    >
    >Karl
    >
    >"Julia Hu" <> wrote in message
    >news:ba9401c48927$777a4c50$...
    >> Karl,
    >> The code works. And now I need to retrieve the data in
    >> the dropdownlist which embeded in the placeholder
    >> control. The following is my code does this, however it
    >> returns nothing.
    >>
    >> plcAI = CType(grid.Items(i).Cells(1).FindControl
    >> ("plcAI"), PlaceHolder)
    >> Dim ddl As DropDownList = CType(plcAI.FindControl
    >> ("ddlAI"), DropDownList)
    >>
    >> thanks again
    >>
    >> >-----Original Message-----
    >> >Julia,
    >> >Try something like this:
    >> >
    >> ><asp:TemplateColumn HeaderText="Hotel Type">
    >> > <ItemTemplate>
    >> > <asp:placeHolder ID="plc" Runat="server" />
    >> > </ItemTemplate>
    >> ></asp:TemplateColumn>
    >> >
    >> >I'm not sure if you are using c# or vb.net, and what

    you
    >> are binding too,
    >> >but assuming you are in vb.net and binding to a

    >> dataset/table, your
    >> >ItemDatabound event should look something like:
    >> >
    >> > Private Sub Grid_ItemDataBound(ByVal sender As

    >> Object, ByVal e As
    >> >DataGridItemEventArgs) Handles grid.ItemDataBound
    >> > If e.Item.ItemType = ListItemType.Item OrElse

    >> e.Item.ItemType =
    >> >ListItemType.AlternatingItem Then
    >> > Dim plc As PlaceHolder = CType

    (e.Item.FindControl
    >> ("plc"), PlaceHolder)
    >> > If Not plc Is Nothing Then
    >> > Dim dr As DataRowView = CType

    >> (e.Item.DataItem, DataRowView)
    >> > Select Case CInt(dr("type"))
    >> > Case 1
    >> > Dim txt As New TextBox
    >> > txt.Text = CStr(dr("value"))
    >> > plc.Controls.Add(txt)
    >> > Case 2
    >> > Dim ddl As New DropDownList
    >> > plc.Controls.Add(ddl)
    >> > End Select
    >> > End If
    >> > End If
    >> > End Sub
    >> >
    >> >As you can see, we try to find the placeholder

    contorl,
    >> if it exists, we get
    >> >a reference to the current row we are using, we can

    use
    >> the values in the
    >> >row (here, I used a mythical "Type" column) to figure

    >> out what type of
    >> >control goes into the placeholder.
    >> >
    >> >Karl
    >> >
    >> >"Julia Hu" <> wrote in message
    >> >news:a8cd01c48786$4044e1c0$...
    >> >> Hi,
    >> >> I have a datagrid, and in different rows I need to
    >> >> programmatically bind different type of controls and

    >> load
    >> >> data into these controls. For example,in the first

    row
    >> I
    >> >> need to bind data into a textbox, and in the second

    >> row I
    >> >> need to bind data into a dropdownlist...It all

    depends
    >> on
    >> >> the data I select from the database.
    >> >>
    >> >> I cannot use TemplateColumn because it has to be the

    >> same
    >> >> type of control for one column.
    >> >>
    >> >> Thanks a lot,
    >> >>
    >> >> Julia
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    Julia Hu, Aug 23, 2004
    #6
  7. Julia Hu

    Karl Guest

    On postback you'll need to re-add the controls

    Karl

    "Karl" <none> wrote in message news:...
    > What's null, plcAI or ddl?
    >
    > Karl
    >
    > "Julia Hu" <> wrote in message
    > news:ba9401c48927$777a4c50$...
    > > Karl,
    > > The code works. And now I need to retrieve the data in
    > > the dropdownlist which embeded in the placeholder
    > > control. The following is my code does this, however it
    > > returns nothing.
    > >
    > > plcAI = CType(grid.Items(i).Cells(1).FindControl
    > > ("plcAI"), PlaceHolder)
    > > Dim ddl As DropDownList = CType(plcAI.FindControl
    > > ("ddlAI"), DropDownList)
    > >
    > > thanks again
    > >
    > > >-----Original Message-----
    > > >Julia,
    > > >Try something like this:
    > > >
    > > ><asp:TemplateColumn HeaderText="Hotel Type">
    > > > <ItemTemplate>
    > > > <asp:placeHolder ID="plc" Runat="server" />
    > > > </ItemTemplate>
    > > ></asp:TemplateColumn>
    > > >
    > > >I'm not sure if you are using c# or vb.net, and what you

    > > are binding too,
    > > >but assuming you are in vb.net and binding to a

    > > dataset/table, your
    > > >ItemDatabound event should look something like:
    > > >
    > > > Private Sub Grid_ItemDataBound(ByVal sender As

    > > Object, ByVal e As
    > > >DataGridItemEventArgs) Handles grid.ItemDataBound
    > > > If e.Item.ItemType = ListItemType.Item OrElse

    > > e.Item.ItemType =
    > > >ListItemType.AlternatingItem Then
    > > > Dim plc As PlaceHolder = CType(e.Item.FindControl

    > > ("plc"), PlaceHolder)
    > > > If Not plc Is Nothing Then
    > > > Dim dr As DataRowView = CType

    > > (e.Item.DataItem, DataRowView)
    > > > Select Case CInt(dr("type"))
    > > > Case 1
    > > > Dim txt As New TextBox
    > > > txt.Text = CStr(dr("value"))
    > > > plc.Controls.Add(txt)
    > > > Case 2
    > > > Dim ddl As New DropDownList
    > > > plc.Controls.Add(ddl)
    > > > End Select
    > > > End If
    > > > End If
    > > > End Sub
    > > >
    > > >As you can see, we try to find the placeholder contorl,

    > > if it exists, we get
    > > >a reference to the current row we are using, we can use

    > > the values in the
    > > >row (here, I used a mythical "Type" column) to figure

    > > out what type of
    > > >control goes into the placeholder.
    > > >
    > > >Karl
    > > >
    > > >"Julia Hu" <> wrote in message
    > > >news:a8cd01c48786$4044e1c0$...
    > > >> Hi,
    > > >> I have a datagrid, and in different rows I need to
    > > >> programmatically bind different type of controls and

    > > load
    > > >> data into these controls. For example,in the first row

    > > I
    > > >> need to bind data into a textbox, and in the second

    > > row I
    > > >> need to bind data into a dropdownlist...It all depends

    > > on
    > > >> the data I select from the database.
    > > >>
    > > >> I cannot use TemplateColumn because it has to be the

    > > same
    > > >> type of control for one column.
    > > >>
    > > >> Thanks a lot,
    > > >>
    > > >> Julia
    > > >
    > > >
    > > >.
    > > >

    >
    >
     
    Karl, Aug 23, 2004
    #7
  8. Julia Hu

    Julia Hu Guest

    As you told me, I loaded controls in ItemDataBound event,
    and how can I re-add them?
    code like this:
    Private Sub grdAI_ItemDataBound(ByVal sender As Object,
    ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    grdAI.ItemDataBound
    Dim i As Integer
    Dim plcAI As PlaceHolder = CType
    (e.Item.FindControl("plcAI"), PlaceHolder)

    If Not plcAI Is Nothing Then
    Dim dr As DataRowView = CType
    (e.Item.DataItem, DataRowView)
    Select Case CInt(dr("FieldType"))
    Case 6 'ListView type
    Dim ddlAI As New DropDownList
    ddlAI.ID = "ddlAI"

    ddlAI.Items.Add("Big")
    ddlAI.Items.Add("Small")

    plcAI.Controls.Add(ddlAI)


    Case Else
    Dim txt As New TextBox
    txt.Text = ""
    plcAI.Controls.Add(txt)
    End Select
    End If

    End Sub

    >-----Original Message-----
    >On postback you'll need to re-add the controls
    >
    >Karl
    >
    >"Karl" <none> wrote in message

    news:...
    >> What's null, plcAI or ddl?
    >>
    >> Karl
    >>
    >> "Julia Hu" <> wrote in message
    >> news:ba9401c48927$777a4c50$...
    >> > Karl,
    >> > The code works. And now I need to retrieve the data

    in
    >> > the dropdownlist which embeded in the placeholder
    >> > control. The following is my code does this, however

    it
    >> > returns nothing.
    >> >
    >> > plcAI = CType(grid.Items(i).Cells(1).FindControl
    >> > ("plcAI"), PlaceHolder)
    >> > Dim ddl As DropDownList = CType(plcAI.FindControl
    >> > ("ddlAI"), DropDownList)
    >> >
    >> > thanks again
    >> >
    >> > >-----Original Message-----
    >> > >Julia,
    >> > >Try something like this:
    >> > >
    >> > ><asp:TemplateColumn HeaderText="Hotel Type">
    >> > > <ItemTemplate>
    >> > > <asp:placeHolder ID="plc" Runat="server" />
    >> > > </ItemTemplate>
    >> > ></asp:TemplateColumn>
    >> > >
    >> > >I'm not sure if you are using c# or vb.net, and

    what you
    >> > are binding too,
    >> > >but assuming you are in vb.net and binding to a
    >> > dataset/table, your
    >> > >ItemDatabound event should look something like:
    >> > >
    >> > > Private Sub Grid_ItemDataBound(ByVal sender As
    >> > Object, ByVal e As
    >> > >DataGridItemEventArgs) Handles grid.ItemDataBound
    >> > > If e.Item.ItemType = ListItemType.Item OrElse
    >> > e.Item.ItemType =
    >> > >ListItemType.AlternatingItem Then
    >> > > Dim plc As PlaceHolder = CType

    (e.Item.FindControl
    >> > ("plc"), PlaceHolder)
    >> > > If Not plc Is Nothing Then
    >> > > Dim dr As DataRowView = CType
    >> > (e.Item.DataItem, DataRowView)
    >> > > Select Case CInt(dr("type"))
    >> > > Case 1
    >> > > Dim txt As New TextBox
    >> > > txt.Text = CStr(dr("value"))
    >> > > plc.Controls.Add(txt)
    >> > > Case 2
    >> > > Dim ddl As New DropDownList
    >> > > plc.Controls.Add(ddl)
    >> > > End Select
    >> > > End If
    >> > > End If
    >> > > End Sub
    >> > >
    >> > >As you can see, we try to find the placeholder

    contorl,
    >> > if it exists, we get
    >> > >a reference to the current row we are using, we can

    use
    >> > the values in the
    >> > >row (here, I used a mythical "Type" column) to

    figure
    >> > out what type of
    >> > >control goes into the placeholder.
    >> > >
    >> > >Karl
    >> > >
    >> > >"Julia Hu" <> wrote in message
    >> > >news:a8cd01c48786$4044e1c0$...
    >> > >> Hi,
    >> > >> I have a datagrid, and in different rows I need to
    >> > >> programmatically bind different type of controls

    and
    >> > load
    >> > >> data into these controls. For example,in the

    first row
    >> > I
    >> > >> need to bind data into a textbox, and in the

    second
    >> > row I
    >> > >> need to bind data into a dropdownlist...It all

    depends
    >> > on
    >> > >> the data I select from the database.
    >> > >>
    >> > >> I cannot use TemplateColumn because it has to be

    the
    >> > same
    >> > >> type of control for one column.
    >> > >>
    >> > >> Thanks a lot,
    >> > >>
    >> > >> Julia
    >> > >
    >> > >
    >> > >.
    >> > >

    >>
    >>

    >
    >
    >.
    >
     
    Julia Hu, Aug 23, 2004
    #8
  9. Julia Hu

    Julia Hu Guest

    If I reload controls again in postback, the selected item
    of the dropdownlist will be lost. How do I keep it?


    >-----Original Message-----
    >As you told me, I loaded controls in ItemDataBound

    event,
    >and how can I re-add them?
    >code like this:
    > Private Sub grdAI_ItemDataBound(ByVal sender As

    Object,
    >ByVal e As
    >System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    >grdAI.ItemDataBound
    > Dim i As Integer
    > Dim plcAI As PlaceHolder = CType
    >(e.Item.FindControl("plcAI"), PlaceHolder)
    >
    > If Not plcAI Is Nothing Then
    > Dim dr As DataRowView = CType
    >(e.Item.DataItem, DataRowView)
    > Select Case CInt(dr("FieldType"))
    > Case 6 'ListView type
    > Dim ddlAI As New DropDownList
    > ddlAI.ID = "ddlAI"
    >
    > ddlAI.Items.Add("Big")
    > ddlAI.Items.Add("Small")
    >
    > plcAI.Controls.Add(ddlAI)
    >
    >
    > Case Else
    > Dim txt As New TextBox
    > txt.Text = ""
    > plcAI.Controls.Add(txt)
    > End Select
    > End If
    >
    > End Sub
    >
    >>-----Original Message-----
    >>On postback you'll need to re-add the controls
    >>
    >>Karl
    >>
    >>"Karl" <none> wrote in message

    >news:...
    >>> What's null, plcAI or ddl?
    >>>
    >>> Karl
    >>>
    >>> "Julia Hu" <> wrote in message
    >>> news:ba9401c48927$777a4c50$...
    >>> > Karl,
    >>> > The code works. And now I need to retrieve the data

    >in
    >>> > the dropdownlist which embeded in the placeholder
    >>> > control. The following is my code does this,

    however
    >it
    >>> > returns nothing.
    >>> >
    >>> > plcAI = CType(grid.Items(i).Cells(1).FindControl
    >>> > ("plcAI"), PlaceHolder)
    >>> > Dim ddl As DropDownList = CType(plcAI.FindControl
    >>> > ("ddlAI"), DropDownList)
    >>> >
    >>> > thanks again
    >>> >
    >>> > >-----Original Message-----
    >>> > >Julia,
    >>> > >Try something like this:
    >>> > >
    >>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    >>> > > <ItemTemplate>
    >>> > > <asp:placeHolder ID="plc" Runat="server" />
    >>> > > </ItemTemplate>
    >>> > ></asp:TemplateColumn>
    >>> > >
    >>> > >I'm not sure if you are using c# or vb.net, and

    >what you
    >>> > are binding too,
    >>> > >but assuming you are in vb.net and binding to a
    >>> > dataset/table, your
    >>> > >ItemDatabound event should look something like:
    >>> > >
    >>> > > Private Sub Grid_ItemDataBound(ByVal sender As
    >>> > Object, ByVal e As
    >>> > >DataGridItemEventArgs) Handles grid.ItemDataBound
    >>> > > If e.Item.ItemType = ListItemType.Item OrElse
    >>> > e.Item.ItemType =
    >>> > >ListItemType.AlternatingItem Then
    >>> > > Dim plc As PlaceHolder = CType

    >(e.Item.FindControl
    >>> > ("plc"), PlaceHolder)
    >>> > > If Not plc Is Nothing Then
    >>> > > Dim dr As DataRowView = CType
    >>> > (e.Item.DataItem, DataRowView)
    >>> > > Select Case CInt(dr("type"))
    >>> > > Case 1
    >>> > > Dim txt As New TextBox
    >>> > > txt.Text = CStr(dr("value"))
    >>> > > plc.Controls.Add(txt)
    >>> > > Case 2
    >>> > > Dim ddl As New DropDownList
    >>> > > plc.Controls.Add(ddl)
    >>> > > End Select
    >>> > > End If
    >>> > > End If
    >>> > > End Sub
    >>> > >
    >>> > >As you can see, we try to find the placeholder

    >contorl,
    >>> > if it exists, we get
    >>> > >a reference to the current row we are using, we

    can
    >use
    >>> > the values in the
    >>> > >row (here, I used a mythical "Type" column) to

    >figure
    >>> > out what type of
    >>> > >control goes into the placeholder.
    >>> > >
    >>> > >Karl
    >>> > >
    >>> > >"Julia Hu" <> wrote in message
    >>> > >news:a8cd01c48786$4044e1c0$...
    >>> > >> Hi,
    >>> > >> I have a datagrid, and in different rows I need

    to
    >>> > >> programmatically bind different type of controls

    >and
    >>> > load
    >>> > >> data into these controls. For example,in the

    >first row
    >>> > I
    >>> > >> need to bind data into a textbox, and in the

    >second
    >>> > row I
    >>> > >> need to bind data into a dropdownlist...It all

    >depends
    >>> > on
    >>> > >> the data I select from the database.
    >>> > >>
    >>> > >> I cannot use TemplateColumn because it has to be

    >the
    >>> > same
    >>> > >> type of control for one column.
    >>> > >>
    >>> > >> Thanks a lot,
    >>> > >>
    >>> > >> Julia
    >>> > >
    >>> > >
    >>> > >.
    >>> > >
    >>>
    >>>

    >>
    >>
    >>.
    >>

    >.
    >
     
    Julia Hu, Aug 23, 2004
    #9
  10. Julia Hu

    Karl Guest

    You have two solutions, neither is particularly nice.

    One of those solution is to rebuild the controls. The other one is to
    access their value directly from Request.Form() Your problem with the
    request.Form is figuring out what the name of the damn thing will be (it
    won't be random - far from it actually). It'll be something like grid.id +
    ":" + row.id +... well not nearly that, but something along the lines of
    parentId appended to childId.

    The other solution is, as I mentioned, to rebuild the controls. This
    solution has its own ugliness...if the above method will work for you, I
    suggest you try it out. Otherwise, you'll need to store values in the
    ViewState that says which controls were loaded, then on page_load, read the
    viewstate, reload everything, and you'll be able to access them. You can
    clean things up a lot by refactoring the creationg code.

    For example, instead of adding the ddl when case 6, simply call a method,
    which your page_load can also call.
    case 6 'listvie type
    AddListView("ddlAI")

    Anyways, try the first solution (or maybe something else)...if ti doesn't
    work and you need more help with the 2nd one, let me know.

    Karl

    "Julia Hu" <> wrote in message
    news:476f01c48937$faf2f6d0$...
    > As you told me, I loaded controls in ItemDataBound event,
    > and how can I re-add them?
    > code like this:
    > Private Sub grdAI_ItemDataBound(ByVal sender As Object,
    > ByVal e As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > grdAI.ItemDataBound
    > Dim i As Integer
    > Dim plcAI As PlaceHolder = CType
    > (e.Item.FindControl("plcAI"), PlaceHolder)
    >
    > If Not plcAI Is Nothing Then
    > Dim dr As DataRowView = CType
    > (e.Item.DataItem, DataRowView)
    > Select Case CInt(dr("FieldType"))
    > Case 6 'ListView type
    > Dim ddlAI As New DropDownList
    > ddlAI.ID = "ddlAI"
    >
    > ddlAI.Items.Add("Big")
    > ddlAI.Items.Add("Small")
    >
    > plcAI.Controls.Add(ddlAI)
    >
    >
    > Case Else
    > Dim txt As New TextBox
    > txt.Text = ""
    > plcAI.Controls.Add(txt)
    > End Select
    > End If
    >
    > End Sub
    >
    > >-----Original Message-----
    > >On postback you'll need to re-add the controls
    > >
    > >Karl
    > >
    > >"Karl" <none> wrote in message

    > news:...
    > >> What's null, plcAI or ddl?
    > >>
    > >> Karl
    > >>
    > >> "Julia Hu" <> wrote in message
    > >> news:ba9401c48927$777a4c50$...
    > >> > Karl,
    > >> > The code works. And now I need to retrieve the data

    > in
    > >> > the dropdownlist which embeded in the placeholder
    > >> > control. The following is my code does this, however

    > it
    > >> > returns nothing.
    > >> >
    > >> > plcAI = CType(grid.Items(i).Cells(1).FindControl
    > >> > ("plcAI"), PlaceHolder)
    > >> > Dim ddl As DropDownList = CType(plcAI.FindControl
    > >> > ("ddlAI"), DropDownList)
    > >> >
    > >> > thanks again
    > >> >
    > >> > >-----Original Message-----
    > >> > >Julia,
    > >> > >Try something like this:
    > >> > >
    > >> > ><asp:TemplateColumn HeaderText="Hotel Type">
    > >> > > <ItemTemplate>
    > >> > > <asp:placeHolder ID="plc" Runat="server" />
    > >> > > </ItemTemplate>
    > >> > ></asp:TemplateColumn>
    > >> > >
    > >> > >I'm not sure if you are using c# or vb.net, and

    > what you
    > >> > are binding too,
    > >> > >but assuming you are in vb.net and binding to a
    > >> > dataset/table, your
    > >> > >ItemDatabound event should look something like:
    > >> > >
    > >> > > Private Sub Grid_ItemDataBound(ByVal sender As
    > >> > Object, ByVal e As
    > >> > >DataGridItemEventArgs) Handles grid.ItemDataBound
    > >> > > If e.Item.ItemType = ListItemType.Item OrElse
    > >> > e.Item.ItemType =
    > >> > >ListItemType.AlternatingItem Then
    > >> > > Dim plc As PlaceHolder = CType

    > (e.Item.FindControl
    > >> > ("plc"), PlaceHolder)
    > >> > > If Not plc Is Nothing Then
    > >> > > Dim dr As DataRowView = CType
    > >> > (e.Item.DataItem, DataRowView)
    > >> > > Select Case CInt(dr("type"))
    > >> > > Case 1
    > >> > > Dim txt As New TextBox
    > >> > > txt.Text = CStr(dr("value"))
    > >> > > plc.Controls.Add(txt)
    > >> > > Case 2
    > >> > > Dim ddl As New DropDownList
    > >> > > plc.Controls.Add(ddl)
    > >> > > End Select
    > >> > > End If
    > >> > > End If
    > >> > > End Sub
    > >> > >
    > >> > >As you can see, we try to find the placeholder

    > contorl,
    > >> > if it exists, we get
    > >> > >a reference to the current row we are using, we can

    > use
    > >> > the values in the
    > >> > >row (here, I used a mythical "Type" column) to

    > figure
    > >> > out what type of
    > >> > >control goes into the placeholder.
    > >> > >
    > >> > >Karl
    > >> > >
    > >> > >"Julia Hu" <> wrote in message
    > >> > >news:a8cd01c48786$4044e1c0$...
    > >> > >> Hi,
    > >> > >> I have a datagrid, and in different rows I need to
    > >> > >> programmatically bind different type of controls

    > and
    > >> > load
    > >> > >> data into these controls. For example,in the

    > first row
    > >> > I
    > >> > >> need to bind data into a textbox, and in the

    > second
    > >> > row I
    > >> > >> need to bind data into a dropdownlist...It all

    > depends
    > >> > on
    > >> > >> the data I select from the database.
    > >> > >>
    > >> > >> I cannot use TemplateColumn because it has to be

    > the
    > >> > same
    > >> > >> type of control for one column.
    > >> > >>
    > >> > >> Thanks a lot,
    > >> > >>
    > >> > >> Julia
    > >> > >
    > >> > >
    > >> > >.
    > >> > >
    > >>
    > >>

    > >
    > >
    > >.
    > >
     
    Karl, Aug 23, 2004
    #10
  11. Julia Hu

    Karl Guest

    If it gets re-added in the page_load, it'll happen before the Begin
    ProcessPostData Second Try which will preserve the value...If you do it in
    the button click, you'll lose it.

    Karl

    "Julia Hu" <> wrote in message
    news:ba2b01c4893a$66d685e0$...
    > If I reload controls again in postback, the selected item
    > of the dropdownlist will be lost. How do I keep it?
    >
    >
    > >-----Original Message-----
    > >As you told me, I loaded controls in ItemDataBound

    > event,
    > >and how can I re-add them?
    > >code like this:
    > > Private Sub grdAI_ItemDataBound(ByVal sender As

    > Object,
    > >ByVal e As
    > >System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > >grdAI.ItemDataBound
    > > Dim i As Integer
    > > Dim plcAI As PlaceHolder = CType
    > >(e.Item.FindControl("plcAI"), PlaceHolder)
    > >
    > > If Not plcAI Is Nothing Then
    > > Dim dr As DataRowView = CType
    > >(e.Item.DataItem, DataRowView)
    > > Select Case CInt(dr("FieldType"))
    > > Case 6 'ListView type
    > > Dim ddlAI As New DropDownList
    > > ddlAI.ID = "ddlAI"
    > >
    > > ddlAI.Items.Add("Big")
    > > ddlAI.Items.Add("Small")
    > >
    > > plcAI.Controls.Add(ddlAI)
    > >
    > >
    > > Case Else
    > > Dim txt As New TextBox
    > > txt.Text = ""
    > > plcAI.Controls.Add(txt)
    > > End Select
    > > End If
    > >
    > > End Sub
    > >
    > >>-----Original Message-----
    > >>On postback you'll need to re-add the controls
    > >>
    > >>Karl
    > >>
    > >>"Karl" <none> wrote in message

    > >news:...
    > >>> What's null, plcAI or ddl?
    > >>>
    > >>> Karl
    > >>>
    > >>> "Julia Hu" <> wrote in message
    > >>> news:ba9401c48927$777a4c50$...
    > >>> > Karl,
    > >>> > The code works. And now I need to retrieve the data

    > >in
    > >>> > the dropdownlist which embeded in the placeholder
    > >>> > control. The following is my code does this,

    > however
    > >it
    > >>> > returns nothing.
    > >>> >
    > >>> > plcAI = CType(grid.Items(i).Cells(1).FindControl
    > >>> > ("plcAI"), PlaceHolder)
    > >>> > Dim ddl As DropDownList = CType(plcAI.FindControl
    > >>> > ("ddlAI"), DropDownList)
    > >>> >
    > >>> > thanks again
    > >>> >
    > >>> > >-----Original Message-----
    > >>> > >Julia,
    > >>> > >Try something like this:
    > >>> > >
    > >>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    > >>> > > <ItemTemplate>
    > >>> > > <asp:placeHolder ID="plc" Runat="server" />
    > >>> > > </ItemTemplate>
    > >>> > ></asp:TemplateColumn>
    > >>> > >
    > >>> > >I'm not sure if you are using c# or vb.net, and

    > >what you
    > >>> > are binding too,
    > >>> > >but assuming you are in vb.net and binding to a
    > >>> > dataset/table, your
    > >>> > >ItemDatabound event should look something like:
    > >>> > >
    > >>> > > Private Sub Grid_ItemDataBound(ByVal sender As
    > >>> > Object, ByVal e As
    > >>> > >DataGridItemEventArgs) Handles grid.ItemDataBound
    > >>> > > If e.Item.ItemType = ListItemType.Item OrElse
    > >>> > e.Item.ItemType =
    > >>> > >ListItemType.AlternatingItem Then
    > >>> > > Dim plc As PlaceHolder = CType

    > >(e.Item.FindControl
    > >>> > ("plc"), PlaceHolder)
    > >>> > > If Not plc Is Nothing Then
    > >>> > > Dim dr As DataRowView = CType
    > >>> > (e.Item.DataItem, DataRowView)
    > >>> > > Select Case CInt(dr("type"))
    > >>> > > Case 1
    > >>> > > Dim txt As New TextBox
    > >>> > > txt.Text = CStr(dr("value"))
    > >>> > > plc.Controls.Add(txt)
    > >>> > > Case 2
    > >>> > > Dim ddl As New DropDownList
    > >>> > > plc.Controls.Add(ddl)
    > >>> > > End Select
    > >>> > > End If
    > >>> > > End If
    > >>> > > End Sub
    > >>> > >
    > >>> > >As you can see, we try to find the placeholder

    > >contorl,
    > >>> > if it exists, we get
    > >>> > >a reference to the current row we are using, we

    > can
    > >use
    > >>> > the values in the
    > >>> > >row (here, I used a mythical "Type" column) to

    > >figure
    > >>> > out what type of
    > >>> > >control goes into the placeholder.
    > >>> > >
    > >>> > >Karl
    > >>> > >
    > >>> > >"Julia Hu" <> wrote in message
    > >>> > >news:a8cd01c48786$4044e1c0$...
    > >>> > >> Hi,
    > >>> > >> I have a datagrid, and in different rows I need

    > to
    > >>> > >> programmatically bind different type of controls

    > >and
    > >>> > load
    > >>> > >> data into these controls. For example,in the

    > >first row
    > >>> > I
    > >>> > >> need to bind data into a textbox, and in the

    > >second
    > >>> > row I
    > >>> > >> need to bind data into a dropdownlist...It all

    > >depends
    > >>> > on
    > >>> > >> the data I select from the database.
    > >>> > >>
    > >>> > >> I cannot use TemplateColumn because it has to be

    > >the
    > >>> > same
    > >>> > >> type of control for one column.
    > >>> > >>
    > >>> > >> Thanks a lot,
    > >>> > >>
    > >>> > >> Julia
    > >>> > >
    > >>> > >
    > >>> > >.
    > >>> > >
    > >>>
    > >>>
    > >>
    > >>
    > >>.
    > >>

    > >.
    > >
     
    Karl, Aug 23, 2004
    #11
  12. Julia Hu

    Julia Hu Guest

    I tried both ClientID and UniqueID and the UniqueID
    works!! I got the SelectedValue, and how can I get the
    SelectedText??

    >-----Original Message-----
    >If I reload controls again in postback, the selected

    item
    >of the dropdownlist will be lost. How do I keep it?
    >
    >
    >>-----Original Message-----
    >>As you told me, I loaded controls in ItemDataBound

    >event,
    >>and how can I re-add them?
    >>code like this:
    >> Private Sub grdAI_ItemDataBound(ByVal sender As

    >Object,
    >>ByVal e As
    >>System.Web.UI.WebControls.DataGridItemEventArgs)

    Handles
    >>grdAI.ItemDataBound
    >> Dim i As Integer
    >> Dim plcAI As PlaceHolder = CType
    >>(e.Item.FindControl("plcAI"), PlaceHolder)
    >>
    >> If Not plcAI Is Nothing Then
    >> Dim dr As DataRowView = CType
    >>(e.Item.DataItem, DataRowView)
    >> Select Case CInt(dr("FieldType"))
    >> Case 6 'ListView type
    >> Dim ddlAI As New DropDownList
    >> ddlAI.ID = "ddlAI"
    >>
    >> ddlAI.Items.Add("Big")
    >> ddlAI.Items.Add("Small")
    >>
    >> plcAI.Controls.Add(ddlAI)
    >>
    >>
    >> Case Else
    >> Dim txt As New TextBox
    >> txt.Text = ""
    >> plcAI.Controls.Add(txt)
    >> End Select
    >> End If
    >>
    >> End Sub
    >>
    >>>-----Original Message-----
    >>>On postback you'll need to re-add the controls
    >>>
    >>>Karl
    >>>
    >>>"Karl" <none> wrote in message

    >>news:...
    >>>> What's null, plcAI or ddl?
    >>>>
    >>>> Karl
    >>>>
    >>>> "Julia Hu" <> wrote in message
    >>>> news:ba9401c48927$777a4c50$...
    >>>> > Karl,
    >>>> > The code works. And now I need to retrieve the

    data
    >>in
    >>>> > the dropdownlist which embeded in the placeholder
    >>>> > control. The following is my code does this,

    >however
    >>it
    >>>> > returns nothing.
    >>>> >
    >>>> > plcAI = CType(grid.Items(i).Cells(1).FindControl
    >>>> > ("plcAI"), PlaceHolder)
    >>>> > Dim ddl As DropDownList = CType(plcAI.FindControl
    >>>> > ("ddlAI"), DropDownList)
    >>>> >
    >>>> > thanks again
    >>>> >
    >>>> > >-----Original Message-----
    >>>> > >Julia,
    >>>> > >Try something like this:
    >>>> > >
    >>>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    >>>> > > <ItemTemplate>
    >>>> > > <asp:placeHolder ID="plc" Runat="server" />
    >>>> > > </ItemTemplate>
    >>>> > ></asp:TemplateColumn>
    >>>> > >
    >>>> > >I'm not sure if you are using c# or vb.net, and

    >>what you
    >>>> > are binding too,
    >>>> > >but assuming you are in vb.net and binding to a
    >>>> > dataset/table, your
    >>>> > >ItemDatabound event should look something like:
    >>>> > >
    >>>> > > Private Sub Grid_ItemDataBound(ByVal sender As
    >>>> > Object, ByVal e As
    >>>> > >DataGridItemEventArgs) Handles grid.ItemDataBound
    >>>> > > If e.Item.ItemType = ListItemType.Item

    OrElse
    >>>> > e.Item.ItemType =
    >>>> > >ListItemType.AlternatingItem Then
    >>>> > > Dim plc As PlaceHolder = CType

    >>(e.Item.FindControl
    >>>> > ("plc"), PlaceHolder)
    >>>> > > If Not plc Is Nothing Then
    >>>> > > Dim dr As DataRowView = CType
    >>>> > (e.Item.DataItem, DataRowView)
    >>>> > > Select Case CInt(dr("type"))
    >>>> > > Case 1
    >>>> > > Dim txt As New TextBox
    >>>> > > txt.Text = CStr(dr("value"))
    >>>> > > plc.Controls.Add(txt)
    >>>> > > Case 2
    >>>> > > Dim ddl As New DropDownList
    >>>> > > plc.Controls.Add(ddl)
    >>>> > > End Select
    >>>> > > End If
    >>>> > > End If
    >>>> > > End Sub
    >>>> > >
    >>>> > >As you can see, we try to find the placeholder

    >>contorl,
    >>>> > if it exists, we get
    >>>> > >a reference to the current row we are using, we

    >can
    >>use
    >>>> > the values in the
    >>>> > >row (here, I used a mythical "Type" column) to

    >>figure
    >>>> > out what type of
    >>>> > >control goes into the placeholder.
    >>>> > >
    >>>> > >Karl
    >>>> > >
    >>>> > >"Julia Hu" <> wrote in message
    >>>> > >news:a8cd01c48786$4044e1c0$...
    >>>> > >> Hi,
    >>>> > >> I have a datagrid, and in different rows I need

    >to
    >>>> > >> programmatically bind different type of

    controls
    >>and
    >>>> > load
    >>>> > >> data into these controls. For example,in the

    >>first row
    >>>> > I
    >>>> > >> need to bind data into a textbox, and in the

    >>second
    >>>> > row I
    >>>> > >> need to bind data into a dropdownlist...It all

    >>depends
    >>>> > on
    >>>> > >> the data I select from the database.
    >>>> > >>
    >>>> > >> I cannot use TemplateColumn because it has to

    be
    >>the
    >>>> > same
    >>>> > >> type of control for one column.
    >>>> > >>
    >>>> > >> Thanks a lot,
    >>>> > >>
    >>>> > >> Julia
    >>>> > >
    >>>> > >
    >>>> > >.
    >>>> > >
    >>>>
    >>>>
    >>>
    >>>
    >>>.
    >>>

    >>.
    >>

    >.
    >
     
    Julia Hu, Aug 23, 2004
    #12
  13. Julia Hu

    Julia Hu Guest

    Really appreciate your assistance!
    I tried both ClientID and UniqueID and the UniqueID
    works!! I got the SelectedValue, and how can I get the
    SelectedText??

    >-----Original Message-----
    >You have two solutions, neither is particularly nice.
    >
    >One of those solution is to rebuild the controls. The

    other one is to
    >access their value directly from Request.Form() Your

    problem with the
    >request.Form is figuring out what the name of the damn

    thing will be (it
    >won't be random - far from it actually). It'll be

    something like grid.id +
    >":" + row.id +... well not nearly that, but something

    along the lines of
    >parentId appended to childId.
    >
    >The other solution is, as I mentioned, to rebuild the

    controls. This
    >solution has its own ugliness...if the above method will

    work for you, I
    >suggest you try it out. Otherwise, you'll need to store

    values in the
    >ViewState that says which controls were loaded, then on

    page_load, read the
    >viewstate, reload everything, and you'll be able to

    access them. You can
    >clean things up a lot by refactoring the creationg code.
    >
    >For example, instead of adding the ddl when case 6,

    simply call a method,
    >which your page_load can also call.
    >case 6 'listvie type
    > AddListView("ddlAI")
    >
    >Anyways, try the first solution (or maybe something

    else)...if ti doesn't
    >work and you need more help with the 2nd one, let me

    know.
    >
    >Karl
    >
    >"Julia Hu" <> wrote in message
    >news:476f01c48937$faf2f6d0$...
    >> As you told me, I loaded controls in ItemDataBound

    event,
    >> and how can I re-add them?
    >> code like this:
    >> Private Sub grdAI_ItemDataBound(ByVal sender As

    Object,
    >> ByVal e As
    >> System.Web.UI.WebControls.DataGridItemEventArgs)

    Handles
    >> grdAI.ItemDataBound
    >> Dim i As Integer
    >> Dim plcAI As PlaceHolder = CType
    >> (e.Item.FindControl("plcAI"), PlaceHolder)
    >>
    >> If Not plcAI Is Nothing Then
    >> Dim dr As DataRowView = CType
    >> (e.Item.DataItem, DataRowView)
    >> Select Case CInt(dr("FieldType"))
    >> Case 6 'ListView type
    >> Dim ddlAI As New DropDownList
    >> ddlAI.ID = "ddlAI"
    >>
    >> ddlAI.Items.Add("Big")
    >> ddlAI.Items.Add("Small")
    >>
    >> plcAI.Controls.Add(ddlAI)
    >>
    >>
    >> Case Else
    >> Dim txt As New TextBox
    >> txt.Text = ""
    >> plcAI.Controls.Add(txt)
    >> End Select
    >> End If
    >>
    >> End Sub
    >>
    >> >-----Original Message-----
    >> >On postback you'll need to re-add the controls
    >> >
    >> >Karl
    >> >
    >> >"Karl" <none> wrote in message

    >> news:...
    >> >> What's null, plcAI or ddl?
    >> >>
    >> >> Karl
    >> >>
    >> >> "Julia Hu" <> wrote in message
    >> >> news:ba9401c48927$777a4c50$...
    >> >> > Karl,
    >> >> > The code works. And now I need to retrieve the

    data
    >> in
    >> >> > the dropdownlist which embeded in the placeholder
    >> >> > control. The following is my code does this,

    however
    >> it
    >> >> > returns nothing.
    >> >> >
    >> >> > plcAI = CType(grid.Items(i).Cells(1).FindControl
    >> >> > ("plcAI"), PlaceHolder)
    >> >> > Dim ddl As DropDownList = CType(plcAI.FindControl
    >> >> > ("ddlAI"), DropDownList)
    >> >> >
    >> >> > thanks again
    >> >> >
    >> >> > >-----Original Message-----
    >> >> > >Julia,
    >> >> > >Try something like this:
    >> >> > >
    >> >> > ><asp:TemplateColumn HeaderText="Hotel Type">
    >> >> > > <ItemTemplate>
    >> >> > > <asp:placeHolder ID="plc" Runat="server" />
    >> >> > > </ItemTemplate>
    >> >> > ></asp:TemplateColumn>
    >> >> > >
    >> >> > >I'm not sure if you are using c# or vb.net, and

    >> what you
    >> >> > are binding too,
    >> >> > >but assuming you are in vb.net and binding to a
    >> >> > dataset/table, your
    >> >> > >ItemDatabound event should look something like:
    >> >> > >
    >> >> > > Private Sub Grid_ItemDataBound(ByVal sender As
    >> >> > Object, ByVal e As
    >> >> > >DataGridItemEventArgs) Handles grid.ItemDataBound
    >> >> > > If e.Item.ItemType = ListItemType.Item

    OrElse
    >> >> > e.Item.ItemType =
    >> >> > >ListItemType.AlternatingItem Then
    >> >> > > Dim plc As PlaceHolder = CType

    >> (e.Item.FindControl
    >> >> > ("plc"), PlaceHolder)
    >> >> > > If Not plc Is Nothing Then
    >> >> > > Dim dr As DataRowView = CType
    >> >> > (e.Item.DataItem, DataRowView)
    >> >> > > Select Case CInt(dr("type"))
    >> >> > > Case 1
    >> >> > > Dim txt As New TextBox
    >> >> > > txt.Text = CStr(dr("value"))
    >> >> > > plc.Controls.Add(txt)
    >> >> > > Case 2
    >> >> > > Dim ddl As New DropDownList
    >> >> > > plc.Controls.Add(ddl)
    >> >> > > End Select
    >> >> > > End If
    >> >> > > End If
    >> >> > > End Sub
    >> >> > >
    >> >> > >As you can see, we try to find the placeholder

    >> contorl,
    >> >> > if it exists, we get
    >> >> > >a reference to the current row we are using, we

    can
    >> use
    >> >> > the values in the
    >> >> > >row (here, I used a mythical "Type" column) to

    >> figure
    >> >> > out what type of
    >> >> > >control goes into the placeholder.
    >> >> > >
    >> >> > >Karl
    >> >> > >
    >> >> > >"Julia Hu" <> wrote in message
    >> >> > >news:a8cd01c48786$4044e1c0$...
    >> >> > >> Hi,
    >> >> > >> I have a datagrid, and in different rows I

    need to
    >> >> > >> programmatically bind different type of

    controls
    >> and
    >> >> > load
    >> >> > >> data into these controls. For example,in the

    >> first row
    >> >> > I
    >> >> > >> need to bind data into a textbox, and in the

    >> second
    >> >> > row I
    >> >> > >> need to bind data into a dropdownlist...It all

    >> depends
    >> >> > on
    >> >> > >> the data I select from the database.
    >> >> > >>
    >> >> > >> I cannot use TemplateColumn because it has to

    be
    >> the
    >> >> > same
    >> >> > >> type of control for one column.
    >> >> > >>
    >> >> > >> Thanks a lot,
    >> >> > >>
    >> >> > >> Julia
    >> >> > >
    >> >> > >
    >> >> > >.
    >> >> > >
    >> >>
    >> >>
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    Julia Hu, Aug 23, 2004
    #13
  14. Julia Hu

    Karl Guest

    You can't directly....this gets uglier, doesn't it? If you rebuild you can,
    if you don't you just have access to the value...although from the value you
    can obvious get the text either by hitting your database or cache..

    Karl

    "Julia Hu" <> wrote in message
    news:bc4f01c4893e$7bbe1910$...
    > I tried both ClientID and UniqueID and the UniqueID
    > works!! I got the SelectedValue, and how can I get the
    > SelectedText??
    >
    > >-----Original Message-----
    > >If I reload controls again in postback, the selected

    > item
    > >of the dropdownlist will be lost. How do I keep it?
    > >
    > >
    > >>-----Original Message-----
    > >>As you told me, I loaded controls in ItemDataBound

    > >event,
    > >>and how can I re-add them?
    > >>code like this:
    > >> Private Sub grdAI_ItemDataBound(ByVal sender As

    > >Object,
    > >>ByVal e As
    > >>System.Web.UI.WebControls.DataGridItemEventArgs)

    > Handles
    > >>grdAI.ItemDataBound
    > >> Dim i As Integer
    > >> Dim plcAI As PlaceHolder = CType
    > >>(e.Item.FindControl("plcAI"), PlaceHolder)
    > >>
    > >> If Not plcAI Is Nothing Then
    > >> Dim dr As DataRowView = CType
    > >>(e.Item.DataItem, DataRowView)
    > >> Select Case CInt(dr("FieldType"))
    > >> Case 6 'ListView type
    > >> Dim ddlAI As New DropDownList
    > >> ddlAI.ID = "ddlAI"
    > >>
    > >> ddlAI.Items.Add("Big")
    > >> ddlAI.Items.Add("Small")
    > >>
    > >> plcAI.Controls.Add(ddlAI)
    > >>
    > >>
    > >> Case Else
    > >> Dim txt As New TextBox
    > >> txt.Text = ""
    > >> plcAI.Controls.Add(txt)
    > >> End Select
    > >> End If
    > >>
    > >> End Sub
    > >>
    > >>>-----Original Message-----
    > >>>On postback you'll need to re-add the controls
    > >>>
    > >>>Karl
    > >>>
    > >>>"Karl" <none> wrote in message
    > >>news:...
    > >>>> What's null, plcAI or ddl?
    > >>>>
    > >>>> Karl
    > >>>>
    > >>>> "Julia Hu" <> wrote in message
    > >>>> news:ba9401c48927$777a4c50$...
    > >>>> > Karl,
    > >>>> > The code works. And now I need to retrieve the

    > data
    > >>in
    > >>>> > the dropdownlist which embeded in the placeholder
    > >>>> > control. The following is my code does this,

    > >however
    > >>it
    > >>>> > returns nothing.
    > >>>> >
    > >>>> > plcAI = CType(grid.Items(i).Cells(1).FindControl
    > >>>> > ("plcAI"), PlaceHolder)
    > >>>> > Dim ddl As DropDownList = CType(plcAI.FindControl
    > >>>> > ("ddlAI"), DropDownList)
    > >>>> >
    > >>>> > thanks again
    > >>>> >
    > >>>> > >-----Original Message-----
    > >>>> > >Julia,
    > >>>> > >Try something like this:
    > >>>> > >
    > >>>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    > >>>> > > <ItemTemplate>
    > >>>> > > <asp:placeHolder ID="plc" Runat="server" />
    > >>>> > > </ItemTemplate>
    > >>>> > ></asp:TemplateColumn>
    > >>>> > >
    > >>>> > >I'm not sure if you are using c# or vb.net, and
    > >>what you
    > >>>> > are binding too,
    > >>>> > >but assuming you are in vb.net and binding to a
    > >>>> > dataset/table, your
    > >>>> > >ItemDatabound event should look something like:
    > >>>> > >
    > >>>> > > Private Sub Grid_ItemDataBound(ByVal sender As
    > >>>> > Object, ByVal e As
    > >>>> > >DataGridItemEventArgs) Handles grid.ItemDataBound
    > >>>> > > If e.Item.ItemType = ListItemType.Item

    > OrElse
    > >>>> > e.Item.ItemType =
    > >>>> > >ListItemType.AlternatingItem Then
    > >>>> > > Dim plc As PlaceHolder = CType
    > >>(e.Item.FindControl
    > >>>> > ("plc"), PlaceHolder)
    > >>>> > > If Not plc Is Nothing Then
    > >>>> > > Dim dr As DataRowView = CType
    > >>>> > (e.Item.DataItem, DataRowView)
    > >>>> > > Select Case CInt(dr("type"))
    > >>>> > > Case 1
    > >>>> > > Dim txt As New TextBox
    > >>>> > > txt.Text = CStr(dr("value"))
    > >>>> > > plc.Controls.Add(txt)
    > >>>> > > Case 2
    > >>>> > > Dim ddl As New DropDownList
    > >>>> > > plc.Controls.Add(ddl)
    > >>>> > > End Select
    > >>>> > > End If
    > >>>> > > End If
    > >>>> > > End Sub
    > >>>> > >
    > >>>> > >As you can see, we try to find the placeholder
    > >>contorl,
    > >>>> > if it exists, we get
    > >>>> > >a reference to the current row we are using, we

    > >can
    > >>use
    > >>>> > the values in the
    > >>>> > >row (here, I used a mythical "Type" column) to
    > >>figure
    > >>>> > out what type of
    > >>>> > >control goes into the placeholder.
    > >>>> > >
    > >>>> > >Karl
    > >>>> > >
    > >>>> > >"Julia Hu" <> wrote in message
    > >>>> > >news:a8cd01c48786$4044e1c0$...
    > >>>> > >> Hi,
    > >>>> > >> I have a datagrid, and in different rows I need

    > >to
    > >>>> > >> programmatically bind different type of

    > controls
    > >>and
    > >>>> > load
    > >>>> > >> data into these controls. For example,in the
    > >>first row
    > >>>> > I
    > >>>> > >> need to bind data into a textbox, and in the
    > >>second
    > >>>> > row I
    > >>>> > >> need to bind data into a dropdownlist...It all
    > >>depends
    > >>>> > on
    > >>>> > >> the data I select from the database.
    > >>>> > >>
    > >>>> > >> I cannot use TemplateColumn because it has to

    > be
    > >>the
    > >>>> > same
    > >>>> > >> type of control for one column.
    > >>>> > >>
    > >>>> > >> Thanks a lot,
    > >>>> > >>
    > >>>> > >> Julia
    > >>>> > >
    > >>>> > >
    > >>>> > >.
    > >>>> > >
    > >>>>
    > >>>>
    > >>>
    > >>>
    > >>>.
    > >>>
    > >>.
    > >>

    > >.
    > >
     
    Karl, Aug 23, 2004
    #14
  15. Julia Hu

    Julia Hu Guest

    Yes,just one more database call.
    I still have 2 questions:
    First,the UniqueID will never change if I keep the same
    columns structure in the datagrid?
    Second,if I use your 2nd solution to reload data,can I
    put in Page_PreRender instead of Page_Load? And can I
    still use the FindControl method to find "plc" and "ddl"
    like this
    plcAI = CType(grid.Items(i).Cells(1).FindControl
    ("plcAI"), PlaceHolder)
    Dim ddl As DropDownList = CType(plcAI.FindControl
    ("ddlAI"), DropDownList) ??

    thank you very much


    >-----Original Message-----
    >You can't directly....this gets uglier, doesn't it? If

    you rebuild you can,
    >if you don't you just have access to the

    value...although from the value you
    >can obvious get the text either by hitting your database

    or cache..
    >
    >Karl
    >
    >"Julia Hu" <> wrote

    in message
    >news:bc4f01c4893e$7bbe1910$...
    >> I tried both ClientID and UniqueID and the UniqueID
    >> works!! I got the SelectedValue, and how can I get the
    >> SelectedText??
    >>
    >> >-----Original Message-----
    >> >If I reload controls again in postback, the selected

    >> item
    >> >of the dropdownlist will be lost. How do I keep it?
    >> >
    >> >
    >> >>-----Original Message-----
    >> >>As you told me, I loaded controls in ItemDataBound
    >> >event,
    >> >>and how can I re-add them?
    >> >>code like this:
    >> >> Private Sub grdAI_ItemDataBound(ByVal sender As
    >> >Object,
    >> >>ByVal e As
    >> >>System.Web.UI.WebControls.DataGridItemEventArgs)

    >> Handles
    >> >>grdAI.ItemDataBound
    >> >> Dim i As Integer
    >> >> Dim plcAI As PlaceHolder = CType
    >> >>(e.Item.FindControl("plcAI"), PlaceHolder)
    >> >>
    >> >> If Not plcAI Is Nothing Then
    >> >> Dim dr As DataRowView = CType
    >> >>(e.Item.DataItem, DataRowView)
    >> >> Select Case CInt(dr("FieldType"))
    >> >> Case 6 'ListView type
    >> >> Dim ddlAI As New DropDownList
    >> >> ddlAI.ID = "ddlAI"
    >> >>
    >> >> ddlAI.Items.Add("Big")
    >> >> ddlAI.Items.Add("Small")
    >> >>
    >> >> plcAI.Controls.Add(ddlAI)
    >> >>
    >> >>
    >> >> Case Else
    >> >> Dim txt As New TextBox
    >> >> txt.Text = ""
    >> >> plcAI.Controls.Add(txt)
    >> >> End Select
    >> >> End If
    >> >>
    >> >> End Sub
    >> >>
    >> >>>-----Original Message-----
    >> >>>On postback you'll need to re-add the controls
    >> >>>
    >> >>>Karl
    >> >>>
    >> >>>"Karl" <none> wrote in message
    >> >>news:...
    >> >>>> What's null, plcAI or ddl?
    >> >>>>
    >> >>>> Karl
    >> >>>>
    >> >>>> "Julia Hu" <> wrote in message
    >> >>>> news:ba9401c48927$777a4c50$...
    >> >>>> > Karl,
    >> >>>> > The code works. And now I need to retrieve the

    >> data
    >> >>in
    >> >>>> > the dropdownlist which embeded in the

    placeholder
    >> >>>> > control. The following is my code does this,
    >> >however
    >> >>it
    >> >>>> > returns nothing.
    >> >>>> >
    >> >>>> > plcAI = CType(grid.Items(i).Cells

    (1).FindControl
    >> >>>> > ("plcAI"), PlaceHolder)
    >> >>>> > Dim ddl As DropDownList = CType

    (plcAI.FindControl
    >> >>>> > ("ddlAI"), DropDownList)
    >> >>>> >
    >> >>>> > thanks again
    >> >>>> >
    >> >>>> > >-----Original Message-----
    >> >>>> > >Julia,
    >> >>>> > >Try something like this:
    >> >>>> > >
    >> >>>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    >> >>>> > > <ItemTemplate>
    >> >>>> > > <asp:placeHolder ID="plc"

    Runat="server" />
    >> >>>> > > </ItemTemplate>
    >> >>>> > ></asp:TemplateColumn>
    >> >>>> > >
    >> >>>> > >I'm not sure if you are using c# or vb.net, and
    >> >>what you
    >> >>>> > are binding too,
    >> >>>> > >but assuming you are in vb.net and binding to a
    >> >>>> > dataset/table, your
    >> >>>> > >ItemDatabound event should look something like:
    >> >>>> > >
    >> >>>> > > Private Sub Grid_ItemDataBound(ByVal sender

    As
    >> >>>> > Object, ByVal e As
    >> >>>> > >DataGridItemEventArgs) Handles

    grid.ItemDataBound
    >> >>>> > > If e.Item.ItemType = ListItemType.Item

    >> OrElse
    >> >>>> > e.Item.ItemType =
    >> >>>> > >ListItemType.AlternatingItem Then
    >> >>>> > > Dim plc As PlaceHolder = CType
    >> >>(e.Item.FindControl
    >> >>>> > ("plc"), PlaceHolder)
    >> >>>> > > If Not plc Is Nothing Then
    >> >>>> > > Dim dr As DataRowView = CType
    >> >>>> > (e.Item.DataItem, DataRowView)
    >> >>>> > > Select Case CInt(dr("type"))
    >> >>>> > > Case 1
    >> >>>> > > Dim txt As New TextBox
    >> >>>> > > txt.Text = CStr(dr("value"))
    >> >>>> > > plc.Controls.Add(txt)
    >> >>>> > > Case 2
    >> >>>> > > Dim ddl As New DropDownList
    >> >>>> > > plc.Controls.Add(ddl)
    >> >>>> > > End Select
    >> >>>> > > End If
    >> >>>> > > End If
    >> >>>> > > End Sub
    >> >>>> > >
    >> >>>> > >As you can see, we try to find the placeholder
    >> >>contorl,
    >> >>>> > if it exists, we get
    >> >>>> > >a reference to the current row we are using, we
    >> >can
    >> >>use
    >> >>>> > the values in the
    >> >>>> > >row (here, I used a mythical "Type" column) to
    >> >>figure
    >> >>>> > out what type of
    >> >>>> > >control goes into the placeholder.
    >> >>>> > >
    >> >>>> > >Karl
    >> >>>> > >
    >> >>>> > >"Julia Hu" <> wrote in message
    >> >>>> > >news:a8cd01c48786$4044e1c0$...
    >> >>>> > >> Hi,
    >> >>>> > >> I have a datagrid, and in different rows I

    need
    >> >to
    >> >>>> > >> programmatically bind different type of

    >> controls
    >> >>and
    >> >>>> > load
    >> >>>> > >> data into these controls. For example,in the
    >> >>first row
    >> >>>> > I
    >> >>>> > >> need to bind data into a textbox, and in the
    >> >>second
    >> >>>> > row I
    >> >>>> > >> need to bind data into a dropdownlist...It

    all
    >> >>depends
    >> >>>> > on
    >> >>>> > >> the data I select from the database.
    >> >>>> > >>
    >> >>>> > >> I cannot use TemplateColumn because it has to

    >> be
    >> >>the
    >> >>>> > same
    >> >>>> > >> type of control for one column.
    >> >>>> > >>
    >> >>>> > >> Thanks a lot,
    >> >>>> > >>
    >> >>>> > >> Julia
    >> >>>> > >
    >> >>>> > >
    >> >>>> > >.
    >> >>>> > >
    >> >>>>
    >> >>>>
    >> >>>
    >> >>>
    >> >>>.
    >> >>>
    >> >>.
    >> >>
    >> >.
    >> >

    >
    >
    >.
    >
     
    Julia Hu, Aug 23, 2004
    #15
  16. Julia Hu

    Karl Guest

    it isn't an extra call if you cache the data...which is obviously something
    you should consider at this point.

    I believe that the id's won't change if the structure is the same, and you
    specify the same ID for the ones you have control of.

    Page_PreRender I believe so, but remember that your placeholder won't exist
    until AFTER you bind the data to the grid. So before you do anything in
    page_load you need your grid to be bound to it's source. Unless you
    absolutely need the UI re-constructed after you post, I'd really recommend
    you try and get away with the Request.Form. You might also have luck
    searching for similar solution *dynamically added controls* or something.

    Finally, if you post again to this newsgroup in a new thread, I won't reply,
    so you might get the input of someone else and find a better answer (I say I
    won't reply 'cuz if the other helpers are like me, they rarely reply if they
    see that a post has a reply). Of course we can keep this thread going :)

    Karl

    "Julia Hu" <> wrote in message
    news:b87601c48945$8fef4d30$...
    > Yes,just one more database call.
    > I still have 2 questions:
    > First,the UniqueID will never change if I keep the same
    > columns structure in the datagrid?
    > Second,if I use your 2nd solution to reload data,can I
    > put in Page_PreRender instead of Page_Load? And can I
    > still use the FindControl method to find "plc" and "ddl"
    > like this
    > plcAI = CType(grid.Items(i).Cells(1).FindControl
    > ("plcAI"), PlaceHolder)
    > Dim ddl As DropDownList = CType(plcAI.FindControl
    > ("ddlAI"), DropDownList) ??
    >
    > thank you very much
    >
    >
    > >-----Original Message-----
    > >You can't directly....this gets uglier, doesn't it? If

    > you rebuild you can,
    > >if you don't you just have access to the

    > value...although from the value you
    > >can obvious get the text either by hitting your database

    > or cache..
    > >
    > >Karl
    > >
    > >"Julia Hu" <> wrote

    > in message
    > >news:bc4f01c4893e$7bbe1910$...
    > >> I tried both ClientID and UniqueID and the UniqueID
    > >> works!! I got the SelectedValue, and how can I get the
    > >> SelectedText??
    > >>
    > >> >-----Original Message-----
    > >> >If I reload controls again in postback, the selected
    > >> item
    > >> >of the dropdownlist will be lost. How do I keep it?
    > >> >
    > >> >
    > >> >>-----Original Message-----
    > >> >>As you told me, I loaded controls in ItemDataBound
    > >> >event,
    > >> >>and how can I re-add them?
    > >> >>code like this:
    > >> >> Private Sub grdAI_ItemDataBound(ByVal sender As
    > >> >Object,
    > >> >>ByVal e As
    > >> >>System.Web.UI.WebControls.DataGridItemEventArgs)
    > >> Handles
    > >> >>grdAI.ItemDataBound
    > >> >> Dim i As Integer
    > >> >> Dim plcAI As PlaceHolder = CType
    > >> >>(e.Item.FindControl("plcAI"), PlaceHolder)
    > >> >>
    > >> >> If Not plcAI Is Nothing Then
    > >> >> Dim dr As DataRowView = CType
    > >> >>(e.Item.DataItem, DataRowView)
    > >> >> Select Case CInt(dr("FieldType"))
    > >> >> Case 6 'ListView type
    > >> >> Dim ddlAI As New DropDownList
    > >> >> ddlAI.ID = "ddlAI"
    > >> >>
    > >> >> ddlAI.Items.Add("Big")
    > >> >> ddlAI.Items.Add("Small")
    > >> >>
    > >> >> plcAI.Controls.Add(ddlAI)
    > >> >>
    > >> >>
    > >> >> Case Else
    > >> >> Dim txt As New TextBox
    > >> >> txt.Text = ""
    > >> >> plcAI.Controls.Add(txt)
    > >> >> End Select
    > >> >> End If
    > >> >>
    > >> >> End Sub
    > >> >>
    > >> >>>-----Original Message-----
    > >> >>>On postback you'll need to re-add the controls
    > >> >>>
    > >> >>>Karl
    > >> >>>
    > >> >>>"Karl" <none> wrote in message
    > >> >>news:...
    > >> >>>> What's null, plcAI or ddl?
    > >> >>>>
    > >> >>>> Karl
    > >> >>>>
    > >> >>>> "Julia Hu" <> wrote in message
    > >> >>>> news:ba9401c48927$777a4c50$...
    > >> >>>> > Karl,
    > >> >>>> > The code works. And now I need to retrieve the
    > >> data
    > >> >>in
    > >> >>>> > the dropdownlist which embeded in the

    > placeholder
    > >> >>>> > control. The following is my code does this,
    > >> >however
    > >> >>it
    > >> >>>> > returns nothing.
    > >> >>>> >
    > >> >>>> > plcAI = CType(grid.Items(i).Cells

    > (1).FindControl
    > >> >>>> > ("plcAI"), PlaceHolder)
    > >> >>>> > Dim ddl As DropDownList = CType

    > (plcAI.FindControl
    > >> >>>> > ("ddlAI"), DropDownList)
    > >> >>>> >
    > >> >>>> > thanks again
    > >> >>>> >
    > >> >>>> > >-----Original Message-----
    > >> >>>> > >Julia,
    > >> >>>> > >Try something like this:
    > >> >>>> > >
    > >> >>>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    > >> >>>> > > <ItemTemplate>
    > >> >>>> > > <asp:placeHolder ID="plc"

    > Runat="server" />
    > >> >>>> > > </ItemTemplate>
    > >> >>>> > ></asp:TemplateColumn>
    > >> >>>> > >
    > >> >>>> > >I'm not sure if you are using c# or vb.net, and
    > >> >>what you
    > >> >>>> > are binding too,
    > >> >>>> > >but assuming you are in vb.net and binding to a
    > >> >>>> > dataset/table, your
    > >> >>>> > >ItemDatabound event should look something like:
    > >> >>>> > >
    > >> >>>> > > Private Sub Grid_ItemDataBound(ByVal sender

    > As
    > >> >>>> > Object, ByVal e As
    > >> >>>> > >DataGridItemEventArgs) Handles

    > grid.ItemDataBound
    > >> >>>> > > If e.Item.ItemType = ListItemType.Item
    > >> OrElse
    > >> >>>> > e.Item.ItemType =
    > >> >>>> > >ListItemType.AlternatingItem Then
    > >> >>>> > > Dim plc As PlaceHolder = CType
    > >> >>(e.Item.FindControl
    > >> >>>> > ("plc"), PlaceHolder)
    > >> >>>> > > If Not plc Is Nothing Then
    > >> >>>> > > Dim dr As DataRowView = CType
    > >> >>>> > (e.Item.DataItem, DataRowView)
    > >> >>>> > > Select Case CInt(dr("type"))
    > >> >>>> > > Case 1
    > >> >>>> > > Dim txt As New TextBox
    > >> >>>> > > txt.Text = CStr(dr("value"))
    > >> >>>> > > plc.Controls.Add(txt)
    > >> >>>> > > Case 2
    > >> >>>> > > Dim ddl As New DropDownList
    > >> >>>> > > plc.Controls.Add(ddl)
    > >> >>>> > > End Select
    > >> >>>> > > End If
    > >> >>>> > > End If
    > >> >>>> > > End Sub
    > >> >>>> > >
    > >> >>>> > >As you can see, we try to find the placeholder
    > >> >>contorl,
    > >> >>>> > if it exists, we get
    > >> >>>> > >a reference to the current row we are using, we
    > >> >can
    > >> >>use
    > >> >>>> > the values in the
    > >> >>>> > >row (here, I used a mythical "Type" column) to
    > >> >>figure
    > >> >>>> > out what type of
    > >> >>>> > >control goes into the placeholder.
    > >> >>>> > >
    > >> >>>> > >Karl
    > >> >>>> > >
    > >> >>>> > >"Julia Hu" <> wrote in message
    > >> >>>> > >news:a8cd01c48786$4044e1c0$...
    > >> >>>> > >> Hi,
    > >> >>>> > >> I have a datagrid, and in different rows I

    > need
    > >> >to
    > >> >>>> > >> programmatically bind different type of
    > >> controls
    > >> >>and
    > >> >>>> > load
    > >> >>>> > >> data into these controls. For example,in the
    > >> >>first row
    > >> >>>> > I
    > >> >>>> > >> need to bind data into a textbox, and in the
    > >> >>second
    > >> >>>> > row I
    > >> >>>> > >> need to bind data into a dropdownlist...It

    > all
    > >> >>depends
    > >> >>>> > on
    > >> >>>> > >> the data I select from the database.
    > >> >>>> > >>
    > >> >>>> > >> I cannot use TemplateColumn because it has to
    > >> be
    > >> >>the
    > >> >>>> > same
    > >> >>>> > >> type of control for one column.
    > >> >>>> > >>
    > >> >>>> > >> Thanks a lot,
    > >> >>>> > >>
    > >> >>>> > >> Julia
    > >> >>>> > >
    > >> >>>> > >
    > >> >>>> > >.
    > >> >>>> > >
    > >> >>>>
    > >> >>>>
    > >> >>>
    > >> >>>
    > >> >>>.
    > >> >>>
    > >> >>.
    > >> >>
    > >> >.
    > >> >

    > >
    > >
    > >.
    > >
     
    Karl, Aug 23, 2004
    #16
  17. Julia Hu

    Julia Hu Guest

    Thank you very much for your help!
    I probably will have questions about this PlaceHolder
    control in the near future, and I'd like to keep this
    thread going and hope you can give me some advice.
    I guess this is the only way to contact you directly 'cuz
    I don't have your email address.

    Regards,
    Julia

    >-----Original Message-----
    >it isn't an extra call if you cache the data...which is

    obviously something
    >you should consider at this point.
    >
    >I believe that the id's won't change if the structure is

    the same, and you
    >specify the same ID for the ones you have control of.
    >
    >Page_PreRender I believe so, but remember that your

    placeholder won't exist
    >until AFTER you bind the data to the grid. So before

    you do anything in
    >page_load you need your grid to be bound to it's

    source. Unless you
    >absolutely need the UI re-constructed after you post,

    I'd really recommend
    >you try and get away with the Request.Form. You might

    also have luck
    >searching for similar solution *dynamically added

    controls* or something.
    >
    >Finally, if you post again to this newsgroup in a new

    thread, I won't reply,
    >so you might get the input of someone else and find a

    better answer (I say I
    >won't reply 'cuz if the other helpers are like me, they

    rarely reply if they
    >see that a post has a reply). Of course we can keep

    this thread going :)
    >
    >Karl
    >
    >"Julia Hu" <> wrote

    in message
    >news:b87601c48945$8fef4d30$...
    >> Yes,just one more database call.
    >> I still have 2 questions:
    >> First,the UniqueID will never change if I keep the same
    >> columns structure in the datagrid?
    >> Second,if I use your 2nd solution to reload data,can I
    >> put in Page_PreRender instead of Page_Load? And can I
    >> still use the FindControl method to find "plc"

    and "ddl"
    >> like this
    >> plcAI = CType(grid.Items(i).Cells(1).FindControl
    >> ("plcAI"), PlaceHolder)
    >> Dim ddl As DropDownList = CType(plcAI.FindControl
    >> ("ddlAI"), DropDownList) ??
    >>
    >> thank you very much
    >>
    >>
    >> >-----Original Message-----
    >> >You can't directly....this gets uglier, doesn't it?

    If
    >> you rebuild you can,
    >> >if you don't you just have access to the

    >> value...although from the value you
    >> >can obvious get the text either by hitting your

    database
    >> or cache..
    >> >
    >> >Karl
    >> >
    >> >"Julia Hu" <> wrote

    >> in message
    >> >news:bc4f01c4893e$7bbe1910$...
    >> >> I tried both ClientID and UniqueID and the UniqueID
    >> >> works!! I got the SelectedValue, and how can I get

    the
    >> >> SelectedText??
    >> >>
    >> >> >-----Original Message-----
    >> >> >If I reload controls again in postback, the

    selected
    >> >> item
    >> >> >of the dropdownlist will be lost. How do I keep it?
    >> >> >
    >> >> >
    >> >> >>-----Original Message-----
    >> >> >>As you told me, I loaded controls in ItemDataBound
    >> >> >event,
    >> >> >>and how can I re-add them?
    >> >> >>code like this:
    >> >> >> Private Sub grdAI_ItemDataBound(ByVal sender As
    >> >> >Object,
    >> >> >>ByVal e As
    >> >> >>System.Web.UI.WebControls.DataGridItemEventArgs)
    >> >> Handles
    >> >> >>grdAI.ItemDataBound
    >> >> >> Dim i As Integer
    >> >> >> Dim plcAI As PlaceHolder = CType
    >> >> >>(e.Item.FindControl("plcAI"), PlaceHolder)
    >> >> >>
    >> >> >> If Not plcAI Is Nothing Then
    >> >> >> Dim dr As DataRowView = CType
    >> >> >>(e.Item.DataItem, DataRowView)
    >> >> >> Select Case CInt(dr("FieldType"))
    >> >> >> Case 6 'ListView type
    >> >> >> Dim ddlAI As New DropDownList
    >> >> >> ddlAI.ID = "ddlAI"
    >> >> >>
    >> >> >> ddlAI.Items.Add("Big")
    >> >> >> ddlAI.Items.Add("Small")
    >> >> >>
    >> >> >> plcAI.Controls.Add(ddlAI)
    >> >> >>
    >> >> >>
    >> >> >> Case Else
    >> >> >> Dim txt As New TextBox
    >> >> >> txt.Text = ""
    >> >> >> plcAI.Controls.Add(txt)
    >> >> >> End Select
    >> >> >> End If
    >> >> >>
    >> >> >> End Sub
    >> >> >>
    >> >> >>>-----Original Message-----
    >> >> >>>On postback you'll need to re-add the controls
    >> >> >>>
    >> >> >>>Karl
    >> >> >>>
    >> >> >>>"Karl" <none> wrote in message
    >> >> >>news:...
    >> >> >>>> What's null, plcAI or ddl?
    >> >> >>>>
    >> >> >>>> Karl
    >> >> >>>>
    >> >> >>>> "Julia Hu" <> wrote in message
    >> >> >>>> news:ba9401c48927$777a4c50$...
    >> >> >>>> > Karl,
    >> >> >>>> > The code works. And now I need to retrieve

    the
    >> >> data
    >> >> >>in
    >> >> >>>> > the dropdownlist which embeded in the

    >> placeholder
    >> >> >>>> > control. The following is my code does this,
    >> >> >however
    >> >> >>it
    >> >> >>>> > returns nothing.
    >> >> >>>> >
    >> >> >>>> > plcAI = CType(grid.Items(i).Cells

    >> (1).FindControl
    >> >> >>>> > ("plcAI"), PlaceHolder)
    >> >> >>>> > Dim ddl As DropDownList = CType

    >> (plcAI.FindControl
    >> >> >>>> > ("ddlAI"), DropDownList)
    >> >> >>>> >
    >> >> >>>> > thanks again
    >> >> >>>> >
    >> >> >>>> > >-----Original Message-----
    >> >> >>>> > >Julia,
    >> >> >>>> > >Try something like this:
    >> >> >>>> > >
    >> >> >>>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    >> >> >>>> > > <ItemTemplate>
    >> >> >>>> > > <asp:placeHolder ID="plc"

    >> Runat="server" />
    >> >> >>>> > > </ItemTemplate>
    >> >> >>>> > ></asp:TemplateColumn>
    >> >> >>>> > >
    >> >> >>>> > >I'm not sure if you are using c# or vb.net,

    and
    >> >> >>what you
    >> >> >>>> > are binding too,
    >> >> >>>> > >but assuming you are in vb.net and binding

    to a
    >> >> >>>> > dataset/table, your
    >> >> >>>> > >ItemDatabound event should look something

    like:
    >> >> >>>> > >
    >> >> >>>> > > Private Sub Grid_ItemDataBound(ByVal

    sender
    >> As
    >> >> >>>> > Object, ByVal e As
    >> >> >>>> > >DataGridItemEventArgs) Handles

    >> grid.ItemDataBound
    >> >> >>>> > > If e.Item.ItemType = ListItemType.Item
    >> >> OrElse
    >> >> >>>> > e.Item.ItemType =
    >> >> >>>> > >ListItemType.AlternatingItem Then
    >> >> >>>> > > Dim plc As PlaceHolder = CType
    >> >> >>(e.Item.FindControl
    >> >> >>>> > ("plc"), PlaceHolder)
    >> >> >>>> > > If Not plc Is Nothing Then
    >> >> >>>> > > Dim dr As DataRowView = CType
    >> >> >>>> > (e.Item.DataItem, DataRowView)
    >> >> >>>> > > Select Case CInt(dr("type"))
    >> >> >>>> > > Case 1
    >> >> >>>> > > Dim txt As New TextBox
    >> >> >>>> > > txt.Text = CStr(dr

    ("value"))
    >> >> >>>> > > plc.Controls.Add(txt)
    >> >> >>>> > > Case 2
    >> >> >>>> > > Dim ddl As New

    DropDownList
    >> >> >>>> > > plc.Controls.Add(ddl)
    >> >> >>>> > > End Select
    >> >> >>>> > > End If
    >> >> >>>> > > End If
    >> >> >>>> > > End Sub
    >> >> >>>> > >
    >> >> >>>> > >As you can see, we try to find the

    placeholder
    >> >> >>contorl,
    >> >> >>>> > if it exists, we get
    >> >> >>>> > >a reference to the current row we are

    using, we
    >> >> >can
    >> >> >>use
    >> >> >>>> > the values in the
    >> >> >>>> > >row (here, I used a mythical "Type" column)

    to
    >> >> >>figure
    >> >> >>>> > out what type of
    >> >> >>>> > >control goes into the placeholder.
    >> >> >>>> > >
    >> >> >>>> > >Karl
    >> >> >>>> > >
    >> >> >>>> > >"Julia Hu" <> wrote in message
    >> >> >>>> > >news:a8cd01c48786$4044e1c0

    $...
    >> >> >>>> > >> Hi,
    >> >> >>>> > >> I have a datagrid, and in different rows I

    >> need
    >> >> >to
    >> >> >>>> > >> programmatically bind different type of
    >> >> controls
    >> >> >>and
    >> >> >>>> > load
    >> >> >>>> > >> data into these controls. For example,in

    the
    >> >> >>first row
    >> >> >>>> > I
    >> >> >>>> > >> need to bind data into a textbox, and in

    the
    >> >> >>second
    >> >> >>>> > row I
    >> >> >>>> > >> need to bind data into a dropdownlist...It

    >> all
    >> >> >>depends
    >> >> >>>> > on
    >> >> >>>> > >> the data I select from the database.
    >> >> >>>> > >>
    >> >> >>>> > >> I cannot use TemplateColumn because it

    has to
    >> >> be
    >> >> >>the
    >> >> >>>> > same
    >> >> >>>> > >> type of control for one column.
    >> >> >>>> > >>
    >> >> >>>> > >> Thanks a lot,
    >> >> >>>> > >>
    >> >> >>>> > >> Julia
    >> >> >>>> > >
    >> >> >>>> > >
    >> >> >>>> > >.
    >> >> >>>> > >
    >> >> >>>>
    >> >> >>>>
    >> >> >>>
    >> >> >>>
    >> >> >>>.
    >> >> >>>
    >> >> >>.
    >> >> >>
    >> >> >.
    >> >> >
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    Julia Hu, Aug 23, 2004
    #17
  18. Julia Hu

    Karl Guest

    if you check the details from this thread my email address is there (with a
    bunch of REMOVEs) 'cuz I'm at home..but it's better to do is on here, might
    help other people :)

    karl

    "Julia Hu" <> wrote in message
    news:bc7c01c48959$675efeb0$...
    > Thank you very much for your help!
    > I probably will have questions about this PlaceHolder
    > control in the near future, and I'd like to keep this
    > thread going and hope you can give me some advice.
    > I guess this is the only way to contact you directly 'cuz
    > I don't have your email address.
    >
    > Regards,
    > Julia
    >
    > >-----Original Message-----
    > >it isn't an extra call if you cache the data...which is

    > obviously something
    > >you should consider at this point.
    > >
    > >I believe that the id's won't change if the structure is

    > the same, and you
    > >specify the same ID for the ones you have control of.
    > >
    > >Page_PreRender I believe so, but remember that your

    > placeholder won't exist
    > >until AFTER you bind the data to the grid. So before

    > you do anything in
    > >page_load you need your grid to be bound to it's

    > source. Unless you
    > >absolutely need the UI re-constructed after you post,

    > I'd really recommend
    > >you try and get away with the Request.Form. You might

    > also have luck
    > >searching for similar solution *dynamically added

    > controls* or something.
    > >
    > >Finally, if you post again to this newsgroup in a new

    > thread, I won't reply,
    > >so you might get the input of someone else and find a

    > better answer (I say I
    > >won't reply 'cuz if the other helpers are like me, they

    > rarely reply if they
    > >see that a post has a reply). Of course we can keep

    > this thread going :)
    > >
    > >Karl
    > >
    > >"Julia Hu" <> wrote

    > in message
    > >news:b87601c48945$8fef4d30$...
    > >> Yes,just one more database call.
    > >> I still have 2 questions:
    > >> First,the UniqueID will never change if I keep the same
    > >> columns structure in the datagrid?
    > >> Second,if I use your 2nd solution to reload data,can I
    > >> put in Page_PreRender instead of Page_Load? And can I
    > >> still use the FindControl method to find "plc"

    > and "ddl"
    > >> like this
    > >> plcAI = CType(grid.Items(i).Cells(1).FindControl
    > >> ("plcAI"), PlaceHolder)
    > >> Dim ddl As DropDownList = CType(plcAI.FindControl
    > >> ("ddlAI"), DropDownList) ??
    > >>
    > >> thank you very much
    > >>
    > >>
    > >> >-----Original Message-----
    > >> >You can't directly....this gets uglier, doesn't it?

    > If
    > >> you rebuild you can,
    > >> >if you don't you just have access to the
    > >> value...although from the value you
    > >> >can obvious get the text either by hitting your

    > database
    > >> or cache..
    > >> >
    > >> >Karl
    > >> >
    > >> >"Julia Hu" <> wrote
    > >> in message
    > >> >news:bc4f01c4893e$7bbe1910$...
    > >> >> I tried both ClientID and UniqueID and the UniqueID
    > >> >> works!! I got the SelectedValue, and how can I get

    > the
    > >> >> SelectedText??
    > >> >>
    > >> >> >-----Original Message-----
    > >> >> >If I reload controls again in postback, the

    > selected
    > >> >> item
    > >> >> >of the dropdownlist will be lost. How do I keep it?
    > >> >> >
    > >> >> >
    > >> >> >>-----Original Message-----
    > >> >> >>As you told me, I loaded controls in ItemDataBound
    > >> >> >event,
    > >> >> >>and how can I re-add them?
    > >> >> >>code like this:
    > >> >> >> Private Sub grdAI_ItemDataBound(ByVal sender As
    > >> >> >Object,
    > >> >> >>ByVal e As
    > >> >> >>System.Web.UI.WebControls.DataGridItemEventArgs)
    > >> >> Handles
    > >> >> >>grdAI.ItemDataBound
    > >> >> >> Dim i As Integer
    > >> >> >> Dim plcAI As PlaceHolder = CType
    > >> >> >>(e.Item.FindControl("plcAI"), PlaceHolder)
    > >> >> >>
    > >> >> >> If Not plcAI Is Nothing Then
    > >> >> >> Dim dr As DataRowView = CType
    > >> >> >>(e.Item.DataItem, DataRowView)
    > >> >> >> Select Case CInt(dr("FieldType"))
    > >> >> >> Case 6 'ListView type
    > >> >> >> Dim ddlAI As New DropDownList
    > >> >> >> ddlAI.ID = "ddlAI"
    > >> >> >>
    > >> >> >> ddlAI.Items.Add("Big")
    > >> >> >> ddlAI.Items.Add("Small")
    > >> >> >>
    > >> >> >> plcAI.Controls.Add(ddlAI)
    > >> >> >>
    > >> >> >>
    > >> >> >> Case Else
    > >> >> >> Dim txt As New TextBox
    > >> >> >> txt.Text = ""
    > >> >> >> plcAI.Controls.Add(txt)
    > >> >> >> End Select
    > >> >> >> End If
    > >> >> >>
    > >> >> >> End Sub
    > >> >> >>
    > >> >> >>>-----Original Message-----
    > >> >> >>>On postback you'll need to re-add the controls
    > >> >> >>>
    > >> >> >>>Karl
    > >> >> >>>
    > >> >> >>>"Karl" <none> wrote in message
    > >> >> >>news:...
    > >> >> >>>> What's null, plcAI or ddl?
    > >> >> >>>>
    > >> >> >>>> Karl
    > >> >> >>>>
    > >> >> >>>> "Julia Hu" <> wrote in message
    > >> >> >>>> news:ba9401c48927$777a4c50$...
    > >> >> >>>> > Karl,
    > >> >> >>>> > The code works. And now I need to retrieve

    > the
    > >> >> data
    > >> >> >>in
    > >> >> >>>> > the dropdownlist which embeded in the
    > >> placeholder
    > >> >> >>>> > control. The following is my code does this,
    > >> >> >however
    > >> >> >>it
    > >> >> >>>> > returns nothing.
    > >> >> >>>> >
    > >> >> >>>> > plcAI = CType(grid.Items(i).Cells
    > >> (1).FindControl
    > >> >> >>>> > ("plcAI"), PlaceHolder)
    > >> >> >>>> > Dim ddl As DropDownList = CType
    > >> (plcAI.FindControl
    > >> >> >>>> > ("ddlAI"), DropDownList)
    > >> >> >>>> >
    > >> >> >>>> > thanks again
    > >> >> >>>> >
    > >> >> >>>> > >-----Original Message-----
    > >> >> >>>> > >Julia,
    > >> >> >>>> > >Try something like this:
    > >> >> >>>> > >
    > >> >> >>>> > ><asp:TemplateColumn HeaderText="Hotel Type">
    > >> >> >>>> > > <ItemTemplate>
    > >> >> >>>> > > <asp:placeHolder ID="plc"
    > >> Runat="server" />
    > >> >> >>>> > > </ItemTemplate>
    > >> >> >>>> > ></asp:TemplateColumn>
    > >> >> >>>> > >
    > >> >> >>>> > >I'm not sure if you are using c# or vb.net,

    > and
    > >> >> >>what you
    > >> >> >>>> > are binding too,
    > >> >> >>>> > >but assuming you are in vb.net and binding

    > to a
    > >> >> >>>> > dataset/table, your
    > >> >> >>>> > >ItemDatabound event should look something

    > like:
    > >> >> >>>> > >
    > >> >> >>>> > > Private Sub Grid_ItemDataBound(ByVal

    > sender
    > >> As
    > >> >> >>>> > Object, ByVal e As
    > >> >> >>>> > >DataGridItemEventArgs) Handles
    > >> grid.ItemDataBound
    > >> >> >>>> > > If e.Item.ItemType = ListItemType.Item
    > >> >> OrElse
    > >> >> >>>> > e.Item.ItemType =
    > >> >> >>>> > >ListItemType.AlternatingItem Then
    > >> >> >>>> > > Dim plc As PlaceHolder = CType
    > >> >> >>(e.Item.FindControl
    > >> >> >>>> > ("plc"), PlaceHolder)
    > >> >> >>>> > > If Not plc Is Nothing Then
    > >> >> >>>> > > Dim dr As DataRowView = CType
    > >> >> >>>> > (e.Item.DataItem, DataRowView)
    > >> >> >>>> > > Select Case CInt(dr("type"))
    > >> >> >>>> > > Case 1
    > >> >> >>>> > > Dim txt As New TextBox
    > >> >> >>>> > > txt.Text = CStr(dr

    > ("value"))
    > >> >> >>>> > > plc.Controls.Add(txt)
    > >> >> >>>> > > Case 2
    > >> >> >>>> > > Dim ddl As New

    > DropDownList
    > >> >> >>>> > > plc.Controls.Add(ddl)
    > >> >> >>>> > > End Select
    > >> >> >>>> > > End If
    > >> >> >>>> > > End If
    > >> >> >>>> > > End Sub
    > >> >> >>>> > >
    > >> >> >>>> > >As you can see, we try to find the

    > placeholder
    > >> >> >>contorl,
    > >> >> >>>> > if it exists, we get
    > >> >> >>>> > >a reference to the current row we are

    > using, we
    > >> >> >can
    > >> >> >>use
    > >> >> >>>> > the values in the
    > >> >> >>>> > >row (here, I used a mythical "Type" column)

    > to
    > >> >> >>figure
    > >> >> >>>> > out what type of
    > >> >> >>>> > >control goes into the placeholder.
    > >> >> >>>> > >
    > >> >> >>>> > >Karl
    > >> >> >>>> > >
    > >> >> >>>> > >"Julia Hu" <> wrote in message
    > >> >> >>>> > >news:a8cd01c48786$4044e1c0

    > $...
    > >> >> >>>> > >> Hi,
    > >> >> >>>> > >> I have a datagrid, and in different rows I
    > >> need
    > >> >> >to
    > >> >> >>>> > >> programmatically bind different type of
    > >> >> controls
    > >> >> >>and
    > >> >> >>>> > load
    > >> >> >>>> > >> data into these controls. For example,in

    > the
    > >> >> >>first row
    > >> >> >>>> > I
    > >> >> >>>> > >> need to bind data into a textbox, and in

    > the
    > >> >> >>second
    > >> >> >>>> > row I
    > >> >> >>>> > >> need to bind data into a dropdownlist...It
    > >> all
    > >> >> >>depends
    > >> >> >>>> > on
    > >> >> >>>> > >> the data I select from the database.
    > >> >> >>>> > >>
    > >> >> >>>> > >> I cannot use TemplateColumn because it

    > has to
    > >> >> be
    > >> >> >>the
    > >> >> >>>> > same
    > >> >> >>>> > >> type of control for one column.
    > >> >> >>>> > >>
    > >> >> >>>> > >> Thanks a lot,
    > >> >> >>>> > >>
    > >> >> >>>> > >> Julia
    > >> >> >>>> > >
    > >> >> >>>> > >
    > >> >> >>>> > >.
    > >> >> >>>> > >
    > >> >> >>>>
    > >> >> >>>>
    > >> >> >>>
    > >> >> >>>
    > >> >> >>>.
    > >> >> >>>
    > >> >> >>.
    > >> >> >>
    > >> >> >.
    > >> >> >
    > >> >
    > >> >
    > >> >.
    > >> >

    > >
    > >
    > >.
    > >
     
    Karl, Aug 23, 2004
    #18
  19. Julia Hu

    Julia Hu Guest

    As you told me,the UniqueID is composed of grid ID,row ID
    and ddl ID,but why the row ID is always the actual row +
    1? The grid doesn't have a header. For example,the ddl in
    the first row of my grid is:
    grdAiEditGrid:grdAI:_ctl2:ddlAI

    thanks

    >-----Original Message-----
    >if you check the details from this thread my email

    address is there (with a
    >bunch of REMOVEs) 'cuz I'm at home..but it's better to

    do is on here, might
    >help other people :)
    >
    >karl
    >
    >"Julia Hu" <> wrote in message
    >news:bc7c01c48959$675efeb0$...
    >> Thank you very much for your help!
    >> I probably will have questions about this PlaceHolder
    >> control in the near future, and I'd like to keep this
    >> thread going and hope you can give me some advice.
    >> I guess this is the only way to contact you

    directly 'cuz
    >> I don't have your email address.
    >>
    >> Regards,
    >> Julia
    >>
    >> >-----Original Message-----
    >> >it isn't an extra call if you cache the data...which

    is
    >> obviously something
    >> >you should consider at this point.
    >> >
    >> >I believe that the id's won't change if the structure

    is
    >> the same, and you
    >> >specify the same ID for the ones you have control of.
    >> >
    >> >Page_PreRender I believe so, but remember that your

    >> placeholder won't exist
    >> >until AFTER you bind the data to the grid. So before

    >> you do anything in
    >> >page_load you need your grid to be bound to it's

    >> source. Unless you
    >> >absolutely need the UI re-constructed after you post,

    >> I'd really recommend
    >> >you try and get away with the Request.Form. You might

    >> also have luck
    >> >searching for similar solution *dynamically added

    >> controls* or something.
    >> >
    >> >Finally, if you post again to this newsgroup in a new

    >> thread, I won't reply,
    >> >so you might get the input of someone else and find a

    >> better answer (I say I
    >> >won't reply 'cuz if the other helpers are like me,

    they
    >> rarely reply if they
    >> >see that a post has a reply). Of course we can keep

    >> this thread going :)
    >> >
    >> >Karl
    >> >
    >> >"Julia Hu" <> wrote

    >> in message
    >> >news:b87601c48945$8fef4d30$...
    >> >> Yes,just one more database call.
    >> >> I still have 2 questions:
    >> >> First,the UniqueID will never change if I keep the

    same
    >> >> columns structure in the datagrid?
    >> >> Second,if I use your 2nd solution to reload

    data,can I
    >> >> put in Page_PreRender instead of Page_Load? And can

    I
    >> >> still use the FindControl method to find "plc"

    >> and "ddl"
    >> >> like this
    >> >> plcAI = CType(grid.Items(i).Cells(1).FindControl
    >> >> ("plcAI"), PlaceHolder)
    >> >> Dim ddl As DropDownList = CType(plcAI.FindControl
    >> >> ("ddlAI"), DropDownList) ??
    >> >>
    >> >> thank you very much
    >> >>
    >> >>
    >> >> >-----Original Message-----
    >> >> >You can't directly....this gets uglier, doesn't it?

    >> If
    >> >> you rebuild you can,
    >> >> >if you don't you just have access to the
    >> >> value...although from the value you
    >> >> >can obvious get the text either by hitting your

    >> database
    >> >> or cache..
    >> >> >
    >> >> >Karl
    >> >> >
    >> >> >"Julia Hu" <>

    wrote
    >> >> in message
    >> >> >news:bc4f01c4893e$7bbe1910$...
    >> >> >> I tried both ClientID and UniqueID and the

    UniqueID
    >> >> >> works!! I got the SelectedValue, and how can I

    get
    >> the
    >> >> >> SelectedText??
    >> >> >>
    >> >> >> >-----Original Message-----
    >> >> >> >If I reload controls again in postback, the

    >> selected
    >> >> >> item
    >> >> >> >of the dropdownlist will be lost. How do I keep

    it?
    >> >> >> >
    >> >> >> >
    >> >> >> >>-----Original Message-----
    >> >> >> >>As you told me, I loaded controls in

    ItemDataBound
    >> >> >> >event,
    >> >> >> >>and how can I re-add them?
    >> >> >> >>code like this:
    >> >> >> >> Private Sub grdAI_ItemDataBound(ByVal sender

    As
    >> >> >> >Object,
    >> >> >> >>ByVal e As
    >> >> >>

    >>System.Web.UI.WebControls.DataGridItemEventArgs)
    >> >> >> Handles
    >> >> >> >>grdAI.ItemDataBound
    >> >> >> >> Dim i As Integer
    >> >> >> >> Dim plcAI As PlaceHolder = CType
    >> >> >> >>(e.Item.FindControl("plcAI"), PlaceHolder)
    >> >> >> >>
    >> >> >> >> If Not plcAI Is Nothing Then
    >> >> >> >> Dim dr As DataRowView = CType
    >> >> >> >>(e.Item.DataItem, DataRowView)
    >> >> >> >> Select Case CInt(dr("FieldType"))
    >> >> >> >> Case 6 'ListView type
    >> >> >> >> Dim ddlAI As New

    DropDownList
    >> >> >> >> ddlAI.ID = "ddlAI"
    >> >> >> >>
    >> >> >> >> ddlAI.Items.Add("Big")
    >> >> >> >> ddlAI.Items.Add("Small")
    >> >> >> >>
    >> >> >> >> plcAI.Controls.Add(ddlAI)
    >> >> >> >>
    >> >> >> >>
    >> >> >> >> Case Else
    >> >> >> >> Dim txt As New TextBox
    >> >> >> >> txt.Text = ""
    >> >> >> >> plcAI.Controls.Add(txt)
    >> >> >> >> End Select
    >> >> >> >> End If
    >> >> >> >>
    >> >> >> >> End Sub
    >> >> >> >>
    >> >> >> >>>-----Original Message-----
    >> >> >> >>>On postback you'll need to re-add the controls
    >> >> >> >>>
    >> >> >> >>>Karl
    >> >> >> >>>
    >> >> >> >>>"Karl" <none> wrote in message
    >> >> >> >>news:...
    >> >> >> >>>> What's null, plcAI or ddl?
    >> >> >> >>>>
    >> >> >> >>>> Karl
    >> >> >> >>>>
    >> >> >> >>>> "Julia Hu" <> wrote in message
    >> >> >> >>>> news:ba9401c48927$777a4c50

    $...
    >> >> >> >>>> > Karl,
    >> >> >> >>>> > The code works. And now I need to retrieve

    >> the
    >> >> >> data
    >> >> >> >>in
    >> >> >> >>>> > the dropdownlist which embeded in the
    >> >> placeholder
    >> >> >> >>>> > control. The following is my code does

    this,
    >> >> >> >however
    >> >> >> >>it
    >> >> >> >>>> > returns nothing.
    >> >> >> >>>> >
    >> >> >> >>>> > plcAI = CType(grid.Items(i).Cells
    >> >> (1).FindControl
    >> >> >> >>>> > ("plcAI"), PlaceHolder)
    >> >> >> >>>> > Dim ddl As DropDownList = CType
    >> >> (plcAI.FindControl
    >> >> >> >>>> > ("ddlAI"), DropDownList)
    >> >> >> >>>> >
    >> >> >> >>>> > thanks again
    >> >> >> >>>> >
    >> >> >> >>>> > >-----Original Message-----
    >> >> >> >>>> > >Julia,
    >> >> >> >>>> > >Try something like this:
    >> >> >> >>>> > >
    >> >> >> >>>> > ><asp:TemplateColumn HeaderText="Hotel

    Type">
    >> >> >> >>>> > > <ItemTemplate>
    >> >> >> >>>> > > <asp:placeHolder ID="plc"
    >> >> Runat="server" />
    >> >> >> >>>> > > </ItemTemplate>
    >> >> >> >>>> > ></asp:TemplateColumn>
    >> >> >> >>>> > >
    >> >> >> >>>> > >I'm not sure if you are using c# or

    vb.net,
    >> and
    >> >> >> >>what you
    >> >> >> >>>> > are binding too,
    >> >> >> >>>> > >but assuming you are in vb.net and

    binding
    >> to a
    >> >> >> >>>> > dataset/table, your
    >> >> >> >>>> > >ItemDatabound event should look something

    >> like:
    >> >> >> >>>> > >
    >> >> >> >>>> > > Private Sub Grid_ItemDataBound(ByVal

    >> sender
    >> >> As
    >> >> >> >>>> > Object, ByVal e As
    >> >> >> >>>> > >DataGridItemEventArgs) Handles
    >> >> grid.ItemDataBound
    >> >> >> >>>> > > If e.Item.ItemType =

    ListItemType.Item
    >> >> >> OrElse
    >> >> >> >>>> > e.Item.ItemType =
    >> >> >> >>>> > >ListItemType.AlternatingItem Then
    >> >> >> >>>> > > Dim plc As PlaceHolder = CType
    >> >> >> >>(e.Item.FindControl
    >> >> >> >>>> > ("plc"), PlaceHolder)
    >> >> >> >>>> > > If Not plc Is Nothing Then
    >> >> >> >>>> > > Dim dr As DataRowView = CType
    >> >> >> >>>> > (e.Item.DataItem, DataRowView)
    >> >> >> >>>> > > Select Case CInt(dr("type"))
    >> >> >> >>>> > > Case 1
    >> >> >> >>>> > > Dim txt As New TextBox
    >> >> >> >>>> > > txt.Text = CStr(dr

    >> ("value"))
    >> >> >> >>>> > > plc.Controls.Add(txt)
    >> >> >> >>>> > > Case 2
    >> >> >> >>>> > > Dim ddl As New

    >> DropDownList
    >> >> >> >>>> > > plc.Controls.Add(ddl)
    >> >> >> >>>> > > End Select
    >> >> >> >>>> > > End If
    >> >> >> >>>> > > End If
    >> >> >> >>>> > > End Sub
    >> >> >> >>>> > >
    >> >> >> >>>> > >As you can see, we try to find the

    >> placeholder
    >> >> >> >>contorl,
    >> >> >> >>>> > if it exists, we get
    >> >> >> >>>> > >a reference to the current row we are

    >> using, we
    >> >> >> >can
    >> >> >> >>use
    >> >> >> >>>> > the values in the
    >> >> >> >>>> > >row (here, I used a mythical "Type"

    column)
    >> to
    >> >> >> >>figure
    >> >> >> >>>> > out what type of
    >> >> >> >>>> > >control goes into the placeholder.
    >> >> >> >>>> > >
    >> >> >> >>>> > >Karl
    >> >> >> >>>> > >
    >> >> >> >>>> > >"Julia Hu" <> wrote in

    message
    >> >> >> >>>> > >news:a8cd01c48786$4044e1c0

    >> $...
    >> >> >> >>>> > >> Hi,
    >> >> >> >>>> > >> I have a datagrid, and in different

    rows I
    >> >> need
    >> >> >> >to
    >> >> >> >>>> > >> programmatically bind different type of
    >> >> >> controls
    >> >> >> >>and
    >> >> >> >>>> > load
    >> >> >> >>>> > >> data into these controls. For

    example,in
    >> the
    >> >> >> >>first row
    >> >> >> >>>> > I
    >> >> >> >>>> > >> need to bind data into a textbox, and

    in
    >> the
    >> >> >> >>second
    >> >> >> >>>> > row I
    >> >> >> >>>> > >> need to bind data into a

    dropdownlist...It
    >> >> all
    >> >> >> >>depends
    >> >> >> >>>> > on
    >> >> >> >>>> > >> the data I select from the database.
    >> >> >> >>>> > >>
    >> >> >> >>>> > >> I cannot use TemplateColumn because it

    >> has to
    >> >> >> be
    >> >> >> >>the
    >> >> >> >>>> > same
    >> >> >> >>>> > >> type of control for one column.
    >> >> >> >>>> > >>
    >> >> >> >>>> > >> Thanks a lot,
    >> >> >> >>>> > >>
    >> >> >> >>>> > >> Julia
    >> >> >> >>>> > >
    >> >> >> >>>> > >
    >> >> >> >>>> > >.
    >> >> >> >>>> > >
    >> >> >> >>>>
    >> >> >> >>>>
    >> >> >> >>>
    >> >> >> >>>
    >> >> >> >>>.
    >> >> >> >>>
    >> >> >> >>.
    >> >> >> >>
    >> >> >> >.
    >> >> >> >
    >> >> >
    >> >> >
    >> >> >.
    >> >> >
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    Julia Hu, Aug 24, 2004
    #19
    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. amessimon
    Replies:
    3
    Views:
    12,908
    niranjan44
    Jun 2, 2011
  2. Dan
    Replies:
    3
    Views:
    334
  3. Chris
    Replies:
    9
    Views:
    1,660
  4. Julia Hu

    Programmatically load dropdownlist into datagrid

    Julia Hu, Aug 21, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    129
    Julia Hu
    Aug 21, 2004
  5. Philjo

    Programmatically assign dropdownList to a GridView control

    Philjo, Sep 20, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    1,855
    Philjo
    Sep 20, 2006
Loading...

Share This Page