[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.4

ASP.NET: Retrieving DropDownList in Gridview RowUpdating event

Asked by BigBadJock in Programming for ASP.NET, C# Programming Language, .NET Framework 3.x versions

Tags: ASP.NET; .Net, C#; GridView; DropDownList

I have a problem, with GridView and TemplateFields. We have an application which creates maintenance screens based on an xml configuration file. The xml describes which fields to show, etc. This all works

However, we have a new requirement to add DropDownLists to some columns to enforce data integrity.

I have therefore added the relevant xml, and when creating the grid, create the column as a TemplateField  and the EditItem as a DropDownList with its own SqlDataSource.

So far so good, this all works, the grid is displayed, the DropDownList appears, filled with the correct data, shows the correct initial item and lets the user select another.

Here is where I have problems. The when trying to get the value from the DropDownList in the RowUpdating event, I cant find it. Ive used FindControl to no avail. I create a RecursiveFindControl method that ran through every control on the page, and it cant find it. Ive tried peeking in the cell controls in the debugger and its just not there.

Can anyone help? Ive duplicated it in a simpler test page, with a test database.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="WareHouseAdmin.test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated" onrowupdating="GridView1_RowUpdating" DataSourceID="SqlDataSource1">
        </asp:GridView>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" onupdated="SqlDataSource1_Updated" onupdating="SqlDataSource1_Updating"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource>
    </form>
</body>
</html>
 
 
-----------------------------
 
 
 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WareHouseAdmin
{
    public partial class test2 : System.Web.UI.Page
    {
        
        protected void Page_Init(object sender, EventArgs e)
        {
            SetupData();
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            BuildGrid();
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
 
        private void SetupData()
        {
            string connection = ConfigurationManager.ConnectionStrings["warehouseConnectionString"].ConnectionString;
            this.SqlDataSource1.ConnectionString = connection;
            this.SqlDataSource1.SelectCommand = "select * from songs";
            this.SqlDataSource1.UpdateCommand = "UPDATE [Songs] SET [AlbumId]=@albumId, [ArtistId]=@artistId, [SongTitle] = @SongTitle, [notes] = @notes WHERE [ID] = @ID";
            this.SqlDataSource1.UpdateParameters.Clear();
 
            this.SqlDataSource2.ConnectionString = connection;
            this.SqlDataSource2.SelectCommand = "select * from albums";
        }
 
        private void BindGrid()
        {
            this.GridView1.DataBind();
        }
 
        private void BuildGrid()
        {
            this.GridView1.Columns.Clear();
            AddColumn("id", "id", "Int32");
            //AddColumn("Album", "albumId", "Int32");
            AddDropDownColumn("Album", "albumId","id","name",SqlDataSource2);
            AddColumn("Artist", "artistId", "Int32");
            AddColumn("Title", "songTitle","String");
            AddColumn("Notes", "notes", "String");
 
            this.GridView1.DataKeyNames = new string[] { "id" };
 
        }
 
 
        private void AddColumn(string header, string name, string dataType)
        {
            BoundField bf = new BoundField();
            bf.HeaderText = header;
            bf.DataField = name;
            bf.SortExpression = name;
            bf.ReadOnly = false;
            bf.HtmlEncode = true;
            this.GridView1.Columns.Add(bf);
 
 
            TypeCode tc = (TypeCode)Enum.Parse(typeof(TypeCode), dataType);
            Parameter p = new Parameter(name, tc);
            this.SqlDataSource1.UpdateParameters.Add(p);
 
        }
 
        private void AddDropDownColumn(string header, string name, string valueColumn, string displayColumn, SqlDataSource dataSource)
        {
            TemplateField tf = new TemplateField();
            tf.HeaderText = header;
            tf.ItemTemplate = new DDLTemplate(ListItemType.Item, name, null, null, null);
            tf.EditItemTemplate = new DDLTemplate(ListItemType.EditItem, name, valueColumn, displayColumn, dataSource );
            this.GridView1.Columns.Add(tf);
 
            TypeCode tc = (TypeCode)Enum.Parse(typeof(TypeCode), "Int32");
            Parameter p = new Parameter(name, tc);
            this.SqlDataSource1.UpdateParameters.Add(p);
 
        }
 
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            BindGrid();
        }
 
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = this.GridView1.Rows[e.RowIndex];
            Control foundControl = RecursiveFindControl(row, "GridView1$ctl07$albumId_ddl");
            if (foundControl != null)
            {
                DropDownList ddl = (DropDownList)foundControl;
                string val = ddl.SelectedValue;
                e.NewValues.Add("albumId", val);
            }
            BindGrid();
        }
 
        private Control RecursiveFindControl(Control root, string id)
        {
            if (root.ID != null)
            {
                if (root.ID.EndsWith(id))
                {
                    return root;
                }
            }
            if (root.ClientID != null)
            {
                if (root.ClientID.EndsWith(id))
                {
                    return root;
                }
            }
            foreach (Control subControl in root.Controls)
            {
                Control foundControl = RecursiveFindControl(subControl, id);
                if (foundControl != null)
                {
                    return foundControl;
                }
            }
            return null;
        }
 
        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
        }
 
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            BindGrid();
        }
 
        protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
            }
        }
 
        protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {
            System.Console.WriteLine(e.Command);
 
        }
 
    }
 
    public class DDLTemplate : System.Web.UI.Page, ITemplate
    {
        ListItemType templateType;
        string column;
        string valueColumn;
        string displayColumn;
        SqlDataSource dataSource;
 
        public DDLTemplate(ListItemType type, string column, string valueColumn, string displayColumn, SqlDataSource dataSource)
        {
            this.templateType = type;
            this.column = column;
            this.valueColumn = valueColumn;
            this.displayColumn = displayColumn;
            this.dataSource = dataSource;
        }
 
        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (templateType)
            {
                case ListItemType.Header:
                    break;
 
                case ListItemType.Item:
                    Literal lc = new Literal();
                    lc.DataBinding += new EventHandler(lc_DataBinding);
                    container.Controls.Add(lc);
                    break;
 
                case ListItemType.EditItem:
                    DropDownList ddl = new DropDownList();
                    ddl.ID = "albumId_ddl";
                    ddl.DataValueField = this.valueColumn;
                    ddl.DataTextField = this.displayColumn;
                    ddl.DataSource = this.dataSource;
                    ddl.DataBinding += new EventHandler(ddl_DataBinding);
                    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
                    container.Controls.Add(ddl);
                    break;
 
                case ListItemType.Footer:
                    break;
 
            }
        }
 
        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            Session["albumId_val"] = ddl.SelectedValue;
        }
 
        void lc_DataBinding(object sender, EventArgs e)
        {
            Literal lc = (Literal)sender;
            GridViewRow row = (GridViewRow)lc.NamingContainer;
            string text = DataBinder.Eval(row.DataItem, this.column).ToString();
            lc.Text = text;
        }
 
        void ddl_DataBinding(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            GridViewRow row = (GridViewRow)ddl.NamingContainer;
            string id = DataBinder.Eval(row.DataItem, column).ToString();
            ddl.SelectedValue = id;
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
 
        }
 
    }
 
}
[+][-]03/06/09 06:31 AM, ID: 23816835Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 06:38 AM, ID: 23816916Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/06/09 06:43 AM, ID: 23816977Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 06:48 AM, ID: 23817030Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/06/09 06:50 AM, ID: 23817043Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 06:55 AM, ID: 23817087Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 06:58 AM, ID: 23817110Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:02 AM, ID: 23817151Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/06/09 07:04 AM, ID: 23817168Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:06 AM, ID: 23817193Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:07 AM, ID: 23817210Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:09 AM, ID: 23817228Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:17 AM, ID: 23817326Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:17 AM, ID: 23817335Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/06/09 07:26 AM, ID: 23817432Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:28 AM, ID: 23817458Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 07:46 AM, ID: 23817688Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/06/09 07:55 AM, ID: 23817787Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 08:11 AM, ID: 23817971Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/06/09 08:17 AM, ID: 23818054Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/06/09 08:20 AM, ID: 23818086Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/06/09 08:30 AM, ID: 23818200Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/11/09 08:57 AM, ID: 23858815Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/11/09 09:05 AM, ID: 23858917Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/09 04:30 AM, ID: 23866991Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Programming for ASP.NET, C# Programming Language, .NET Framework 3.x versions
Tags: ASP.NET; .Net, C#; GridView; DropDownList
Sign Up Now!
Solution Provided By: BigBadJock
Participating Experts: 2
Solution Grade: A
 
[+][-]03/12/09 05:54 AM, ID: 23867633Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/09 05:55 AM, ID: 23867635Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625