471,579 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Custom Control Again

A custom control is derived from the WebControl class & encapsulates a
TextBox & a Button. When the Button is clicked, the user is shown the
JavaScript confirm dialog with the 'OK' & 'Cancel' buttons. If the user
clicks 'OK', the TextBox gets populated with 'true' & if 'Cancel' is
clicked, the TextBox gets populated with 'false'. Note that when the
user first comes to the ASPX page that uses this custom control, the
TextBox is empty. This is the code in the VB class file:

Namespace MyNS
Public Class MyCls : Inherits WebControl
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
Dim txt1 As TextBox
Dim btn1 As Button

txt1 = New TextBox
btn1 = New Button

txt1.ID = "txt1"
Me.Controls.Add(txt1)

btn1.ID = "btn1"
btn1.Text = "CLICK"
btn1.Attributes.Add("OnClick", "ConfirmMsg(); return
false;")
Me.Controls.Add(btn1)

ChildControlsCreated = True
End Sub

Protected Overrides Sub RenderContents(ByVal Output As
HtmlTextWriter)
MyBase.RenderContents(Output)
Output.Write("<script language='JavaScript'>")
Output.Write(vbCrLf)
Output.Write("function ConfirmMsg()")
Output.Write(vbCrLf)
Output.Write("var answer = confirm('Are U Sure?'))
Output.Write(vbCrLf)
Output.Write("document.forms[0].txt1.value = answer")
Output.Write(vbCrLf)
Output.Write("}")
Output.Write(vbCrLf)
Output.Write("</script>")
End Sub
End Class
End Namespace

The above custom control, when used in a ASPX page, does pop-up the
JavaScript confirm dialog & depending upon whether the user has clicked
'OK' or 'Cancel', the TextBox gets populated with 'true' & 'false'
respectively.

The problem I am facing is how do I make the ASPX page get the value of
the TextBox. Any ideas on how do I go about it?

Another problem I am facing is when the ASPX page has validation
controls. If the ASPX page does not have any validation controls, then
when I have a look at the HTML source code of the ASPX page, the source
of the Button control looks like this:

<input type="submit" name="btn1" value="QUIT" onclick="ConfirmMsg();
return false;" id="btn1" />

But if the ASPX page has validation controls, when the Button is
clicked, the JavaScript confirm dialog doesn't pop-up. The source code
of the Button control also shows 2 OnClick events:

<input type="submit" name="btn1" value="QUIT"
onclick="javascript:WebForm_DoPostBackWithOptions( new
WebForm_PostBackOptions(&quot;btn1&quot;, &quot;&quot;, true,
&quot;&quot;, &quot;&quot;, false, false))" id="btn1"
onclick="ConfirmMsg();return false;"/>

How do I make the Button in the custom control work in the norwal way
when the ASPX page has got validation controls?

Nov 4 '06 #1
0 1301

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Tinus | last post: by
21 posts views Thread by One Handed Man \( OHM - Terry Burns \) | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | 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.