Javascript Function w/ webcontrol as a pramater

Discussion in 'ASP .Net' started by Jon, Apr 23, 2004.

  1. Jon

    Jon Guest

    I would like to pass a reference of a webcontrol into a javascript
    function. Below I have a snippet of code. "me.selectedValue" returns
    undefined. I was hoping that would return the value that the user has
    selected in the radio list..

    ps: this is my first post, be gentle:)

    **aspx page**

    <form id="Form1" method="post" runat="server">
    <asp:RadioButtonList ID=radio1 Runat=server >
    <asp:ListItem Value=1 >Option 1</asp:ListItem>
    <asp:ListItem Value=2 >Option 2</asp:ListItem>
    <asp:ListItem Value=3 >Option 3</asp:ListItem>
    </asp:RadioButtonList>
    </form>

    **Code Behind**
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim strScript As String = "<script language=""JavaScript"">" &
    vbCrLf
    strScript += " function test1(me) " & vbCrLf
    strScript += "{alert(me.SelectedValue); " & vbCrLf
    strScript += "}</script>"

    If (Not Page.IsClientScriptBlockRegistered("clientScript"))
    Then
    Page.RegisterClientScriptBlock("clientScript", strScript)
    End If
    End Sub

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles MyBase.PreRender
    radio1.Attributes.Add("onclick", "test1(this)")
    End Sub
    Jon, Apr 23, 2004
    #1
    1. Advertising

  2. Jon

    Rick Spiewak Guest

    You have Me.Selected.Value inside quotes. You seem to be passing what you
    would like to have as a variable as a string.

    This might be clearer if you used a StringBuilder:

    Imports System.Text
    ..
    ..
    ..

    Dim sbldr As New StringBuilder
    sbldr.Append("<script language=""JavaScript"">") :
    sbldr.Append(vbCrLf)
    sbldr.Append(" function test1(me) ") : sbldr.Append(vbCrLf)
    sbldr.AppendFormat("{alert({0}); {1}}</script>", Me.SelectedValue,
    vbCrLf)

    "Jon" <> wrote in message
    news:...
    > I would like to pass a reference of a webcontrol into a javascript
    > function. Below I have a snippet of code. "me.selectedValue" returns
    > undefined. I was hoping that would return the value that the user has
    > selected in the radio list..
    >
    > ps: this is my first post, be gentle:)
    >
    > **aspx page**
    >
    > <form id="Form1" method="post" runat="server">
    > <asp:RadioButtonList ID=radio1 Runat=server >
    > <asp:ListItem Value=1 >Option 1</asp:ListItem>
    > <asp:ListItem Value=2 >Option 2</asp:ListItem>
    > <asp:ListItem Value=3 >Option 3</asp:ListItem>
    > </asp:RadioButtonList>
    > </form>
    >
    > **Code Behind**
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > Dim strScript As String = "<script language=""JavaScript"">" &
    > vbCrLf
    > strScript += " function test1(me) " & vbCrLf
    > strScript += "{alert(me.SelectedValue); " & vbCrLf
    > strScript += "}</script>"
    >
    > If (Not Page.IsClientScriptBlockRegistered("clientScript"))
    > Then
    > Page.RegisterClientScriptBlock("clientScript", strScript)
    > End If
    > End Sub
    >
    > Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles MyBase.PreRender
    > radio1.Attributes.Add("onclick", "test1(this)")
    > End Sub
    Rick Spiewak, Apr 23, 2004
    #2
    1. Advertising

  3. Jon

    Jon Turner Guest

    Thank you for your quick response :)

    I am currently working with VB. Is stringBuilder a C# object?

    The javascript is now working. me.ID yields the objects ID and me.Value
    yeilds the value for asp:TextBox and asp:DropDownList.

    How do you get the selected values for the RadioButtonList and
    CheckBoxList controls? Is there an easy way to do this or do you have
    to do some wierd looping through the generated HTML table?



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Jon Turner, Apr 23, 2004
    #3
  4. You can retrieve selected values like this

    String s = "Selected items:<br>"

    for (int i=0; i < CheckBoxList1.Items.Count; i++)

    if ( CheckBoxList1.Items[ i ].Selected )

    // List the selected item
    s = s + CheckBoxList1.Items.Value
    s = s + "<br>"
    }

    Label1my1.Text = s;
    =?Utf-8?B?UGV0ZXI=?=, Apr 23, 2004
    #4
  5. Jon

    Rick Spiewak Guest

    Stringbuilder is a .NET object, you can use it in VB.NET.

    "Jon Turner" <> wrote in message
    news:%...
    > Thank you for your quick response :)
    >
    > I am currently working with VB. Is stringBuilder a C# object?
    >
    > The javascript is now working. me.ID yields the objects ID and me.Value
    > yeilds the value for asp:TextBox and asp:DropDownList.
    >
    > How do you get the selected values for the RadioButtonList and
    > CheckBoxList controls? Is there an easy way to do this or do you have
    > to do some wierd looping through the generated HTML table?
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Rick Spiewak, Apr 23, 2004
    #5
  6. Jon

    Jon Turner Guest

    Thank you Peter. I have tried your example, but can't seem to get it to
    work. Below is a code snippet. I keep getting the error Message
    "'check1.Items.Count' is null or not an object"

    My goal is to get the sum total of the selected values. Then take that
    number and perform a bitwise operation with another number. If returns
    true - make a Div visible Else Hidden.


    <script language="JavaScript">
    function test1()
    {
    for ( i=0; i < check1.Items.Count; i++)
    {
    if (check1.Items.Selected)
    {
    sValue = Svalue + check1.Items.Value;
    }
    }
    alert(sValue);
    }
    </script>

    *****

    <asp:CheckBoxList ID=check1 Runat=server onclick="test1();">
    <asp:ListItem Selected=True Value=1>Option 1</asp:ListItem>
    <asp:ListItem Value=2 >Option 2</asp:ListItem>
    <asp:ListItem Value=4 >Option 3</asp:ListItem>
    </asp:CheckBoxList>


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Jon Turner, Apr 23, 2004
    #6
    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. Guest
    Replies:
    2
    Views:
    725
  2. Gijs Korremans

    return pramater in com object function

    Gijs Korremans, Jun 2, 2005, in forum: Python
    Replies:
    0
    Views:
    280
    Gijs Korremans
    Jun 2, 2005
  3. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    185
    Luis Ramírez
    Jul 6, 2004
  4. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    177
    Kasabaarde Sumta
    Dec 13, 2004
  5. Fabricio

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    171
    Fabricio
    Sep 21, 2005
Loading...

Share This Page