简体   繁体   中英

TextBox within ListView control does not exist in current context | C#

I've been stuck on this for a few days and I am really not sure where I am going wrong in my code, any help would be appreciated.

In my .aspx page I have the following code

<%@ Page Title="User Registration" MasterPageFile="MasterPage.master" Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>

<asp:Content ContentPlaceHolderID="main_content" Runat="Server">
        <div class="row">
            <div class="col-12">
                 <asp:Label Visible="false" ID="username" runat="server" Text=""></asp:Label>                


        <asp:SqlDataSource ID="emergency_contact" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT student_records.user_id, student_records.f_name, student_records.l_name, emergency_contact.em_contact_id, emergency_contact.relationship_id, emergency_contact.contact_name, emergency_contact.phone_number, emergency_contact.address_1, emergency_contact.address_2, emergency_contact.town_city, emergency_contact.county, emergency_contact.postcode, emergency_contact.country, relationship.relationship FROM student_records INNER JOIN emergency_contact ON student_records.user_id = emergency_contact.user_id INNER JOIN relationship ON emergency_contact.relationship_id = relationship.relationship_id WHERE (student_records.user_id = @user_id)"
          UpdateCommand="UPDATE [emergency_contact] SET [relationship_id]=@relationship_id, [contact_name]=@contact_name, [phone_number]=@phone_number, [address_1]=@address_1, [address_2]=@address_2, [town_city]=@town_city, [county]=@county, [postcode]=@postcode, [country]=@country WHERE [user_id]=@user_id ">
            <SelectParameters>
                <asp:QueryStringParameter QueryStringField="user_id" Name="user_id" Type="Int32"></asp:QueryStringParameter>
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="relationship_id" Type="Int32" />
                <asp:Parameter Name="contact_name" Type="String" />
                <asp:Parameter Name="phone_number" Type="String" />
                <asp:Parameter Name="address_1" Type="String" />
                <asp:Parameter Name="address_2" Type="String" />
                <asp:Parameter Name="town_city" Type="String" />
                <asp:Parameter Name="county" Type="String" />
                <asp:Parameter Name="postcode" Type="String" />
                <asp:Parameter Name="country" Type="String" />
                <asp:Parameter Name="user_id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>


        <asp:MultiView ID="MultiView" runat="server" ActiveViewIndex="0">

        <asp:View ID="em_contact_view" runat="server">

        <h1>Emergency Contact</h1>

            <asp:ListView ID="em_contact_list" runat="server" DataSourceID="emergency_contact" OnSelectedIndexChanged="em_contact_list_SelectedIndexChanged" >

                <EditItemTemplate>
                    <span style="">
                        contact_name:
                        <asp:TextBox Text='<%# Bind("contact_name") %>' runat="server" ID="contact_nameTextBox" />
                        <br />
                        <asp:Button runat="server" CommandName="Update" Text="Update" ID="update_em_contact" />
                        <asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="cancel_update_em_contact" /><br />
                        <br />
                    </span>
                </EditItemTemplate>

                <EmptyDataTemplate>
                    <span>No data was returned.</span>
                </EmptyDataTemplate>

                <InsertItemTemplate>
                    <span style="">
                        contact_name:
                        <asp:TextBox Text='<%# Bind("contact_name") %>' runat="server" ID="contact_name_insert" />
                        <br />
                        <asp:Button runat="server" CommandName="Insert" Text="Insert" ID="insert_em_contact" />
                        <asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="canncel_insert_em_contact" /><br />
                        <br />
                    </span>
                </InsertItemTemplate>

                <ItemTemplate>
                    <span style="">
                        contact_name:
                        <asp:Label Text='<%# Eval("contact_name") %>' runat="server" ID="contact_nameLabel" />
                        <br />
                        <asp:Button runat="server" CommandName="Edit" Text="Edit" ID="edit_em_contact" />
                        <br />
                        <br />
                    </span>
                </ItemTemplate>

                <LayoutTemplate>
                    <div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
                    <div style="">
                    </div>
                </LayoutTemplate>

                <SelectedItemTemplate>
                    <span style="">
                        contact_name:
                        <asp:Label Text='<%# Eval("contact_name") %>' runat="server" ID="contact_nameLabel" />
                        <br />
                        <asp:Button runat="server" CommandName="Edit" Text="Edit" ID="edit_selected_em_contact" />
                        <br />
                    </span>
                </SelectedItemTemplate>

            </asp:ListView>

            <asp:Button CommandName="NextView" ID="em_contact_next" runat="server" Text="Next" />

        </asp:View>

In my C# file, I have the following code.

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

using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Globalization;

public partial class registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String name = Request.QueryString["user_id"];
        username.Text = name;
    }

    protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(ConnectionString);

        myConnection.Open();

        string value = contact_nameTextBox.Text;
        //additional code

    }
}

Whenever I try and call string value = contact_nameTextBox.Text; I get:

CS0103: The name 'contact_nameTextBox' does not exist in the current context

I have tried referencing TextBoxes outside of a ListView and it works fine, so I am not sure what it is about ListView that prevents me from calling TextBoxes.

You need to loop over all of the items and update the text on the selected row. I have not used web forms in years and the code below is all by memory (or lack there of) so you'll need to check it but this is basically what you need to do.

protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
    {
        var items = this.em_contact_list.SelectedItems;

        foreach ( ListViewItem item in items)
        {
            var tbox = item.findControl("contact_nameTextBox");

            if(tbox != null)
            {
               string value = ((TextBox)tbox).Text;
            }
        }
    }

Be aware that because you have the same control in a list view that you will potentially update more than one row so further checking to ensure that you have the one you want will be needed.

  1. make sure that ID is not duplicated in your apsx page because after creating ListView it duplicates the tools name in (EditItemTemplate, Insert, Alternate etc..).

  2. make sure every group contains your tool has runat="server" property

then clean and rebuild

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM