Button & TextBox Custom Control

R

rn5a

I want to create a custom control that encapsulates a Button & a
TextBox. When the Button is clicked, the user is asked a question using
JavaScript confirm (which shows 2 buttons - 'OK' & 'Cancel'). Till this
point, no problem. Initially, the TextBox is empty. The Button has a
property named 'ConfirmMessage' so that the developer using this custom
control can modify the question in the confirm dialog.

If the user clicks 'OK', I want the text in the TextBox to change to
True. If the user clicks 'Cancel', the text in the TextBox should
change to False. This is where I am getting stuck. First of all, how do
I add a TextBox to the custom control & secondly, how do I change the
text in the TextBox depending upon whether the user has clicked 'OK' or
'Cancel' in the JavaScript confirm dialog.

I have come this far (this code exists in a VB class file):

Namespace Confirm
Public Class ShowConfirm : Inherits Button
Private strConfirmMsg As String
Public WriteOnly Property ConfirmMessage() As String
Set(ByVal value As String)
strConfirmMsg = value
End Set
End Property

Protected Overrides Sub AddAttributesToRender(ByVal Output As
HtmlTextWriter)
MyBase.AddAttributesToRender(Output)
Output.AddAttribute(HtmlTextWriterAttribute.OnClick,
"ConfirmMsg()")
End Sub

Protected Overrides Sub RenderContents(ByVal Output As
HtmlTextWriter)
Output.Write(vbCrLf)
Output.Write("<script language='JavaScript'>")
Output.Write(vbCrLf)
Output.Write("function ConfirmMsg(){")
Output.Write(vbCrLf)
Output.Write(vbTab)
Output.Write("var answer = confirm('")
If (strConfirmMsg <> "") Then
Output.Write(strConfirmMsg)
Else
Output.Write("Are You Sure You Want To Exit?")
End If
Output.Write("')")
Output.Write(vbCrLf)
Output.Write("}")
Output.Write(vbCrLf)
Output.Write("</script>")
End Sub
End Class
End Namespace

Can someone please tell me how do I add a TextBox in the above custom
control & change the text in the TextBox depending upon whether the
user has clicked 'OK' or 'Cancel' in the JavaScript confirm dialog?
 
R

rn5a

Well I have resolved the problem I was facing (as described in post
#1). This is how I did it:

Namespace Confirm
Public Class ShowConfirm : Inherits Button
Private strConfirmMsg As String
Public WriteOnly Property ConfirmMessage() As String
Set(ByVal value As String)
strConfirmMsg = value
End Set
End Property

Protected Overrides Sub AddAttributesToRender(ByVal Output As
HtmlTextWriter)
MyBase.AddAttributesToRender(Output)
Output.AddAttribute(HtmlTextWriterAttribute.OnClick,
"ConfirmMsg()")
End Sub

Protected Overrides Sub RenderContents(ByVal Output As
HtmlTextWriter)
Dim pnlConfirm As Panel
Dim txtConfirm As TextBox

pnlConfirm = New Panel
txtConfirm = New TextBox
txtConfirm.ID = "txtConfirm"
txtConfirm.EnableViewState = True

pnlConfirm.Controls.Add(txtConfirm)
Controls.Add(pnlConfirm)
pnlConfirm.RenderControl(Output)

Output.Write(vbCrLf)
Output.Write("<script language='JavaScript'>")
Output.Write(vbCrLf)
Output.Write("function ConfirmMsg(){")
Output.Write(vbCrLf)
Output.Write(vbTab)
Output.Write("var answer = confirm('")
If (strConfirmMsg <> "") Then
Output.Write(strConfirmMsg)
Else
Output.Write("Are You Sure You Want To Exit?")
End If
Output.Write("')")
Output.Write(vbCrLf)
Output.Write(vbTab)
Output.Write("document.forms[0].txtConfirm.value = answer")
Output.Write(vbCrLf)
Output.Write("}")
Output.Write(vbCrLf)
Output.Write("</script>")
End Sub
End Class
End Namespace

The above custom control changes the text in the TextBox to True &
False when the user clicks 'OK' & 'Cancel' in the JavaScript confirm
dialog respectively. That serves my purpose but I have encountered a
new problem.

As already mentioned, when the user clicks 'OK' or 'Cancel' in the
confirm dialog, the text in the TextBox changes to True & False
respectively but when the page gets posted, the TextBox loses its value
immediately after post back though I have set the 'EnableViewState'
property of the TextBox to true. How do I resolve this problem?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top