Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

16 Experts available now in Live!

Get 1:1 Help Now