Do not use on any
shared computer
August 30, 2008 03:31am pdt
 
[x]
Attachment Details

dropDownList in DetailsView being populated twice

I have a page that contains a gridView of records.  When any of the rows in the gridview are selected, I want to populate a DetailsView with the details of that record.  In the edit template of the detailsview I have a series of user controls which are basically dropDownLists populated dynamically.  I need to set the selectedText property of each of those dropDownLists with their corresponding values from the record that has been selected.  The problem is that the DDLs are being populated twice and I can't figure out why.  I'm sure it has something to do with the DetailsView being databound but I am stuck.  

Here's my code
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:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
main page template:
 
<%@ Page Language="C#" MasterPageFile="~/CMOL.master" AutoEventWireup="false" CodeFile="viewRecords.aspx.cs" Inherits="viewRecords" title="View Records" %>
 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
 
<%@ Register Src="~/userControls/ddlAppIdentifier.ascx" TagName="tnAppIdentifier" TagPrefix="tpAppIdentifier" %>
<%@ Register Src="~/userControls/ddlChangeType.ascx" TagName="tnChangeType" TagPrefix="tpChangeType" %>
<%@ Register Src="~/userControls/ddlBusinessImpact.ascx" TagName="tnBusinessImpact" TagPrefix="tpBusinessImpact" %>
<%@ Register Src="~/userControls/ddlDeploymentDriver.ascx" TagName="tnDeploymentDriver" TagPrefix="tpDeploymentDriver" %>
<%@ Register Src="~/userControls/ddlComplexity.ascx" TagName="tnComplexity" TagPrefix="tpComplexity" %>
<%@ Register Src="~/userControls/ddlSize.ascx" TagName="tnSize" TagPrefix="tpSize" %>
<%@ Register Src="~/userControls/ddlApprovingManager.ascx" TagName="tnApprovingManager" TagPrefix="tpApprovingManager" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>
            <table align="center" width="100%" cellpadding="2" cellspacing="2" border="0">
                <tr>
                    <td align="left" valign="top">
                    <asp:GridView 
                        ID="gvRecords"
                        DataKeyNames="changeReleaseRecordID"
                        DataSourceID="adsRecords" 
                        AutoGenerateColumns="False" 
                        width="350"
                        runat="server">
                        <Columns>
                            <asp:CommandField 
                                ButtonType="button" 
                                ShowSelectButton="true" 
                                ItemStyle-HorizontalAlign="center" 
                                ControlStyle-CssClass="button_8" 
                                SelectText="Select" />
                            <asp:BoundField DataField="appIdentifierName" HeaderText="ID" SortExpression="appIdentifierName" />
                            <asp:BoundField ItemStyle-Wrap="true" DataField="appComponentName" HeaderText="Component" SortExpression="appComponentName" />
                        </Columns>
                    </asp:GridView>
                    </td>
                    <td width="75">&nbsp;</td>
                    <td>
                        <asp:DetailsView 
                            ID="dvRecord" 
                            HorizontalAlign="right" 
                            DataSourceID="adsRecord" 
                            AutoGenerateRows="False"
                            runat="server">
                            <Fields>
                                <asp:TemplateField HeaderText="App ID" HeaderStyle-Width="100" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="appIdentifierName">
                                    <EditItemTemplate>
                                        <tpAppIdentifier:tnAppIdentifier id="ddlAppIdentifier" SelectedText='<%# Eval("appIdentifierName") %>' EditControl="true" runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblAppIdentifierName" runat="server" Text='<%# Eval("appIdentifierName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Component" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="appComponentName">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtAppComponentName" runat="server" CssClass="textbox_9" Text='<%# Eval("appComponentName") %>' />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblappComponentName" runat="server" Text='<%# Eval("appComponentName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Implementation Date" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="prodImplementationDate">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtProdImplementationDate" CssClass="textbox_9" Text='<%# Eval("prodImplementationDate") %>' runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblProdImplementationDate" runat="server" Text='<%# Eval("prodImplementationDate") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Release" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="changeReleaseName">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtChangeReleaseName" runat="server" CssClass="textbox_9" Text='<%# Eval("changeReleaseName") %>' />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblchangeReleaseName" runat="server" Text='<%# Eval("changeReleaseName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Number of Changes" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="numberOfChanges">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtNumberOfChanges" runat="server" CssClass="textbox_9" Text='<%# Eval("numberOfChanges") %>' />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblNumberOfChanges" runat="server" Text='<%# Eval("numberOfChanges") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Change Type" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="changeTypeName">
                                    <EditItemTemplate>
                                        <tpChangeType:tnChangeType id="ddlChangeType" EditControl="true" SelectedText='<%# Eval("changeTypeName") %>' runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblChangeTypeName" runat="server" Text='<%# Eval("changeTypeName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Deployment Driver" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="deploymentDriverName">
                                    <EditItemTemplate>
                                        <tpDeploymentDriver:tnDeploymentDriver id="ddlDeploymentDriver" EditControl="true" SelectedText='<%# Eval("deploymentDriverName") %>' runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblDeploymentDriverName" runat="server" Text='<%# Eval("deploymentDriverName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Parent ID" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="parentChangeReleaseID">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtParentChangeReleaseID" runat="server" CssClass="textbox_9" Text='<%# Eval("parentChangeReleaseID") %>' />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblParentChangeReleaseID" runat="server" Text='<%# Eval("parentChangeReleaseID") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Business Impact" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="businessImpactName">
                                    <EditItemTemplate>
                                        <tpBusinessImpact:tnBusinessImpact id="ddlBusinessImpact" EditControl="true" SelectedText='<%# Eval("businessImpactName") %>' runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblBusinessImpactName" runat="server" Text='<%# Eval("businessImpactName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Complexity" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="complexityName">
                                    <EditItemTemplate>
                                        <tpComplexity:tnComplexity id="ddlComplexity" EditControl="true" SelectedText='<%# Eval("complexityName") %>' runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblComplexityName" runat="server" Text='<%# Eval("complexityName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                
                                <asp:TemplateField HeaderText="Size" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="sizeName">
                                    <EditItemTemplate>
                                        <tpSize:tnSize id="ddlSize" EditControl="true" SelectedText='<%# Eval("sizeName") %>' runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblSizeName" runat="server" Text='<%# Eval("sizeName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Manager" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="approvingManagerName">
                                    <EditItemTemplate>
                                        <tpApprovingManager:tnApprovingManager id="ddlApprovingManager" EditControl="true" SelectedText='<%# Eval("approvingManagerName") %>' runat="server" />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblApprovingManagerName" runat="server" Text='<%# Eval("approvingManagerName") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderStyle-VerticalAlign="top" HeaderText="Comments" ItemStyle-Width="400" HeaderStyle-CssClass="dvHeaderStyle" SortExpression="comments">
                                    <EditItemTemplate>
                                        <asp:TextBox 
                                            ID="txtComments" 
                                            TextMode="MultiLine" 
                                            Rows="10" 
                                            Columns="70"
                                            CssClass="textbox_9" 
                                            Text='<%# Eval("comments") %>' 
                                            runat="server"/>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblComments" runat="server" Text='<%# Eval("comments") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:CommandField ItemStyle-HorizontalAlign="right" ButtonType="Button" ControlStyle-CssClass="button_8" ShowEditButton="True" />
                            </Fields>
                        </asp:DetailsView>
                    </td>
                </tr>
            </table>
         </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
    </tr>
</table>
<asp:AccessDataSource 
    ID="adsRecords" 
    runat="server" 
    DataFile="~/App_Data/changeReleaseReporting.mdb" 
    SelectCommand="SELECT R.changeReleaseRecordID, s.sizeName, appID.appIdentifierName, R.appComponentName, bi.businessImpactName, am.approvingManagerName, cr.changeReleaseName, R.comments, R.numberOfChanges, R.parentChangeReleaseID FROM ((((((((approvingManager am RIGHT OUTER JOIN Records R ON am.approvingManagerID = R.approvingManagerID) LEFT OUTER JOIN complexity c ON R.complexityID = c.complexityID) LEFT OUTER JOIN deploymentDriver dd ON R.deploymentDriverID = dd.deploymentDriverID) LEFT OUTER JOIN changeType ct ON R.changeTypeID = ct.changeTypeID) LEFT OUTER JOIN businessImpact bi ON R.businessImpactID = bi.businessImpactID) LEFT OUTER JOIN changeRelease cr ON R.changeReleaseID = cr.changeReleaseID) LEFT OUTER JOIN appIdentifier appID ON R.appIdentifierID = appID.appIdentifierID) LEFT OUTER JOIN appSize s ON R.sizeID = s.sizeID)">
</asp:AccessDataSource>
 
<asp:AccessDataSource 
    ID="adsRecord" 
    runat="server" 
    DataFile="~/App_Data/changeReleaseReporting.mdb" 
    SelectCommand="SELECT s.sizeName, appID.appIdentifierName, R.appComponentName, bi.businessImpactName, am.approvingManagerName, cr.changeReleaseName, R.comments, R.numberOfChanges, R.parentChangeReleaseID, c.complexityName, dd.deploymentDriverName, ct.changeTypeName, R.prodImplementationDate FROM ((((((((approvingManager am RIGHT OUTER JOIN Records R ON am.approvingManagerID = R.approvingManagerID) LEFT OUTER JOIN complexity c ON R.complexityID = c.complexityID) LEFT OUTER JOIN deploymentDriver dd ON R.deploymentDriverID = dd.deploymentDriverID) LEFT OUTER JOIN changeType ct ON R.changeTypeID = ct.changeTypeID) LEFT OUTER JOIN businessImpact bi ON R.businessImpactID = bi.businessImpactID) LEFT OUTER JOIN changeRelease cr ON R.changeReleaseID = cr.changeReleaseID) LEFT OUTER JOIN appIdentifier appID ON R.appIdentifierID = appID.appIdentifierID) LEFT OUTER JOIN appSize s ON R.sizeID = s.sizeID) WHERE changeReleaseRecordID = @changeReleaseRecordID">
    <SelectParameters>
        <asp:ControlParameter Name="changeReleaseRecordID" ControlId="gvRecords" PropertyName="SelectedValue"/>
    </SelectParameters>
</asp:AccessDataSource>
</asp:Content>
 
and the code behind for the user control
 
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Xml;
using System.Net;
using System.Web.UI.WebControls;
 
partial class userControls_ddlAppIdentifier : System.Web.UI.UserControl
{
    private string strText;
    private bool bSet;
 
    protected void Page_Load(object sender, System.EventArgs e) 
    {
        if (!IsPostBack || EditControl == true)
        {
            if (ddlAppIdentifier.Items.Count < 1)
            {
                string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
                OleDbConnection oConn = new OleDbConnection(sConnString);
 
                string sSQL = "SELECT AI.appIdentifierID, AI.appIdentifierName FROM appIdentifier AI";
                OleDbDataAdapter oAdapter = new OleDbDataAdapter(sSQL, oConn);
 
                DataSet oDS = new DataSet();
                oAdapter.Fill(oDS, "AppIdentifiers");
                ddlAppIdentifier.Items.Clear();
                ddlAppIdentifier.AppendDataBoundItems = true;
 
                ddlAppIdentifier.DataSource = oDS.Tables[0];
                ddlAppIdentifier.DataValueField = oDS.Tables[0].Columns["appIdentifierID"].ColumnName.ToString();
                ddlAppIdentifier.DataTextField = oDS.Tables[0].Columns["appIdentifierName"].ColumnName.ToString();
                ddlAppIdentifier.DataBind();
            }
        }
    }
 
    public string SelectedText
    {
        get { return ddlAppIdentifier.SelectedItem.Text; }
        set
        {
            ListItem li = ddlAppIdentifier.Items.FindByText(value);
 
            if (li != null)
            {
                li.Selected = true;
            }
 
            if (value == "")
            {
                ddlAppIdentifier.SelectedIndex = 0;
            }
        }
    }
 
    public string SelectedValue
    {
        get
        {
            if (ddlAppIdentifier.SelectedValue != "")
            {
                strText = ddlAppIdentifier.SelectedItem.Value;
            }
            else
            {
                strText = "";
                //.Items(0).Value 
            }
            return strText;
        }
    }
 
    public bool EditControl
    {
        get { return bSet; }
        set { bSet = value; }
    }
}
 
thanks in advance,
 
nate
Start your free trial to view this solution
[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!

Question Stats
Zone: Programming
Question Asked By: stretch73
Solution Provided By: l-cm
Participating Experts: 2
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by RDdice

Rank: Master

Expert Comment by RDdice:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by l-cm

Rank: Master

Accepted Solution by l-cm:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by stretch73
Author Comment by stretch73:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by l-cm

Rank: Master

Expert Comment by l-cm:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by stretch73
Author Comment by stretch73:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by stretch73
Author Comment by stretch73:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by l-cm

Rank: Master

Expert Comment by l-cm:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by l-cm
Since the ddl is a child control in the detailsview control, did you try getting the ddl by doing a findcontrol from the detailsview (Such as dvRecord.FindControl("ddlAppIdentifier"))?
 
 
Comment by stretch73
I tried that but it presents another problem.  The value that I want to set the index to is coming off of the detailsView being bound and I don't know how to retrieve the value when the gridViews index is changed.  
 
 
Comment by stretch73
What I don't get is that if I leave in appendDataBoundItems it sets fine but then I get the multiple populating problem.  So if it's set to true, it thinks the items have been cleared out, databinds the control, and then sets the index.
 
 
Comment by stretch73
I-cm

In case you're still out there I figured it out.  If I set the SelectedValue property first it works.  Don't completely understand why but it's how I've done it before and I just wasn't paying attention.  Thanks for your earlier replies.

N
 
 
Comment by l-cm
Glad you were able to figure it out!
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628