Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple Controls in GridView Template Field and Perform Update

Posted on 2008-06-18
5
Medium Priority
?
1,664 Views
Last Modified: 2013-12-16
First Thank You for Reading

I have an image and a dropdownlist in a GridView Template Field.

If the user clicks the image, the dropdownlist displays

When user selects dropdownlist value the new value is updated to the database

If the user clicks the image and the dropdownlist is visible then , dropdownlist visible is set to false

I can get as far as getting the index of the image, how do I build a reference to the dropdownlist in the same row.

// start aspx page
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:ImageButton
                            ID="imgRenumber"
                            runat="server"
                            CausesValidation="false"
                            CommandName="Show"
                            ImageUrl="~/Images/EETMove.gif"
                            AlternateText="Manual Renumber"
                            CommandArgument='<%# Container.DataItemIndex %>' />
                        <asp:DropDownList ID="ddlRenumber" runat="server" AutoPostBack="True"  Visible="false">
                        </asp:DropDownList>
                       
                    </ItemTemplate>
                </asp:TemplateField>
// end aspx

//start aspx.cs
// This Part Works!
     protected void grdMasterExhibit_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton button0 = (ImageButton)e.Row.FindControl("imgRenumber");
            button0.CommandArgument = e.Row.RowIndex.ToString();
        }
    }

// This Part Blows Up

    protected void grdMasterExhibit_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            int index = Convert.ToInt32(e.CommandArgument); // This is the image

            GridViewRow grid0 = (GridViewRow)e.CommandSource;
           //The Error --
           // Unable to cast object of type 'System.Web.UI.WebControls.ImageButton'
          // to type 'System.Web.UI.WebControls.GridViewRow'.

            switch (e.CommandName)
            {
                case "Show":
                    DropDownList dropDown1 = (DropDownList)e.Row.FindControl("ddlRenumber");
                   // The Row here is invalid because image is coming in not the grid view
                    dropDown1.Visible = true;
                    break;

                case "Hide":
                    DropDownList dropDown1 = (DropDownList)e.Row.FindControl("ddlRenumber");
                   // The Row here is invalid because image is coming in not the grid view
                    dropDown1.Visible = false;
                    break;

            }

    }
}

//end aspx.cs
0
Comment
Question by:aracen74
[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
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 1500 total points
ID: 21822205
How about this:

case "Show":
  ImageButton ibtn = (ImageButton)sender;
  GridViewRow gvr = (GridViewRow)ibtn.NamingContainer;
  DropDownList dropDown1 = (DropDownList)gvr.FindControl("ddlRenumber");
  dropDown1.Visible = true;
  break;

Open in new window

0
 

Author Comment

by:aracen74
ID: 21838438
First Thank You Prairedog Very Very  Much, What's coming in is the Grid not the GridView so I have to cast  the Grid to GridViewRow

Here's what I found out

               case "Show":

                    GridViewRow gvr = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
                    DropDownList dropDown1 = (DropDownList)gvr.FindControl("DropDownList1");
                    dropDown1.Visible = true;
                    break;

0
 

Author Closing Comment

by:aracen74
ID: 31468547
http://msmvps.com/blogs/SimpleMan/archive/2005/08/06/61782.aspx

               case "Show":

                    GridViewRow gvr = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
                    DropDownList dropDown1 = (DropDownList)gvr.FindControl("DropDownList1");
                    dropDown1.Visible = true;
                    break;
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21839035
Hello aracen74,
First of all, glad you got it worked out.

Secondly, your solution is the same as mine. e.CommandSource = sender.
0
 

Author Comment

by:aracen74
ID: 21855858
Thanks Again :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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