radiobutton postback change event

Discussion in 'ASP .Net' started by =?Utf-8?B?ZGF2ZQ==?=, Jan 22, 2004.

  1. I have half a dozen web form radio buttons on a web form. Each of them is set to postback=true. However, if for instance radiobutton1 is already selected and the user selects it again, it performs a postback.

    I only want to do a postback if the value of the radiobutton is changed.

    What is the best method to accomplish this?
    thx
    dave
     
    =?Utf-8?B?ZGF2ZQ==?=, Jan 22, 2004
    #1
    1. Advertising

  2. Hi Dave,

    This seems like a bit of a hack, but....

    You could use client-side script to loop through the radio buttons and find
    the one that is checked. When you find it, clear the value from the onclick
    event so the postback won't happen.

    There are probably better ways, but until that solution arrives, this might
    get you going:

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Label1.Text = Now.ToLongTimeString
    Dim sb As New System.Text.StringBuilder
    sb.Append("<script language=javascript>")
    sb.Append("for (var num=0; num < ")
    sb.Append("document.forms[0].elements.length;num++)")
    sb.Append("{")
    sb.Append("if (document.forms[0].elements[num].checked==true)")
    sb.Append("{")
    sb.Append(" document.forms[0].elements[num].onclick='';")
    sb.Append(" break;")
    sb.Append("}")
    sb.Append("}")
    sb.Append("</script>")
    Page.RegisterStartupScript("radchk", sb.ToString)
    End Sub


    <P>
    <asp:RadioButton id="RadioButton1" runat="server" Text="Red"
    GroupName="colours" AutoPostBack="True"></asp:RadioButton>&nbsp;</P>
    <P>
    <asp:RadioButton id="RadioButton2" runat="server" Text="Green"
    GroupName="colours" AutoPostBack="True"
    Checked="True"></asp:RadioButton></P>
    <P>
    <asp:RadioButton id="RadioButton3" runat="server" Text="Blue"
    GroupName="colours" AutoPostBack="True"></asp:RadioButton></P>
    <P>
    <asp:Label id="Label1" runat="server"></asp:Label></P>

    Ken
    MVP [ASP.NET]


    "dave" <> wrote in message
    news:...
    >I have half a dozen web form radio buttons on a web form. Each of them is
    >set to postback=true. However, if for instance radiobutton1 is already
    >selected and the user selects it again, it performs a postback.
    >
    > I only want to do a postback if the value of the radiobutton is changed.
    >
    > What is the best method to accomplish this?
    > thx
    > dave
     
    Ken Cox [Microsoft MVP], Jan 22, 2004
    #2
    1. Advertising

  3. Hi Dave,

    This sparked my curiosity and I think I have a possible solution. Please
    try the following.

    **** HTML
    <script language="javascript">

    function RadioButton_Click() {
    var SourceElement = event.srcElement.value;
    if (SourceElement == undefined)
    {
    //Try Netscape event property
    SourceElement = event.target;
    }
    if (SourceElement == document.all("MyGroupSelectedItem").value)
    {
    return false;
    }
    else
    {
    document.all("MyGroupSelectedItem").value = SourceElement;
    __doPostBack(SourceElement,'');
    return true;
    }
    }

    function Body_OnLoad() {
    for (var num=0; num < document.forms[0].elements["MyGroup"].length;num++)
    {
    document.forms[0].elements["MyGroup"][num].onclick =
    RadioButton_Click;
    if (document.forms[0].elements["MyGroup"][num].checked==true)
    {
    document.all("MyGroupSelectedItem").value =
    document.forms[0].elements["MyGroup"][num].value;
    }
    }
    }
    </script>
    <body onload="Body_OnLoad();">
    <form id="Form1" method="post" runat="server">
    <asp:radiobutton id="RadioButton1" runat="server" AutoPostBack="True"
    GroupName="MyGroup"></asp:radiobutton><br>
    <asp:radiobutton id="RadioButton2" runat="server" AutoPostBack="True"
    GroupName="MyGroup"></asp:radiobutton><br>
    <asp:radiobutton id="RadioButton3" runat="server" AutoPostBack="True"
    GroupName="MyGroup"></asp:radiobutton><br>
    <asp:TextBox id="TextBox1" runat="server" TextMode="MultiLine"
    Width="256px" Height="296px"></asp:TextBox>
    <input id="MyGroupSelectedItem" type="hidden" value="No buttons are
    selected" name="MyGroupSelectedItem">
    </form>
    </body>


    ***** Coed-behind
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    TextBox1.Text = TextBox1.Text & vbCrLf & Request.Form("MyGroup")
    End Sub


    Does this answer your question?

    Thank you, Mike
    Microsoft, ASP.NET Support Professional

    Microsoft highly recommends to all of our customers that they visit the
    http://www.microsoft.com/protect site and perform the three straightforward
    steps listed to improve your computer’s security.

    This posting is provided "AS IS", with no warranties, and confers no rights.


    --------------------
    > From: "Ken Cox [Microsoft MVP]" <>
    > References: <>
    > Subject: Re: radiobutton postback change event
    > Date: Wed, 21 Jan 2004 21:50:44 -0500
    > Lines: 61
    > MIME-Version: 1.0
    > Content-Type: text/plain;
    > format=flowed;
    > charset="Windows-1252";
    > reply-type=original
    > Content-Transfer-Encoding: 7bit
    > X-Priority: 3
    > X-MSMail-Priority: Normal
    > X-Newsreader: Microsoft Outlook Express 6.00.2900.2055
    > X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2055
    > Message-ID: <>
    > Newsgroups: microsoft.public.dotnet.framework.aspnet
    > NNTP-Posting-Host: toronto-hse-ppp3665238.sympatico.ca 65.95.163.55
    > Path:

    cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
    phx.gbl!TK2MSFTNGP12.phx.gbl
    > Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:204077
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >
    > Hi Dave,
    >
    > This seems like a bit of a hack, but....
    >
    > You could use client-side script to loop through the radio buttons and

    find
    > the one that is checked. When you find it, clear the value from the

    onclick
    > event so the postback won't happen.
    >
    > There are probably better ways, but until that solution arrives, this

    might
    > get you going:
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > Label1.Text = Now.ToLongTimeString
    > Dim sb As New System.Text.StringBuilder
    > sb.Append("<script language=javascript>")
    > sb.Append("for (var num=0; num < ")
    > sb.Append("document.forms[0].elements.length;num++)")
    > sb.Append("{")
    > sb.Append("if (document.forms[0].elements[num].checked==true)")
    > sb.Append("{")
    > sb.Append(" document.forms[0].elements[num].onclick='';")
    > sb.Append(" break;")
    > sb.Append("}")
    > sb.Append("}")
    > sb.Append("</script>")
    > Page.RegisterStartupScript("radchk", sb.ToString)
    > End Sub
    >
    >
    > <P>
    > <asp:RadioButton id="RadioButton1" runat="server" Text="Red"
    > GroupName="colours" AutoPostBack="True"></asp:RadioButton>&nbsp;</P>
    > <P>
    > <asp:RadioButton id="RadioButton2" runat="server" Text="Green"
    > GroupName="colours" AutoPostBack="True"
    > Checked="True"></asp:RadioButton></P>
    > <P>
    > <asp:RadioButton id="RadioButton3" runat="server" Text="Blue"
    > GroupName="colours" AutoPostBack="True"></asp:RadioButton></P>
    > <P>
    > <asp:Label id="Label1" runat="server"></asp:Label></P>
    >
    > Ken
    > MVP [ASP.NET]
    >
    >
    > "dave" <> wrote in message
    > news:...
    > >I have half a dozen web form radio buttons on a web form. Each of them

    is
    > >set to postback=true. However, if for instance radiobutton1 is already
    > >selected and the user selects it again, it performs a postback.
    > >
    > > I only want to do a postback if the value of the radiobutton is changed.
    > >
    > > What is the best method to accomplish this?
    > > thx
    > > dave

    >
    >
     
    Mike Moore [MSFT], Jan 23, 2004
    #3
  4. =?Utf-8?B?ZGF2ZQ==?=

    mike Guest

    Mike Moore, thank you very much.

    p.s. I tried to reply to your post using the new MSFT html news viewer
    but after clicking on reply a page opened with "Sorry, we were unable
    to service your request."
     
    mike, Jan 24, 2004
    #4
    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. tshad
    Replies:
    5
    Views:
    5,994
    Joaquin Corchero
    Mar 15, 2005
  2. =?Utf-8?B?16nXntei15XXnyDXpC4=?=

    event fired on allready selected radiobutton

    =?Utf-8?B?16nXntei15XXnyDXpC4=?=, Jul 13, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    460
    Eliyahu Goldin
    Jul 13, 2005
  3. =?Utf-8?B?Vk1J?=

    radioButton change jumps to top of page?

    =?Utf-8?B?Vk1J?=, Aug 1, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    584
    =?Utf-8?B?Vk1J?=
    Aug 1, 2006
  4. BizEd
    Replies:
    1
    Views:
    858
    Aamir Ghanchi
    Oct 2, 2007
  5. Replies:
    12
    Views:
    241
    Thomas 'PointedEars' Lahn
    Dec 6, 2005
Loading...

Share This Page