Custom Control Again

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Nov 4, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amit
    Replies:
    0
    Views:
    367
  2. che
    Replies:
    2
    Views:
    469
  3. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    312
    Maric Michaud
    Jun 9, 2006
  4. Replies:
    4
    Views:
    386
  5. Matt Sokol
    Replies:
    2
    Views:
    360
    Theo Tillotson
    Aug 7, 2003
Loading...

Share This Page