D
Donald Welker
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
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