Solved

Finding the SelectedValue of a Dropdown list in ASP.NET GridView when editing a row

Posted on 2006-11-12
3
2,160 Views
Last Modified: 2008-02-03
I have a Gridview with a templated field that contains a dropdownlist.   I want to disable that dropdown list if the selected value is equal to a specific value.

I have tried several variations of getting to this control but have failed in frustration. Need help.   My assumption is that the templated row is not available until you go into editing mode, so the following code example assumes that I have to have my code in the RowEditing Event Handler.  here is my code so far:

        protected void GV1_RowEditing(object o, GridViewEditEventArgs e)
        {
            DropDownList ddlChangeRole2 = GridView1.Rows[e.NewEditIndex].FindControl("DropDownList2").????  // here is where I am running into problem.  On databoud field this method worked fine. on templated field it does not
            if (ddlChangeRole.SelectedValue == "Owner")
            {
                ddlChangeRole.Enabled = false;
            }
        }
I understand how to do this in .net 1.1 but am new to 2.0 so any help would be appreciated.

Thanks
0
Comment
Question by:jholmes724
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17929248
How is the DropDownList defined within the GridView?

Bob
0
 
LVL 5

Accepted Solution

by:
prajapati84 earned 100 total points
ID: 17929290
In ur html code, in template column of gridview insert the code below for dropdownlist.

<ItemTemplate>                                                          
       <asp:DropDownList ID="ddlChangeRole" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlChangeRole_IndexChanged" />
</ItemTemplate>

And in .aspx.cs page add the code below. The function which i have called in OnSelectedIndexChanged property of dropdownlist will be used here.

void ddlChangeRole_IndexChanged(object sender, EventArgs e)
   {
        DropDownList ddlChangeRole2 = new DropDownList();
        if (GridView1.Rows.Count > 0)
        {
            for (i = 0;i <= (GridView1.Rows.Count - 1); i++)
            {
                GridViewRow row = GridView1.Rows[i];
                if ((((DropDownList)(row.FindControl("DropDownList2"))).SelectedValue == "Owner"))
                {
                    ddlChangeRole.Enabled = false;
                    return;
                }
            }
        }
    }

If u have any difficulty in this code please post.

Regads,
Mukesh
0
 

Author Comment

by:jholmes724
ID: 17931837
Thanks, good tip.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 43
C# MVC and a global static factory that never dies 4 58
Deploying to Azure 3 29
tutorial for ebay api 3 35
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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