Trying to customize DetailsView Edit Template

Dear Experts,
I am using DetailsView controller to edit a database record (MSSQL). I want to limit the entries for "RelationshipStatus". I converted the Edit Field for RelationshipStatus into a template field and added a dropdown list with the list items.

After selecting the "Edit" button on the displayed page the DetailsView displays the field headers and the existing data in textboxes. In the "RelationshipStatus" textbox the data is correctly displayed. Beneath the textbox is the dropdownlist with the listitems.

Everything perfect so far.

What I want to happen is the user will click on the dropdown list (ddlEditRelationshipStatus) to select new status. When selected it shows up in the update textbox (txtRelationshipStatus) as a preview. I added  onselectedindexchanged="RelationshipStatusSelected" to make this happen.

Then when the "Update" button is clicked I want the record updated.

This is the problem:
When the page loads I get this error message:
CS0103: The name 'txtRelationshipStatus' does not exist in the current context.

Below is the HTML and Code Behind.

HTML
       <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="364px" AutoGenerateRows="False" DataKeyNames="BasicProfileRecId" DataSourceID="sdsEditBasicProfile">
            <FieldHeaderStyle Font-Names="Arial" HorizontalAlign="Right" Width="150px" Font-Bold="True" />
            <Fields>
                <asp:BoundField DataField="BasicProfileRecId" HeaderText="BasicProfileRecId" InsertVisible="False" ReadOnly="True" SortExpression="BasicProfileRecId" Visible="False" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
                <asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" />
                <asp:BoundField DataField="Address2" HeaderText="Address2" SortExpression="Address2" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="StateProvince" HeaderText="State/Province" SortExpression="StateProvince" />
                <asp:BoundField DataField="PostalCode" HeaderText="Postal Code" SortExpression="PostalCode" />
                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
                <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number" SortExpression="PhoneNumber" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
                <asp:TemplateField HeaderText="Relationship Status" SortExpression="RelationshipStatus">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtRelationshipStatus" runat="server" Text='<%# Bind("RelationshipStatus") %>'></asp:TextBox>
                        <asp:DropDownList ID="ddlEditRelationshipStatus" runat="server" onselectedindexchanged="RelationshipStatusSelected">
                            <asp:ListItem>--&gt;Select Status&lt;--</asp:ListItem>
                            <asp:ListItem>Single</asp:ListItem>
                            <asp:ListItem>In a Relationship</asp:ListItem>
                            <asp:ListItem>Engaged</asp:ListItem>
                            <asp:ListItem>Married</asp:ListItem>
                            <asp:ListItem>It&#39;s Complicated</asp:ListItem>
                            <asp:ListItem>In an Open Relationship</asp:ListItem>
                            <asp:ListItem>Widowed</asp:ListItem>
                            <asp:ListItem>Separated</asp:ListItem>
                            <asp:ListItem>Divorced</asp:ListItem>
                            <asp:ListItem>In a Civil Union</asp:ListItem>
                            <asp:ListItem>In a domestic Partnership</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("RelationshipStatus") %>'></asp:TextBox>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("RelationshipStatus") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="MonthBorn" HeaderText="Month Born" SortExpression="MonthBorn" />
                <asp:BoundField DataField="DayBorn" HeaderText="Day Born" SortExpression="DayBorn" />
                <asp:BoundField DataField="YearBorn" HeaderText="Year Born" SortExpression="YearBorn" />
                <asp:CommandField ButtonType="Button" ShowEditButton="True" />
            </Fields>
        </asp:DetailsView>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _icss__TeacherPages_EditBasicProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RelationshipStatusSelected(object sender, EventArgs e)
    {
        txtRelationshipStatus.Text = ddlEditRelationshipStatus.SelectedValue;
    }

}

To me this seems so simple. I don't know what I'm doing wrong.
Thanks for your help.
bobbellowsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
You may need to use the FindControl method to get a reference to the textbox. This is because it is inside a "container" control.

Try this:
TextBox relationshipStatus = (TextBox)DetailsView1.FindControl("txtRelationshipStatus");
relationshipStatus.Text = ddlEditRelationshipStatus.SelectedValue;
0
bobbellowsAuthor Commented:
Shaun,
Thanks for your help. It still throws the exception -- copied it below. I've had overnight to think about this. Could it be because the DetailsView isn't bound when it tries to compile the code behind?
0
Shaun KlineLead Software EngineerCommented:
So the issue is occurring when you attempt to update the record? How are you referencing the text box in your update code?

Any reason to keep the text box if you want to limit entries to only those in the drop-down list?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

bobbellowsAuthor Commented:
So the issue is occurring when you attempt to update the record?
>>The exception is thrown when the page is loading

How are you referencing the text box in your update code?
>>I do programming on a very limited scale. So, I created the DetailsView using the controller in VisualStudio and bound it to a SqlDataSource. From the DetailsView controller tasks I edited the RelationshipStatus field converting it to a template so I could insert the dropdown list.

Any reason to keep the text box if you want to limit entries to only those in the drop-down list?
>> thought the textbox was necessary for the update. If it wasn't there how would the selection be updated to the db (MS SQL)?
0
Shaun KlineLead Software EngineerCommented:
If the selected value property of your dropdown list is bound to your datasource, the insert/update work without the textbox.

As for the error, did you modify the source of the aspx page to add the textbox to the template field, or did you use the UI? If you used the source, try switching to the UI. Sometimes when modifying the source directly, VS does not add the control reference into the designer file correctly.
0
bobbellowsAuthor Commented:
As for the error, did you modify the source of the aspx page to add the textbox to the template field, or did you use the UI? If you used the source, try switching to the UI. Sometimes when modifying the source directly, VS does not add the control reference into the designer file correctly.
>>I think you are right. I coded it directly in the code-behind page. I went to the SqlDataSource and looked at the Sql statements. Nothing reflected anything similar to the change.

However, I also didn't see anyplace to make this kind of correction.
0
Shaun KlineLead Software EngineerCommented:
Try removing the control and re-adding it using the page designer. After adding the control, you should be able to use the databind functionality in the page designer to bind the desired properties to your datasource.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bobbellowsAuthor Commented:
Shaun stuck with me to the end. Thanks. Took a  while to get back to this. My main job had me putting in some overtime. These auxiliary tasks always take the back seat.
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.