Update Textbox from adjacent dropdown in datagrid custom template

Discussion in 'ASP .Net Web Controls' started by Donald Welker, Aug 9, 2005.

  1. I have datagrid custom column defined in a custom server control. I want the
    user to be able to (optionally) populate a textbox by selecting a value from
    a drop-down list. I can populate the textbox and the drop-down, but how do I
    handle the user's action on either the client or server side to get the
    selected value and put it into the adjacent textbox? When I tried to handle
    on selectedindexchanged the event handler was not called:

    Private Class dgERoutingTemplate
    ' Smart text box column with alias parsing rules
    Implements ITemplate
    Dim CN As String = "TRControls.Routing.dgERoutingTemplate" '
    class name
    Public List1 As TROps.ValidatedReviewers ' set by caller after
    column creation
    Public List2 As TROps.ValidatedReviewers
    Dim tType As ListItemType

    Const tbName As String = "tbEmail"
    Const tbNameNew As String = "tbNewEmail"
    Const colEmail As String = "Email"

    Dim HeaderText As String
    Dim Columns As Integer ' text box column width
    Sub New(ByVal LIT As ListItemType, ByVal HeadTxt As String)
    ' use column name as default header
    tType = LIT ' column type
    HeaderText = HeadTxt ' column header text (only used in
    header types)
    Columns = 25
    End Sub ' New
    Sub New(ByVal LIT As ListItemType, ByVal HeadTxt As String,
    ByVal cols As Integer)
    ' use column name as default header
    tType = LIT ' column type
    HeaderText = HeadTxt ' column header text (only used in
    header types)
    Columns = cols
    End Sub ' New

    Sub InstantiateIn(ByVal Container As Control) Implements
    ITemplate.InstantiateIn
    Select Case tType
    Case ListItemType.Item, ListItemType.AlternatingItem, _
    ListItemType.EditItem, ListItemType.SelectedItem
    ' output text box
    Dim tb As New TextBox()
    tb.ID = tbName
    tb.Text = ""
    tb.Columns = Columns
    AddHandler tb.DataBinding, AddressOf
    dgERoutingTemplateTextBox_DataBinding
    Container.Controls.Add(tb)
    Dim lc As New LiteralControl()
    lc.ID = "lcAlias"
    Container.Controls.Add(lc)
    Dim dl As New DropDownList()
    dl.ID = "dlRevList"
    dl.DataSource = List1
    dl.DataTextField = "DataText"
    dl.DataValueField = "SMTPAddress"
    AddHandler dl.DataBinding, AddressOf
    dgERoutingTemplateDropDown_DataBinding
    ' The following does not fire!
    AddHandler dl.SelectedIndexChanged, AddressOf
    dgERoutingTemplateDropDown_SelectedIndexChanged ' *** DOES NOT FIRE!! ***
    dl.AutoPostBack = True
    Container.Controls.Add(dl)
    Case ListItemType.Footer
    ' output "new" text box
    Dim tb As New TextBox()
    tb.ID = tbNameNew
    tb.Text = "" ' "New" & ColumnName
    tb.Columns = Columns
    Container.Controls.Add(tb)
    Case Else
    ' assume header and output header text
    Dim lc As New LiteralControl("<B>" & HeaderText &
    "</B>")
    Container.Controls.Add(lc)
    End Select ' tType
    End Sub ' InstantiateIn

    Private Sub dgERoutingTemplateTextBox_DataBinding(ByVal sender
    As Object, ByVal e As System.EventArgs)
    ' If an SMTP email address can be constructed then use that
    as the text,
    ' otherwise use username if found, else use empty text box.
    Dim tb As TextBox = CType(sender, TextBox)
    Dim dgi As DataGridItem = CType(tb.NamingContainer,
    DataGridItem)
    Dim lc As LiteralControl =
    CType(tb.Parent.FindControl("lcAlias"), LiteralControl)
    lc.Text = ""
    Dim strSMTP As String = dbString(dgi, "Email")
    Dim strUsername As String = dbString(dgi, "Username")
    tb.Text = ""
    If strSMTP <> "" Then
    tb.Text = strSMTP
    If strUsername <> "" Then lc.Text = " (" & strUsername &
    ")"
    ElseIf strUsername <> "" Then
    tb.Text = strUsername
    End If
    End Sub ' dgERoutingTemplateTextBox_DataBinding
    Private Sub dgERoutingTemplateDropDown_DataBinding(ByVal sender
    As Object, ByVal e As System.EventArgs)
    Dim dl As DropDownList = CType(sender, DropDownList)
    Dim dgi As DataGridItem = CType(dl.NamingContainer,
    DataGridItem)
    Dim sRevType As String
    sRevType = dbString(dgi, "RevType")
    If IsNumeric(sRevType) Then
    Dim RevType As Integer = CInt(sRevType)
    Select Case RevType
    Case 1 ' Branch Head (was TPM)
    dl.DataSource = List2
    Case Else
    dl.DataSource = List1
    End Select ' RevType
    End If ' numeric RevType
    dl.SelectedIndex = -1
    End Sub ' dgERoutingTemplateDropDown_DataBinding
    Private Sub
    dgERoutingTemplateDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal
    e As System.EventArgs)
    ' Locate the adjacent TextBox and set its value
    Dim dl As DropDownList = CType(sender, DropDownList)
    Dim dgi As DataGridItem = CType(dl.NamingContainer,
    DataGridItem)
    Dim Value As String = dl.SelectedItem.ToString
    Dim tb As TextBox = CType(tb.Parent.FindControl(tbName),
    TextBox)
    tb.Text = Value
    End Sub ' dgERoutingTemplateDropDown_SelectedIndexChanged
    Private Function dbString(ByRef dgi As DataGridItem, ByVal
    Column As String) As String
    Dim s As String
    If DataBinder.Eval(dgi.DataItem, Column) Is Nothing _
    OrElse DataBinder.Eval(dgi.DataItem, Column) Is
    System.DBNull.Value Then
    s = ""
    Else
    s = CStr(DataBinder.Eval(dgi.DataItem, Column))
    End If
    dbString = s
    End Function ' dbString

    End Class ' dgERoutingTemplate
    Donald Welker, Aug 9, 2005
    #1
    1. Advertising

  2. I'm no further on this than I was yesterday, but to clarify what I'm looking
    for:
    I think I want the dropdown list to fire a client-side routine that will
    update the textbox. The problem becomes how to locate the correct textbox
    given that it's inside a datagrid that is also inside a custom server control.

    "Donald Welker" wrote:

    > I have datagrid custom column defined in a custom server control. I want the
    > user to be able to (optionally) populate a textbox by selecting a value from
    > a drop-down list. I can populate the textbox and the drop-down, but how do I
    > handle the user's action on either the client or server side to get the
    > selected value and put it into the adjacent textbox? When I tried to handle
    > on selectedindexchanged the event handler was not called:
    >
    > Private Class dgERoutingTemplate
    > ' Smart text box column with alias parsing rules
    > Implements ITemplate
    > Dim CN As String = "TRControls.Routing.dgERoutingTemplate" '
    > class name
    > Public List1 As TROps.ValidatedReviewers ' set by caller after
    > column creation
    > Public List2 As TROps.ValidatedReviewers
    > Dim tType As ListItemType
    >
    > Const tbName As String = "tbEmail"
    > Const tbNameNew As String = "tbNewEmail"
    > Const colEmail As String = "Email"
    >
    > Dim HeaderText As String
    > Dim Columns As Integer ' text box column width
    > Sub New(ByVal LIT As ListItemType, ByVal HeadTxt As String)
    > ' use column name as default header
    > tType = LIT ' column type
    > HeaderText = HeadTxt ' column header text (only used in
    > header types)
    > Columns = 25
    > End Sub ' New
    > Sub New(ByVal LIT As ListItemType, ByVal HeadTxt As String,
    > ByVal cols As Integer)
    > ' use column name as default header
    > tType = LIT ' column type
    > HeaderText = HeadTxt ' column header text (only used in
    > header types)
    > Columns = cols
    > End Sub ' New
    >
    > Sub InstantiateIn(ByVal Container As Control) Implements
    > ITemplate.InstantiateIn
    > Select Case tType
    > Case ListItemType.Item, ListItemType.AlternatingItem, _
    > ListItemType.EditItem, ListItemType.SelectedItem
    > ' output text box
    > Dim tb As New TextBox()
    > tb.ID = tbName
    > tb.Text = ""
    > tb.Columns = Columns
    > AddHandler tb.DataBinding, AddressOf
    > dgERoutingTemplateTextBox_DataBinding
    > Container.Controls.Add(tb)
    > Dim lc As New LiteralControl()
    > lc.ID = "lcAlias"
    > Container.Controls.Add(lc)
    > Dim dl As New DropDownList()
    > dl.ID = "dlRevList"
    > dl.DataSource = List1
    > dl.DataTextField = "DataText"
    > dl.DataValueField = "SMTPAddress"
    > AddHandler dl.DataBinding, AddressOf
    > dgERoutingTemplateDropDown_DataBinding
    > ' The following does not fire!
    > AddHandler dl.SelectedIndexChanged, AddressOf
    > dgERoutingTemplateDropDown_SelectedIndexChanged ' *** DOES NOT FIRE!! ***
    > dl.AutoPostBack = True
    > Container.Controls.Add(dl)
    > Case ListItemType.Footer
    > ' output "new" text box
    > Dim tb As New TextBox()
    > tb.ID = tbNameNew
    > tb.Text = "" ' "New" & ColumnName
    > tb.Columns = Columns
    > Container.Controls.Add(tb)
    > Case Else
    > ' assume header and output header text
    > Dim lc As New LiteralControl("<B>" & HeaderText &
    > "</B>")
    > Container.Controls.Add(lc)
    > End Select ' tType
    > End Sub ' InstantiateIn
    >
    > Private Sub dgERoutingTemplateTextBox_DataBinding(ByVal sender
    > As Object, ByVal e As System.EventArgs)
    > ' If an SMTP email address can be constructed then use that
    > as the text,
    > ' otherwise use username if found, else use empty text box.
    > Dim tb As TextBox = CType(sender, TextBox)
    > Dim dgi As DataGridItem = CType(tb.NamingContainer,
    > DataGridItem)
    > Dim lc As LiteralControl =
    > CType(tb.Parent.FindControl("lcAlias"), LiteralControl)
    > lc.Text = ""
    > Dim strSMTP As String = dbString(dgi, "Email")
    > Dim strUsername As String = dbString(dgi, "Username")
    > tb.Text = ""
    > If strSMTP <> "" Then
    > tb.Text = strSMTP
    > If strUsername <> "" Then lc.Text = " (" & strUsername &
    > ")"
    > ElseIf strUsername <> "" Then
    > tb.Text = strUsername
    > End If
    > End Sub ' dgERoutingTemplateTextBox_DataBinding
    > Private Sub dgERoutingTemplateDropDown_DataBinding(ByVal sender
    > As Object, ByVal e As System.EventArgs)
    > Dim dl As DropDownList = CType(sender, DropDownList)
    > Dim dgi As DataGridItem = CType(dl.NamingContainer,
    > DataGridItem)
    > Dim sRevType As String
    > sRevType = dbString(dgi, "RevType")
    > If IsNumeric(sRevType) Then
    > Dim RevType As Integer = CInt(sRevType)
    > Select Case RevType
    > Case 1 ' Branch Head (was TPM)
    > dl.DataSource = List2
    > Case Else
    > dl.DataSource = List1
    > End Select ' RevType
    > End If ' numeric RevType
    > dl.SelectedIndex = -1
    > End Sub ' dgERoutingTemplateDropDown_DataBinding
    > Private Sub
    > dgERoutingTemplateDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal
    > e As System.EventArgs)
    > ' Locate the adjacent TextBox and set its value
    > Dim dl As DropDownList = CType(sender, DropDownList)
    > Dim dgi As DataGridItem = CType(dl.NamingContainer,
    > DataGridItem)
    > Dim Value As String = dl.SelectedItem.ToString
    > Dim tb As TextBox = CType(tb.Parent.FindControl(tbName),
    > TextBox)
    > tb.Text = Value
    > End Sub ' dgERoutingTemplateDropDown_SelectedIndexChanged
    > Private Function dbString(ByRef dgi As DataGridItem, ByVal
    > Column As String) As String
    > Dim s As String
    > If DataBinder.Eval(dgi.DataItem, Column) Is Nothing _
    > OrElse DataBinder.Eval(dgi.DataItem, Column) Is
    > System.DBNull.Value Then
    > s = ""
    > Else
    > s = CStr(DataBinder.Eval(dgi.DataItem, Column))
    > End If
    > dbString = s
    > End Function ' dbString
    >
    > End Class ' dgERoutingTemplate
    >
    Donald Welker, Aug 10, 2005
    #2
    1. Advertising

  3. RE: Update Textbox from adjacent dropdown in datagrid custom templ

    I've figured out that I need to use the UniqueID to access the textbox, my
    code now looks like this:
    ....
    Private Sub dgNUWCERoutingTemplateDropDown_DataBinding(ByVal
    sender As Object, ByVal e As System.EventArgs)
    Dim dl As DropDownList = CType(sender, DropDownList)
    ....
    TargetBoxID =
    dl.NamingContainer.FindControl(tbName).UniqueID ' control to set using this
    dropdown

    End Sub ' dgNUWCERoutingTemplateDropDown_DataBinding
    ....

    Now, (1) how do I pass the name of the textbox to the dropdownlist, and (2)
    what do I write for javascript to cause picking on the dropdown to update the
    textbox, and (3) how do I emit that code into the dropdown?

    "Donald Welker" wrote:

    > I'm no further on this than I was yesterday, but to clarify what I'm looking
    > for:
    > I think I want the dropdown list to fire a client-side routine that will
    > update the textbox. The problem becomes how to locate the correct textbox
    > given that it's inside a datagrid that is also inside a custom server control.
    >
    > "Donald Welker" wrote:
    >
    > > I have datagrid custom column defined in a custom server control. I want the
    > > user to be able to (optionally) populate a textbox by selecting a value from
    > > a drop-down list. I can populate the textbox and the drop-down, but how do I
    > > handle the user's action on either the client or server side to get the
    > > selected value and put it into the adjacent textbox? When I tried to handle
    > > on selectedindexchanged the event handler was not called:
    > >
    > > Private Class dgERoutingTemplate
    > > ' Smart text box column with alias parsing rules
    > > Implements ITemplate
    > > Dim CN As String = "TRControls.Routing.dgERoutingTemplate" '
    > > class name
    > > Public List1 As TROps.ValidatedReviewers ' set by caller after
    > > column creation
    > > Public List2 As TROps.ValidatedReviewers
    > > Dim tType As ListItemType
    > >
    > > Const tbName As String = "tbEmail"
    > > Const tbNameNew As String = "tbNewEmail"
    > > Const colEmail As String = "Email"
    > >
    > > Dim HeaderText As String
    > > Dim Columns As Integer ' text box column width
    > > Sub New(ByVal LIT As ListItemType, ByVal HeadTxt As String)
    > > ' use column name as default header
    > > tType = LIT ' column type
    > > HeaderText = HeadTxt ' column header text (only used in
    > > header types)
    > > Columns = 25
    > > End Sub ' New
    > > Sub New(ByVal LIT As ListItemType, ByVal HeadTxt As String,
    > > ByVal cols As Integer)
    > > ' use column name as default header
    > > tType = LIT ' column type
    > > HeaderText = HeadTxt ' column header text (only used in
    > > header types)
    > > Columns = cols
    > > End Sub ' New
    > >
    > > Sub InstantiateIn(ByVal Container As Control) Implements
    > > ITemplate.InstantiateIn
    > > Select Case tType
    > > Case ListItemType.Item, ListItemType.AlternatingItem, _
    > > ListItemType.EditItem, ListItemType.SelectedItem
    > > ' output text box
    > > Dim tb As New TextBox()
    > > tb.ID = tbName
    > > tb.Text = ""
    > > tb.Columns = Columns
    > > AddHandler tb.DataBinding, AddressOf
    > > dgERoutingTemplateTextBox_DataBinding
    > > Container.Controls.Add(tb)
    > > Dim lc As New LiteralControl()
    > > lc.ID = "lcAlias"
    > > Container.Controls.Add(lc)
    > > Dim dl As New DropDownList()
    > > dl.ID = "dlRevList"
    > > dl.DataSource = List1
    > > dl.DataTextField = "DataText"
    > > dl.DataValueField = "SMTPAddress"
    > > AddHandler dl.DataBinding, AddressOf
    > > dgERoutingTemplateDropDown_DataBinding
    > > ' The following does not fire!
    > > AddHandler dl.SelectedIndexChanged, AddressOf
    > > dgERoutingTemplateDropDown_SelectedIndexChanged ' *** DOES NOT FIRE!! ***
    > > dl.AutoPostBack = True
    > > Container.Controls.Add(dl)
    > > Case ListItemType.Footer
    > > ' output "new" text box
    > > Dim tb As New TextBox()
    > > tb.ID = tbNameNew
    > > tb.Text = "" ' "New" & ColumnName
    > > tb.Columns = Columns
    > > Container.Controls.Add(tb)
    > > Case Else
    > > ' assume header and output header text
    > > Dim lc As New LiteralControl("<B>" & HeaderText &
    > > "</B>")
    > > Container.Controls.Add(lc)
    > > End Select ' tType
    > > End Sub ' InstantiateIn
    > >
    > > Private Sub dgERoutingTemplateTextBox_DataBinding(ByVal sender
    > > As Object, ByVal e As System.EventArgs)
    > > ' If an SMTP email address can be constructed then use that
    > > as the text,
    > > ' otherwise use username if found, else use empty text box.
    > > Dim tb As TextBox = CType(sender, TextBox)
    > > Dim dgi As DataGridItem = CType(tb.NamingContainer,
    > > DataGridItem)
    > > Dim lc As LiteralControl =
    > > CType(tb.Parent.FindControl("lcAlias"), LiteralControl)
    > > lc.Text = ""
    > > Dim strSMTP As String = dbString(dgi, "Email")
    > > Dim strUsername As String = dbString(dgi, "Username")
    > > tb.Text = ""
    > > If strSMTP <> "" Then
    > > tb.Text = strSMTP
    > > If strUsername <> "" Then lc.Text = " (" & strUsername &
    > > ")"
    > > ElseIf strUsername <> "" Then
    > > tb.Text = strUsername
    > > End If
    > > End Sub ' dgERoutingTemplateTextBox_DataBinding
    > > Private Sub dgERoutingTemplateDropDown_DataBinding(ByVal sender
    > > As Object, ByVal e As System.EventArgs)
    > > Dim dl As DropDownList = CType(sender, DropDownList)
    > > Dim dgi As DataGridItem = CType(dl.NamingContainer,
    > > DataGridItem)
    > > Dim sRevType As String
    > > sRevType = dbString(dgi, "RevType")
    > > If IsNumeric(sRevType) Then
    > > Dim RevType As Integer = CInt(sRevType)
    > > Select Case RevType
    > > Case 1 ' Branch Head (was TPM)
    > > dl.DataSource = List2
    > > Case Else
    > > dl.DataSource = List1
    > > End Select ' RevType
    > > End If ' numeric RevType
    > > dl.SelectedIndex = -1
    > > End Sub ' dgERoutingTemplateDropDown_DataBinding
    > > Private Sub
    > > dgERoutingTemplateDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal
    > > e As System.EventArgs)
    > > ' Locate the adjacent TextBox and set its value
    > > Dim dl As DropDownList = CType(sender, DropDownList)
    > > Dim dgi As DataGridItem = CType(dl.NamingContainer,
    > > DataGridItem)
    > > Dim Value As String = dl.SelectedItem.ToString
    > > Dim tb As TextBox = CType(tb.Parent.FindControl(tbName),
    > > TextBox)
    > > tb.Text = Value
    > > End Sub ' dgERoutingTemplateDropDown_SelectedIndexChanged
    > > Private Function dbString(ByRef dgi As DataGridItem, ByVal
    > > Column As String) As String
    > > Dim s As String
    > > If DataBinder.Eval(dgi.DataItem, Column) Is Nothing _
    > > OrElse DataBinder.Eval(dgi.DataItem, Column) Is
    > > System.DBNull.Value Then
    > > s = ""
    > > Else
    > > s = CStr(DataBinder.Eval(dgi.DataItem, Column))
    > > End If
    > > dbString = s
    > > End Function ' dbString
    > >
    > > End Class ' dgERoutingTemplate
    > >
    Donald Welker, Aug 10, 2005
    #3
    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. vee dub
    Replies:
    4
    Views:
    696
    Chris Smith
    Nov 20, 2004
  2. The Doctor

    Adjacent Tables

    The Doctor, Oct 21, 2006, in forum: HTML
    Replies:
    2
    Views:
    1,717
    Jonathan N. Little
    Oct 21, 2006
  3. Replies:
    2
    Views:
    332
    Keith Thompson
    Mar 15, 2006
  4. Jarkko Kuoppamäki
    Replies:
    11
    Views:
    774
    Jonathan N. Little
    Jan 14, 2008
  5. Greg Pyatt

    Returned Page Won't Update When Using Custom Template & Textbox

    Greg Pyatt, Sep 18, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    112
    Greg Pyatt
    Sep 18, 2003
Loading...

Share This Page