Solved

Modify control's Enabled property without postback?

Posted on 2010-11-10
2
517 Views
Last Modified: 2012-05-10
I have within the EmptyDataTemplate of a GridView a RadioButtonList, a DropDownList and a DetailsView control. I would like, based on which of the two values of the RBL is selected, for the DDL or DV control's Enabled property to change to true/false. I've accomplished this thru the following code-behind in the RBL's SelectedIndexChanged command but this requires AutoPostBack which causes the user to lose their place on the page as the page reloads scrolled to the top etc. It's not very elegant :( Is there a better way?

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadioButtonList rbl = (RadioButtonList)sender;
            DropDownList ddl = (DropDownList)(rbl.NamingContainer.FindControl("DropDownList1"));
            DetailsView dv = (DetailsView)(rbl.NamingContainer.FindControl("DetailsView1"));

            if (rbl.SelectedValue == "1")
            {
                ddl.Enabled = true;
                dv.Enabled = false;
            }
            else
            {
                ddl.Enabled = false;
                dv.Enabled = true;
            }        
        }
0
Comment
Question by:alright
2 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 63 total points
ID: 34105319
Place your GV inside UpdatePanel i.e. use of Asp.net AJAX.
http://msdn.microsoft.com/en-us/library/bb386454.aspx
0
 
LVL 10

Assisted Solution

by:P1ST0LPETE
P1ST0LPETE earned 62 total points
ID: 34105373
Use JavaScript.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function RadioButtonList1_SelectedIndexChanged(radioList)
        {
            var radioButtons = document.getElementsByName(radioList.id);
            if (radioButtons[0].checked) {
                document.getElementById('myDDL').disabled = false;
                document.getElementById('myDV').disabled = true;
            }
            else {
                document.getElementById('myDDL').disabled = true;
                document.getElementById('myDV').disabled = false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="radioList" runat="server" onclick="RadioButtonList1_SelectedIndexChanged(this)">
            <asp:ListItem Text="EnableDDL" />
            <asp:ListItem Text="EnableDV" />
        </asp:RadioButtonList>
        <asp:DropDownList ID="myDDL" runat="server">
            <asp:ListItem Text="Item1" />
            <asp:ListItem Text="Item2" />
            <asp:ListItem Text="Item3" />
        </asp:DropDownList>
        <asp:DetailsView ID="myDV" runat="server" />
    </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now