471,580 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,580 software developers and data experts.

Button & TextBox Custom Control

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
1 3007
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.

Similar topics

2 posts views Thread by Joseph | last post: by
2 posts views Thread by dskillingstad | last post: by
2 posts views Thread by melanieab | last post: by
reply views Thread by campwes | last post: by
3 posts views Thread by Jay | last post: by
1 post views Thread by Forumtroll | last post: by
6 posts views Thread by Tom P. | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.