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

时间:2015-11-02 17:34:39

标签: c# asp.net listview textbox

我已经被困在这几天了,我真的不确定我的代码在哪里出错,任何帮助都会受到赞赏。

在我的.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>

在我的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

    }
}

每当我尝试拨打string value = contact_nameTextBox.Text;时,我都会:

  

CS0103:名称&#39; contact_nameTextBox&#39;在当前不存在   上下文

我已经尝试在ListView之外引用TextBox并且它工作正常,所以我不确定ListView是什么阻止我调用TextBoxes。

2 个答案:

答案 0 :(得分:0)

您需要遍历所有项目并更新所选行上的文本。我多年没有使用过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;
            }
        }
    }

请注意,因为您在列表视图中具有相同的控件,您可能会更新多行,因此需要进一步检查以确保您拥有所需的控件。

答案 1 :(得分:0)

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

  2. 确保每个包含您工具的组都有runat="server" property

然后清理并重建