简体   繁体   English

ListView控件中的TextBox在当前上下文中不存在 C#

[英]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 在我的.aspx页面中,我有以下代码

<%@ 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. 在我的C#文件中,我有以下代码。

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; 每当我尝试调用string value = contact_nameTextBox.Text; I get: 我得到:

CS0103: The name 'contact_nameTextBox' does not exist in the current context CS0103:名称“ contact_nameTextBox”在当前上下文中不存在

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. 我尝试在ListView之外引用TextBoxes,但它工作正常,因此我不确定ListView是什么使我无法调用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. 我已经多年没有使用过Web表单了,下面的代码都是按内存(或缺少内存)进行的,因此您需要检查它,但这基本上是您需要做的。

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..). 确保ID在您的apsx页面中没有重复,因为在创建ListView之后,它会在(EditItemTemplate,Insert,Alternate等)中复制工具名称。

  2. make sure every group contains your tool has runat="server" property 确保每个组包含您的工具都具有runat="server" property

then clean and rebuild 然后清理并重建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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