Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

clear the RadioButtonList SelectedIndex

EDB2 (MIS) (OP)
20 Dec 07 11:36
I have a button on my home page which, when clicked, makes visible a RadioButtonList showing the years of history available. Since this is a new application, only 1 year is currently available in the history.

When you select this year, the page_load fires, then the SelectedIndexChanged event of the RBL fires and it redirects to the appropriate page.  So far everything is great.

My problem occurs when the user returns to the home page using the browser back button.  It does not fire the Page_Load event, but returns with the RadioButtonList visible and the year selected. If you click any other button on the page, the Page_Load fires, then the SelectedIndexChanged event fires, and the page redirects without ever executing the click event for the button that was clicked.

I tried setting the SelectedIndex to -1 in the SelectedIndexChanged event just before the redirect, but when the page redisplays the value of SelectedIndex is 0 rather than -1 (I assume because that year is selected when the page displays).  I can't change the value in the page_load because that fires before the SelectedIndexChanged event and it would change the value when it's a valid event. I can't change it in the click event of the new button because that event never fires before the page is redirected.  

I'm stumped.  Can anyone help me with an idea of how to either NOT automatically fire the SelectedIndexChanged event or a way to get that index set back to -1 when the user uses the browser back button and the page redisplays?
jbenson001 (Programmer)
20 Dec 07 11:51
The page does not reload when the browser's back button is pressed, so all entered values etc, will be the same as when they navigated away.  The only way around this that I know of is to use javascript to disable the back button.  However, this is not recommended.
EDB2 (MIS) (OP)
20 Dec 07 14:31
I've found one possibility, which is using the OnClientClick with a bit of Javascript that would reset the RBL selected index to -1.  The OnClientClick fires before the page_load - unfortunately, I don't know much about javascript.

Does anyone know if you can use JavaScript to reset the index on a RadioButtonList?  
jbenson001 (Programmer)
20 Dec 07 14:35
That sounds like a good solution, and yes you can do that, but you'd be best off asking in the JavaScript forum
GoTerps88 (Programmer)
21 Dec 07 11:37
This worked for me.  I'm assuming an id of "rdl" for the radio button list.  Because the ASP.NET radio button list renders as a table, you have to search for input elements within the table.  These input elements would be named "rd1_0", "rdl_1", etc.  So you have to iterate through all the input elements with a name like "rdl".

CODE

<script type="text/javascript">
{
    function init()
    {
        //check for DOM
        if(!document.getElementById || !document.createTextNode)
        {
            //DOM NOT SUPPORTED
            return;
        }
        
        
        var tags = document.getElementsByTagName('input');
        
        for(i=0;i<tags.length;i++)
        {
            name = tags[i].name;
            if(name.indexOf('rdl') > -1)
            {                   
                document.getElementById(tags[i].id).checked = false;
               
            }      
           
        }
        
    }
}
</script>    
</head>
<body onload="init();" >
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="rdl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="rdl_SelectedIndexChanged">
            <asp:ListItem>2007</asp:ListItem>
            <asp:ListItem>2006</asp:ListItem>
        </asp:RadioButtonList></div>
GoTerps88 (Programmer)
21 Dec 07 14:07
A better method than above is to recursively traverse through the html table element looking for an input element.  This gets rid of the tricky logic above that assumes no other controls have an id like 'rdl' or whatever you choose.

CODE

<script type="text/javascript">
{
    function init()
    {
        //check for DOM
        if(!document.getElementById || !document.createTextNode)
        {
            //DOM NOT SUPPORTED
            return;
        }
        
        
       chk = document.getElementById('rdl')
      
       if(chk.hasChildNodes())
       {
            var ch = chk.childNodes;
            for(var i=0;i<ch.length;i++)
            {
                getChildNodes(ch[i]);
            }
       }
        
    }
    function getChildNodes(elm)
    {        
       if(elm.nodeName != "INPUT")
       {
            if(elm.hasChildNodes())
            {           
                var ch = elm.childNodes;
                for(var i=0;i<ch.length;i++)
                {       
                   
                    getChildNodes(ch[i]);
                    
                }
            }
            
        }
        else
        {
            elm.checked = false;
        }
    }
}
</script>    
GoTerps88 (Programmer)
21 Dec 07 14:18
Note: This question was answered better by JBENSON on the Javascript forum.  He provided a simpler solution.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close