Solved

Modify control's Enabled property without postback?

Posted on 2010-11-10
2
520 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

777 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