Solved

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

Posted on 2006-11-12
3
2,163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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