Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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