By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,071 Members | 1,747 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,071 IT Pros & Developers. It's quick & easy.

Button & TextBox Custom Control

P: n/a
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.OnClic k,
"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?

Oct 31 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.OnClic k,
"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?

rn**@rediffmail.com wrote:
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.OnClic k,
"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?
Oct 31 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.