The ID is inside the ItemTemplate and it cannot be found in the code file.
<form id="form1" runat="server">
<asp:SqlDataSource ID="searchresults" runat="server"
ConnectionString='<%$ ConnectionStrings:AgileDatabaseConnection %>'
SelectCommand=" SELECT [userID], [userName], [firstName],[lastName],[password], [email] FROM [Users] WHERE ([email] LIKE '%' + #email + '%')">
<selectparameters>
<asp:querystringparameter querystringfield="searchquery" name="email" type="String"></asp:querystringparameter>
</selectparameters>
</asp:SqlDataSource>
<asp:ListView ID="displayitems" runat="server" DataSourceID="searchresults">
<itemtemplate>
<asp:label runat="server" associatedcontrolid="projectOwner" cssclass="col-md-2 control-label">Project Owner:</asp:label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="ProjectOwner" CssClass="form-control" /><br />
<asp:label runat="server" associatedcontrolid="projectOwner" cssclass="col-md-2 control-label">Scrum Master:</asp:label>
<div class="col-md-10">
<asp:SqlDataSource ID="ScrumMaster" runat="server" ConnectionString='<%$ ConnectionStrings:AgileDatabaseConnection %>' SelectCommand="SELECT userName FROM [Users]"></asp:SqlDataSource>
<asp:dropdownlist runat="server" id="usertype" DataSourceID="ScrumMaster" DataTextField="userName"></asp:dropdownlist><br />
</div>
</div>
<asp:label runat="server" cssclass="col-md-2 control-label">Email:</asp:label>
<asp:Label Text='<%#Eval("email") %>' runat="server" ID="emaillabel" /><br />
<asp:label runat="server" cssclass="col-md-2 control-label">UserName:</asp:label>
<asp:Label Text='<%#Eval("userName") %>' runat="server" ID="username" /><br />
<asp:label runat="server" cssclass="col-md-2 control-label">UserID:</asp:label>
<asp:Label runat="server" Text='<%#Eval("userID") %>' ID="UserID" CssClass="form-control" />
<br />
<div class="actions"></div>
<asp:Button Text="Add" runat="server" class="btn pull-right" ID="uploadbutton" OnClick="add_Click"></asp:Button>
</div>
</itemtemplate>
</asp:ListView>
<emptydatatemplate>
<span>No users match <asp:Label Text='' runat="server" ID="email" /> .</span>
</emptydatatemplate>
</form>
C# says it can't find 'userName'. Here's my backend code:
string ownerName = ProjectOwner.Text;
string IDuser = username.Text;
string IDdata = Session["userID"].ToString();
How do I get the userName value through?
The problem is that ListView can return more then one item, so code behind doesn't know about which of the itens controls (username and ProjectOwner) are you talking about.
If you want to do the same thing for EACH item, you can do like this:
protected void displayitems_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
string ownerName = ((TextBox)e.Item.FindControl("ProjectOwner")).Text;
string IDuser = ((Label)e.Item.FindControl("username").Text;
string IDdata = Session["userID"].ToString();
}
}
Related
I want to bind a image (which is stored in binary format column name image(varbinary)) in list view in c#..
i am trying this two but not working..
<img src='<%# string.Format("data:image/gif;base64,{0}",Convert.ToBase64String((byte[])Eval("image")))%>'/>--%>
<%--<asp:Image ID="Image10" runat="server" Height="100px" ImageUrl='<%# "ShowImage.ashx?id=" + Eval("image") %>' GenerateEmptyAlternateText="True" Visible='<%# ShowImg(Eval("image")) %>' />--%>
here is the code:
<div>
<h3 class="calDetail">Event Details</h3>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VeteransDirectoryConnectionString %>" SelectCommand=
"SELECT [EventID], [OrganizationName], [Title], [DescriptionShort], [DescriptionLong], [Location], [EventDate], [URL], (CASE WHEN URL IS NULL OR URL = '' THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END) AS URLLink, [FileName], (CASE WHEN FileName IS NULL OR FileName = '' THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END) AS FileLink, [EventTime],image FROM [t_Event] WHERE ([EventID] = #EventID)">
<SelectParameters>
<asp:QueryStringParameter Name="EventID" QueryStringField="eventid" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<div style="padding: 0px 10px 20px 10px;">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="EventID" DataSourceID="SqlDataSource1">
<EmptyDataTemplate>
<span>No data for this event was found.</span>
</EmptyDataTemplate>
<ItemTemplate>
<span class="listCalLabel">Organization:</span>
<asp:Label ID="OrganizationNameLabel" runat="server" Text='<%# Eval("OrganizationName") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Event:</span>
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Short Description:</span>
<asp:Label ID="DescriptionShortLabel" runat="server" Text='<%# Eval("DescriptionShort") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Long Description:</span>
<asp:Label ID="DescriptionLongLabel" runat="server" Text='<%# Eval("DescriptionLong") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Location:</span>
<asp:Label ID="LocationLabel" runat="server" Text='<%# Eval("Location") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Event Date:</span>
<asp:Label ID="EventDateLabel" runat="server" Text='<%# Eval("EventDate", "{0:MM/dd/yyyy}") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Event Time:</span>
<asp:Label ID="EventTimeLabel" runat="server" Text='<%# Eval("EventTime") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel"><asp:Label ID="lblURL" runat="server" Visible='<%# Eval("URLLink") %>' Text="Additional Info (website):" ></asp:Label></span>
<br />
<asp:HyperLink ID="URLHyperLink" runat="server" Text='<%# Eval("URL") %>' NavigateUrl='<%# Eval("URL") %>' Visible='<%# Eval("URLLink") %>' Target="_blank" CssClass="listLink" />
<br /><br />
<span class="listCalLabel"><asp:Label ID="lblFile" runat="server" Visible='<%# Eval("FileLink") %>' Text="Additional Info (file):"></asp:Label></span>
<br />
<asp:HyperLink ID="URLFile" runat="server" Text='<%# Eval("FileName") %>' NavigateUrl='<%# "EventFiles/" + Eval("FileName") %>' Visible='<%# Eval("FileLink") %>' Target="_blank" CssClass="listLink" />
<br /><br />
<%-- <img src='<%# string.Format("data:image/gif;base64,{0}",Convert.ToBase64String((byte[])Eval("image")))%>'/>--%>
<%--<asp:Image ID="Image10" runat="server" Height="100px" ImageUrl='<%# "ShowImage.ashx?id=" + Eval("image") %>' GenerateEmptyAlternateText="True" Visible='<%# ShowImg(Eval("image")) %>' />--%>
<br /></span>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style="">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<asp:Button ID="btnOK" runat="server" OnClientClick="window.close(); return false;" Text="Close Event Detail" CssClass="btn btn-default" />
<br />
<br />
</div>
</div>
what should i do???
how to retrieve the image and show ina a webform ??
You can use handler to display image in gridview , your html markup look like inside
Gridview ItemTemplate set image control src as src=~/getImage.ashx?id=" + id
where getImage.ashx is your handler which return MemoryStream((byte[])img);
for more details read http://www.aspdotnet-suresh.com/2015/07/save-retrieve-display-binary-images-in-gridview-from-database-in-aspnet-csharp-vbnet.html
I am trying to allow a user of a buy and sell website to only be able to edit the products that they have put up for sale. I have got that, but the result is now that it shows duplicates. I am new to Stack Overflow and asp.net so apologies if I am not following the correct procedures.
<asp:SqlDataSource ID="itemsjoin" runat="server" ConnectionString = '<%$ConnectionStrings:itemsconnection%>' SelectCommand="SELECT itemsupdate.ID, itemsupdate.itemname, itemsupdate.price, itemsupdate.image, itemsupdate.description, itemsupdate.location, itemsupdate.productID, itemsupdate.userID, producttype.productname
FROM producttype, users, itemsupdate
WHERE ([userID] = #userID)">
<SelectParameters>
<asp:QueryStringParameter Name="userID" QueryStringField="userID" Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView ID="itemsdisplay" runat="server" DataSourceID = "itemsjoin" DataKeyNames="ID">
<ItemTemplate>
<div class="col-md-4">
<div class="itemtext">
</br>
<asp:Label ID="itemname" runat="server" Text='<%# Eval("itemname") %>'></asp:Label></br>
<asp:Label ID="itemprice" runat="server" Text='<%# Eval("price") %>'></asp:Label>
<asp:HyperLink ID="itemhyperlink" runat="server" NavigateUrl='<%#"itemediting.aspx?itemid="+Eval("ID")%>'>
<div class="imgright">
<asp:Image ID="imagepic" runat="server" ImageURL='<%# "../../files/"+Eval("image") %>' width="200px" /></asp:HyperLink>
</div>
</div>
</div>
</ItemTemplate>
</asp:ListView>
Just add DISTINCT in your query
SELECT DISTINCT itemsupdate.ID, itemsupdate.itemname, itemsupdate.price, itemsupdate.image, itemsupdate.description, itemsupdate.location, itemsupdate.productID, itemsupdate.userID, producttype.productname
FROM producttype, users, itemsupdate
WHERE ([userID] = #userID)
The name 'Label2.Text' does not exist in the current context
Catalog.aspx
<asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatLayout="Flow">
<ItemTemplate>
<div class="Item">
<div class="name">
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</div>
<div>
Код:<asp:Label ID="Label2" runat="server" Text='<%# Eval("Id") %>' />
</div>
<img src="<%# Eval("Image") %>" height="115" alt="item"/>
<div class="price">
Цена:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price")%>' />p.
<asp:Button ID="Button2" runat="server" ForeColor="Black"
onclick="Button2_Click" Text="В КОРЗИНУ" />
</div>
<div class="desc">
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
</div>
</div>
</ItemTemplate>
</asp:DataList>
Catalog.aspx.cs
sqlCon.Open();
SqlCommand cmd_SQL = new SqlCommand("INSERT INTO Cart(ClientId,ProductId,Amount) VALUES (#ClientId,#ProductId,#Amount)", sqlCon);
cmd_SQL.Parameters.Add("#ClientId", SqlDbType.NVarChar).Value =Membership.GetUser().ProviderUserKey.ToString();
cmd_SQL.Parameters.Add("#ProductId", SqlDbType.NVarChar).Value =Label2.Text;
cmd_SQL.Parameters.Add("#Amount", SqlDbType.NVarChar).Value = 1;
cmd_SQL.CommandType = CommandType.Text;
cmd_SQL.ExecuteNonQuery();
The name 'Label2.Text' does not exist in the current context
Your label is inside of an item template, which means you cannot just access it any old place on the page. If you want to gain access to the value inside the label, then you need to tie into one of the DataList events, such as OnItemCommand (if you wanted to access the value as the result of a command button click, for instance). You can use FindControl inside of the event handler to access the value. For example:
<asp:DataList runat="server" ID="test" OnItemCommand="test_ItemCommand">
<ItemTemplate>
<asp:Label runat="server" ID="Label2" Text="Test" />
</ItemTemplate>
</asp:DataList>
protected void test_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if (e.Item != null)
{
var label2 = e.Item.FindControl("Label2");
if (label2 != null && label2 is Label)
{
var productID = ((Label)label2).Text;
// now you have the contents of the label's text property
}
}
}
In my .aspx page I have the following code in my ListView control;
<%# 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>
I'm trying to update fields in the database table using C#, I have the following C# code, however I am receiving the error stating that TextBox control contact_nameTextBox does not exist in current context and I'm not sure why, any help would be greatly appreciated.
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;
Probably because you´re trying to access it from a protected void, you need to create an instace of it
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;
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
}
I have not tested this code, but hope with some small changes it should work.
I got a gridview to show user data and a item hyperlink when click will populate the data on below table for admin to update.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<fieldset>
<div style="float: left;">
<h2>
Members</h2>
</div>
<div style="float: right; padding-right: 30px;">
Create Member
</div>
<div style="clear: both;" />
<hr />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" DataKeyNames="UserID" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Horizontal" Width="886px" EnableSortingAndPagingCallbacks="true">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "~/member.aspx?UserID="+ DataBinder.Eval(Container.DataItem,"UserID") %>'
Text='View'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
</ContentTemplate>
<Triggers>
<div class="formlabel">
Index :</div>
<div class="formlabel">
<asp:Label ID="userID" runat="server" Text=""></asp:Label></div>
<br />
<div class="formlabel">
Login Name :</div>
<asp:TextBox ID="username" runat="server" MaxLength="40"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="username" ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Username can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" runat="server" TargetControlID="RequiredFieldValidator1" />
<asp:CustomValidator ID="CustomValidator6" runat="server" ControlToValidate="username"
Display="None" ErrorMessage="Username already exists." ClientValidationFunction="CheckUniqueness"
OnServerValidate="UserNameExistsValidator_ServerValidate"></asp:CustomValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender14" runat="server" PopupPosition="Right"
TargetControlID="CustomValidator6">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
Password :</div>
<asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="password" ID="RequiredFieldValidator2"
runat="server" ErrorMessage="Password can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" runat="server" TargetControlID="RequiredFieldValidator2">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
First Name :</div>
<asp:TextBox ID="firstname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="firstname" ID="RequiredFieldValidator3"
runat="server" ErrorMessage="First Name can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender3" runat="server" TargetControlID="RequiredFieldValidator3">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
Last Name :</div>
<asp:TextBox ID="lastname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="lastname" ID="RequiredFieldValidator4"
runat="server" ErrorMessage="Last Name can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender4" runat="server" TargetControlID="RequiredFieldValidator4">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
Email :</div>
<asp:TextBox ID="email" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="lastname" ID="RequiredFieldValidator5"
runat="server" ErrorMessage="Email can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender5" runat="server" TargetControlID="RequiredFieldValidator5">
</cc1:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="REVEmail" runat="server" ControlToValidate="email"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Invalid Email"
Display="None"></asp:RegularExpressionValidator>
<cc1:ValidatorCalloutExtender ID="REVEmail_ValidatorCalloutExtender" runat="server"
TargetControlID="REVEmail">
</cc1:ValidatorCalloutExtender>
<br />
<br />
<asp:Label ID="msg" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Update" />
<br />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
Below are my backend code:
if (Page.IsValid)
{
string id = Request.QueryString["UserID"];
string firstnamevalue = firstname.Text;
string lastnamevalue = lastname.Text;
string emailvalue = email.Text;
MySqlConnection connect = new MySqlConnection(connectionString);
MySqlCommand command = connect.CreateCommand();
command.CommandText = "Update user set Email = '" + emailvalue + "', FirstName ='" + firstnamevalue + "' , LastName ='" + lastnamevalue + "' WHERE UserID =" + id;
try
{
connect.Open();
command.Connection = connect;
command.ExecuteNonQuery();
GridView1.DataBind();
UpdatePanel2.Update();
}
catch
{
msg.Text = "Error Occured";
}
finally
{
connect.Close();
}
}
When I tried update the gridview will gone, may I know what is with my code?