By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

radiobutton postback change event

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <dy@onlinelg.com> wrote in message
news:C4**********************************@microsof t.com...
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


Nov 18 '05 #2

P: n/a
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]" <BA************@sympatico.ca>
References: <C4**********************************@microsoft.co m>
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: <ec**************@TK2MSFTNGP12.phx.gbl>
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!cpmsftng xa09.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" <dy@onlinelg.com> wrote in message
news:C4**********************************@microsof t.com...
I have half a dozen web form radio buttons on a web form. Each of them isset 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



Nov 18 '05 #3

P: n/a
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."
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.