Custom Control Again

R

rn5a

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?
 

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

Similar Threads

Button & TextBox Custom Control 1
AddAttribute 1
Custom Control 1
Suppress Form Action 1
Need help again please 19
How does this code step 0
ASPNET 2.0 Page_Load question 5
public variables 5

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top