Solved

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

Posted on 2006-11-12
3
2,157 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

13 Experts available now in Live!

Get 1:1 Help Now