在我的.aspx页面中,我在ListView控件中有以下代码;
<%@ Page Title="User Registration" MasterPageFile="MasterPage.master" Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>
<asp:Content ContentPlaceHolderID="head" Runat="Server">
<!-- Your Code goes here -->
</asp:Content>
<asp:Content ContentPlaceHolderID="menu" Runat="Server">
<!-- Your Code goes here -->
</asp:Content>
<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="semester1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT modules.cats_points, modules.module_id, modules.compulsory, modules.module_name, modules.year FROM Courses INNER JOIN courses_vs_modules ON Courses.course_id = courses_vs_modules.course_id INNER JOIN modules ON courses_vs_modules.module_id = modules.module_id INNER JOIN student_records ON Courses.course_id = student_records.course_id AND modules.year = student_records.year AND courses_vs_modules.course_id = student_records.course_id WHERE (student_records.user_id = @user_id) AND (modules.semester = 1)"
InsertCommand="INSERT INTO [students_vs_modules]([user_id], [module_id]) VALUES (@user_id, @module_id)">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="user_id" Name="user_id" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="module_id" Type="Int32" />
</InsertParameters>
</asp:sqldatasource>
<asp:sqldatasource ID="semester2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT modules.cats_points, modules.module_id, modules.compulsory, modules.module_name, modules.year FROM Courses INNER JOIN courses_vs_modules ON Courses.course_id = courses_vs_modules.course_id INNER JOIN modules ON courses_vs_modules.module_id = modules.module_id INNER JOIN student_records ON Courses.course_id = student_records.course_id AND modules.year = student_records.year AND courses_vs_modules.course_id = student_records.course_id WHERE (student_records.user_id = @user_id) AND (modules.semester = 2)"
InsertCommand="INSERT INTO [students_vs_modules]([user_id], [module_id]) VALUES (@user_id, @module_id)">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="user_id" Name="user_id" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="module_id" Type="Int32" />
</InsertParameters>
</asp:sqldatasource>
<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:SqlDataSource ID="emergency_contact_list" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT relationship_id, relationship FROM relationship"></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" >
<EditItemTemplate>
<span style="">user_id:
<asp:TextBox Text='<%# Bind("user_id") %>' runat="server" ID="user_idTextBox" /><br />
f_name:
<asp:TextBox Text='<%# Bind("f_name") %>' runat="server" ID="f_nameTextBox" /><br />
l_name:
<asp:TextBox Text='<%# Bind("l_name") %>' runat="server" ID="l_nameTextBox" /><br />
em_contact_id:
<asp:Label Text='<%# Eval("em_contact_id") %>' runat="server" ID="em_contact_idLabel1" /><br />
relationship_id:
<asp:TextBox Text='<%# Bind("relationship_id") %>' runat="server" ID="relationship_idTextBox" /><br />
contact_name:
<asp:TextBox Text='<%# Bind("contact_name") %>' runat="server" ID="contact_nameTextBox" /><br />
phone_number:
<asp:TextBox Text='<%# Bind("phone_number") %>' runat="server" ID="phone_numberTextBox" /><br />
address_1:
<asp:TextBox Text='<%# Bind("address_1") %>' runat="server" ID="address_1TextBox" /><br />
address_2:
<asp:TextBox Text='<%# Bind("address_2") %>' runat="server" ID="address_2TextBox" /><br />
town_city:
<asp:TextBox Text='<%# Bind("town_city") %>' runat="server" ID="town_cityTextBox" /><br />
county:
<asp:TextBox Text='<%# Bind("county") %>' runat="server" ID="countyTextBox" /><br />
postcode:
<asp:TextBox Text='<%# Bind("postcode") %>' runat="server" ID="postcodeTextBox" /><br />
country:
<asp:TextBox Text='<%# Bind("country") %>' runat="server" ID="countryTextBox" /><br />
relationship:
<asp:TextBox Text='<%# Bind("relationship") %>' runat="server" ID="relationshipTextBox" /><br />
<asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" />
<asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
<br />
</span>
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">user_id:
<asp:TextBox Text='<%# Bind("user_id") %>' runat="server" ID="user_idTextBox" /><br />
f_name:
<asp:TextBox Text='<%# Bind("f_name") %>' runat="server" ID="f_nameTextBox" /><br />
l_name:
<asp:TextBox Text='<%# Bind("l_name") %>' runat="server" ID="l_nameTextBox" /><br />
relationship_id:
<asp:TextBox Text='<%# Bind("relationship_id") %>' runat="server" ID="relationship_idTextBox" /><br />
contact_name:
<asp:TextBox Text='<%# Bind("contact_name") %>' runat="server" ID="contact_nameTextBox" /><br />
phone_number:
<asp:TextBox Text='<%# Bind("phone_number") %>' runat="server" ID="phone_numberTextBox" /><br />
address_1:
<asp:TextBox Text='<%# Bind("address_1") %>' runat="server" ID="address_1TextBox" /><br />
address_2:
<asp:TextBox Text='<%# Bind("address_2") %>' runat="server" ID="address_2TextBox" /><br />
town_city:
<asp:TextBox Text='<%# Bind("town_city") %>' runat="server" ID="town_cityTextBox" /><br />
county:
<asp:TextBox Text='<%# Bind("county") %>' runat="server" ID="countyTextBox" /><br />
postcode:
<asp:TextBox Text='<%# Bind("postcode") %>' runat="server" ID="postcodeTextBox" /><br />
country:
<asp:TextBox Text='<%# Bind("country") %>' runat="server" ID="countryTextBox" /><br />
relationship:
<asp:TextBox Text='<%# Bind("relationship") %>' runat="server" ID="relationshipTextBox" /><br />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
<br />
</span>
</InsertItemTemplate>
<ItemTemplate>
<span style="">user_id:
<asp:Label Text='<%# Eval("user_id") %>' runat="server" ID="user_idLabel" /><br />
f_name:
<asp:Label Text='<%# Eval("f_name") %>' runat="server" ID="f_nameLabel" /><br />
l_name:
<asp:Label Text='<%# Eval("l_name") %>' runat="server" ID="l_nameLabel" /><br />
em_contact_id:
<asp:Label Text='<%# Eval("em_contact_id") %>' runat="server" ID="em_contact_idLabel" /><br />
relationship_id:
<asp:Label Text='<%# Eval("relationship_id") %>' runat="server" ID="relationship_idLabel" /><br />
contact_name:
<asp:Label Text='<%# Eval("contact_name") %>' runat="server" ID="contact_nameLabel" /><br />
phone_number:
<asp:Label Text='<%# Eval("phone_number") %>' runat="server" ID="phone_numberLabel" /><br />
address_1:
<asp:Label Text='<%# Eval("address_1") %>' runat="server" ID="address_1Label" /><br />
address_2:
<asp:Label Text='<%# Eval("address_2") %>' runat="server" ID="address_2Label" /><br />
town_city:
<asp:Label Text='<%# Eval("town_city") %>' runat="server" ID="town_cityLabel" /><br />
county:
<asp:Label Text='<%# Eval("county") %>' runat="server" ID="countyLabel" /><br />
postcode:
<asp:Label Text='<%# Eval("postcode") %>' runat="server" ID="postcodeLabel" /><br />
country:
<asp:Label Text='<%# Eval("country") %>' runat="server" ID="countryLabel" /><br />
relationship:
<asp:Label Text='<%# Eval("relationship") %>' runat="server" ID="relationshipLabel" /><br />
<asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
<br />
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
<div style="">
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">user_id:
<asp:Label Text='<%# Eval("user_id") %>' runat="server" ID="user_idLabel" /><br />
f_name:
<asp:Label Text='<%# Eval("f_name") %>' runat="server" ID="f_nameLabel" /><br />
l_name:
<asp:Label Text='<%# Eval("l_name") %>' runat="server" ID="l_nameLabel" /><br />
em_contact_id:
<asp:Label Text='<%# Eval("em_contact_id") %>' runat="server" ID="em_contact_idLabel" /><br />
relationship_id:
<asp:Label Text='<%# Eval("relationship_id") %>' runat="server" ID="relationship_idLabel" /><br />
contact_name:
<asp:Label Text='<%# Eval("contact_name") %>' runat="server" ID="contact_nameLabel" /><br />
phone_number:
<asp:Label Text='<%# Eval("phone_number") %>' runat="server" ID="phone_numberLabel" /><br />
address_1:
<asp:Label Text='<%# Eval("address_1") %>' runat="server" ID="address_1Label" /><br />
address_2:
<asp:Label Text='<%# Eval("address_2") %>' runat="server" ID="address_2Label" /><br />
town_city:
<asp:Label Text='<%# Eval("town_city") %>' runat="server" ID="town_cityLabel" /><br />
county:
<asp:Label Text='<%# Eval("county") %>' runat="server" ID="countyLabel" /><br />
postcode:
<asp:Label Text='<%# Eval("postcode") %>' runat="server" ID="postcodeLabel" /><br />
country:
<asp:Label Text='<%# Eval("country") %>' runat="server" ID="countryLabel" /><br />
relationship:
<asp:Label Text='<%# Eval("relationship") %>' runat="server" ID="relationshipLabel" /><br />
<asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
<br />
<br />
</span>
</SelectedItemTemplate>
</asp:ListView>
<asp:Button CommandName="NextView" ID="em_contact_next" runat="server" Text="Next" />
</asp:View>
<asp:View ID="semester_1_view" runat="server">
<h1>Semester 1</h1>
<asp:DataList ID="semester_1_datalist" runat="server" DataSourceID="semester1">
<ItemTemplate>
module_name:
<asp:Label ID="module_nameLabel" runat="server" Text='<%# Eval("module_name") %>' />
<br />
year:
<asp:Label ID="yearLabel" runat="server" Text='<%# Eval("year") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<script type="text/javascript">
var limit = 3;
$(function () {
$('[id*="module_semester_1"]').on('change', function (evt) {
if ($('[id*="module_semester_1"]:checked').length > limit) {
this.checked = true;
alert('cannot select more than ' + limit);
}
});
});
</script>
<asp:CheckBoxList ID="module_semester_1" runat="server" DataSourceID="semester1" DataTextField="module_name" DataValueField="module_id" ></asp:CheckBoxList>
<asp:Button CommandName="NextView" ID="btnnext2" runat="server" Text="Next" OnClick="btnnext2_Click" />
</asp:View>
<asp:View ID="semester_2_view" runat="server">
<h1>Semester 2</h1>
<div runat="server">
<asp:CheckBoxList ID="moduleselect" runat="server" DataSourceID="semester2" DataTextField="module_name" DataValueField="module_id" ></asp:CheckBoxList>
<asp:DataList ID="semester_2_list" runat="server" DataSourceID="semester2">
<ItemTemplate>
module_name:
<asp:Label ID="module_nameLabel" runat="server" Text='<%# Eval("cats_points") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<br />
<asp:Button ID="uploadbutton" runat="server" Text="Choose Modules" OnClick="uploadbutton_Click" CssClass="submitbtn" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" CssClass="submitbtn" />
</div></asp:View></asp:MultiView>
</div>
</div>
</asp:Content>
我正在尝试使用C#更新数据库表中的字段,我有以下C#代码,但是我收到的错误声明TextBox控件contact_nameTextBox
在当前上下文中不存在而我不是确定为什么,非常感谢任何帮助。
protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
{
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
string Name_data = contact_nameTextBox.Text;
答案 0 :(得分:0)
可能是因为您试图从受保护的虚空中访问它,您需要创建它的实例
protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
{
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
contact_nameTextBox.Text = new textBox();
string Name_data = contact_nameTextBox.Text;
答案 1 :(得分:0)
protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
{
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
ListView parent = (ListView)sender;
if(parent != null)
var contact_nameTextBox = (TextBox)(parent.FindName("contact_nameTextBox"));
string contact_Name = contact_nameTextBox.Text;
//Your code
}
我还没有测试过这段代码,但希望通过一些小改动来实现它。