Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clear the RadioButtonList SelectedIndex

Status
Not open for further replies.

EDB2

MIS
Sep 11, 2002
36
US
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?
 
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.
 
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?
 
That sounds like a good solution, and yes you can do that, but you'd be best off asking in the JavaScript forum
 
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>
 
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>
 
Note: This question was answered better by JBENSON on the Javascript forum. He provided a simpler solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top