radiobutton postback change event

G

Guest

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
 
K

Ken Cox [Microsoft MVP]

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]
 
M

Mike Moore [MSFT]

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]" <[email protected]>
References: <[email protected]>
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: <[email protected]>
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 said:
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
 
M

mike

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."
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top