I have a listview control databound to a database in my ASP project and I have it set so that it can be edited. The listview is inside a multiview and it loads the information perfectly but clicking on the edit button doesn't do anything. The listview is in an update panel which I though might be part of it but putting the listview outside the update panel just causes the multivew to change view back the default page load view.
I have a perfectly functional editable listview in a previous project in almost identical circumstances and I just can't figure out what's made this one not work. I've tried deleting and remaking but this problem seems to be affecting any listview I make in the multiview. On other listviews where I've implemented the 'insert' feature, the insert button does not do anything either.
My code is below:
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView2" runat="server" DataKeyNames="StudentID"
DataSourceID="SqlDataSource10" GroupItemCount="3">
<AlternatingItemTemplate>
<td runat="server" style="background-color: #FFFFFF;color: #284775;">
StudentID:
<asp:Label ID="StudentIDLabel" runat="server" Text='<%# Eval("StudentID") %>' />
<br />
CurrentSchoolID:
<asp:Label ID="CurrentSchoolIDLabel" runat="server"
Text='<%# Eval("CurrentSchoolID") %>' />
<br />
Gender:
<asp:Label ID="GenderLabel" runat="server" Text='<%# Eval("Gender") %>' />
<br />
Fname:
<asp:Label ID="FnameLabel" runat="server" Text='<%# Eval("Fname") %>' />
<br />
Lname:
<asp:Label ID="LnameLabel" runat="server" Text='<%# Eval("Lname") %>' />
<br />
ParentCarer:
<asp:Label ID="ParentCarerLabel" runat="server"
Text='<%# Eval("ParentCarer") %>' />
<br />
StAddress:
<asp:Label ID="StAddressLabel" runat="server" Text='<%# Eval("StAddress") %>' />
<br />
suburb:
<asp:Label ID="suburbLabel" runat="server" Text='<%# Eval("suburb") %>' />
<br />
postCode:
<asp:Label ID="postCodeLabel" runat="server" Text='<%# Eval("postCode") %>' />
<br />
DOB:
<asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("DOB") %>' />
<br />
homePhone:
<asp:Label ID="homePhoneLabel" runat="server" Text='<%# Eval("homePhone") %>' />
<br />
MobilePhone:
<asp:Label ID="MobilePhoneLabel" runat="server"
Text='<%# Eval("MobilePhone") %>' />
<br />
WorkPhone:
<asp:Label ID="WorkPhoneLabel" runat="server" Text='<%# Eval("WorkPhone") %>' />
<br />
email:
<asp:Label ID="emailLabel" runat="server" Text='<%# Eval("email") %>' />
<br />
UserID:
<asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
<br />
Username:
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</AlternatingItemTemplate>
<EditItemTemplate>
<td runat="server" style="background-color: #999999;">
StudentID:
<asp:Label ID="StudentIDLabel1" runat="server"
Text='<%# Eval("StudentID") %>' />
<br />
CurrentSchoolID:
<asp:TextBox ID="CurrentSchoolIDTextBox" runat="server"
Text='<%# Bind("CurrentSchoolID") %>' />
<br />
Gender:
<asp:TextBox ID="GenderTextBox" runat="server" Text='<%# Bind("Gender") %>' />
<br />
Fname:
<asp:TextBox ID="FnameTextBox" runat="server" Text='<%# Bind("Fname") %>' />
<br />
Lname:
<asp:TextBox ID="LnameTextBox" runat="server" Text='<%# Bind("Lname") %>' />
<br />
ParentCarer:
<asp:TextBox ID="ParentCarerTextBox" runat="server"
Text='<%# Bind("ParentCarer") %>' />
<br />
StAddress:
<asp:TextBox ID="StAddressTextBox" runat="server"
Text='<%# Bind("StAddress") %>' />
<br />
suburb:
<asp:TextBox ID="suburbTextBox" runat="server" Text='<%# Bind("suburb") %>' />
<br />
postCode:
<asp:TextBox ID="postCodeTextBox" runat="server"
Text='<%# Bind("postCode") %>' />
<br />
DOB:
<asp:TextBox ID="DOBTextBox" runat="server" Text='<%# Bind("DOB") %>' />
<br />
homePhone:
<asp:TextBox ID="homePhoneTextBox" runat="server"
Text='<%# Bind("homePhone") %>' />
<br />
MobilePhone:
<asp:TextBox ID="MobilePhoneTextBox" runat="server"
Text='<%# Bind("MobilePhone") %>' />
<br />
WorkPhone:
<asp:TextBox ID="WorkPhoneTextBox" runat="server"
Text='<%# Bind("WorkPhone") %>' />
<br />
email:
<asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>' />
<br />
UserID:
<asp:TextBox ID="UserIDTextBox" runat="server" Text='<%# Bind("UserID") %>' />
<br />
Username:
<asp:TextBox ID="UsernameTextBox" runat="server"
Text='<%# Bind("Username") %>' />
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
<br />
</td>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr ID="itemPlaceholderContainer" runat="server">
<td ID="itemPlaceholder" runat="server">
</td>
</tr>
</GroupTemplate>
<InsertItemTemplate>
<td runat="server" style="">
CurrentSchoolID:
<asp:TextBox ID="CurrentSchoolIDTextBox" runat="server"
Text='<%# Bind("CurrentSchoolID") %>' />
<br />
Gender:
<asp:TextBox ID="GenderTextBox" runat="server" Text='<%# Bind("Gender") %>' />
<br />
Fname:
<asp:TextBox ID="FnameTextBox" runat="server" Text='<%# Bind("Fname") %>' />
<br />
Lname:
<asp:TextBox ID="LnameTextBox" runat="server" Text='<%# Bind("Lname") %>' />
<br />
ParentCarer:
<asp:TextBox ID="ParentCarerTextBox" runat="server"
Text='<%# Bind("ParentCarer") %>' />
<br />
StAddress:
<asp:TextBox ID="StAddressTextBox" runat="server"
Text='<%# Bind("StAddress") %>' />
<br />
suburb:
<asp:TextBox ID="suburbTextBox" runat="server" Text='<%# Bind("suburb") %>' />
<br />
postCode:
<asp:TextBox ID="postCodeTextBox" runat="server"
Text='<%# Bind("postCode") %>' />
<br />
DOB:
<asp:TextBox ID="DOBTextBox" runat="server" Text='<%# Bind("DOB") %>' />
<br />
homePhone:
<asp:TextBox ID="homePhoneTextBox" runat="server"
Text='<%# Bind("homePhone") %>' />
<br />
MobilePhone:
<asp:TextBox ID="MobilePhoneTextBox" runat="server"
Text='<%# Bind("MobilePhone") %>' />
<br />
WorkPhone:
<asp:TextBox ID="WorkPhoneTextBox" runat="server"
Text='<%# Bind("WorkPhone") %>' />
<br />
email:
<asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>' />
<br />
UserID:
<asp:TextBox ID="UserIDTextBox" runat="server" Text='<%# Bind("UserID") %>' />
<br />
Username:
<asp:TextBox ID="UsernameTextBox" runat="server"
Text='<%# Bind("Username") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
<br />
</td>
</InsertItemTemplate>
<ItemTemplate>
<td runat="server" style="background-color: #E0FFFF;color: #333333;">
StudentID:
<asp:Label ID="StudentIDLabel" runat="server" Text='<%# Eval("StudentID") %>' />
<br />
CurrentSchoolID:
<asp:Label ID="CurrentSchoolIDLabel" runat="server"
Text='<%# Eval("CurrentSchoolID") %>' />
<br />
Gender:
<asp:Label ID="GenderLabel" runat="server" Text='<%# Eval("Gender") %>' />
<br />
Fname:
<asp:Label ID="FnameLabel" runat="server" Text='<%# Eval("Fname") %>' />
<br />
Lname:
<asp:Label ID="LnameLabel" runat="server" Text='<%# Eval("Lname") %>' />
<br />
ParentCarer:
<asp:Label ID="ParentCarerLabel" runat="server"
Text='<%# Eval("ParentCarer") %>' />
<br />
StAddress:
<asp:Label ID="StAddressLabel" runat="server" Text='<%# Eval("StAddress") %>' />
<br />
suburb:
<asp:Label ID="suburbLabel" runat="server" Text='<%# Eval("suburb") %>' />
<br />
postCode:
<asp:Label ID="postCodeLabel" runat="server" Text='<%# Eval("postCode") %>' />
<br />
DOB:
<asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("DOB") %>' />
<br />
homePhone:
<asp:Label ID="homePhoneLabel" runat="server" Text='<%# Eval("homePhone") %>' />
<br />
MobilePhone:
<asp:Label ID="MobilePhoneLabel" runat="server"
Text='<%# Eval("MobilePhone") %>' />
<br />
WorkPhone:
<asp:Label ID="WorkPhoneLabel" runat="server" Text='<%# Eval("WorkPhone") %>' />
<br />
email:
<asp:Label ID="emailLabel" runat="server" Text='<%# Eval("email") %>' />
<br />
UserID:
<asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
<br />
Username:
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="groupPlaceholderContainer" runat="server" border="1"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<td runat="server"
style="background-color: #E2DED6;font-weight: bold;color: #333333;">
StudentID:
<asp:Label ID="StudentIDLabel" runat="server" Text='<%# Eval("StudentID") %>' />
<br />
CurrentSchoolID:
<asp:Label ID="CurrentSchoolIDLabel" runat="server"
Text='<%# Eval("CurrentSchoolID") %>' />
<br />
Gender:
<asp:Label ID="GenderLabel" runat="server" Text='<%# Eval("Gender") %>' />
<br />
Fname:
<asp:Label ID="FnameLabel" runat="server" Text='<%# Eval("Fname") %>' />
<br />
Lname:
<asp:Label ID="LnameLabel" runat="server" Text='<%# Eval("Lname") %>' />
<br />
ParentCarer:
<asp:Label ID="ParentCarerLabel" runat="server"
Text='<%# Eval("ParentCarer") %>' />
<br />
StAddress:
<asp:Label ID="StAddressLabel" runat="server" Text='<%# Eval("StAddress") %>' />
<br />
suburb:
<asp:Label ID="suburbLabel" runat="server" Text='<%# Eval("suburb") %>' />
<br />
postCode:
<asp:Label ID="postCodeLabel" runat="server" Text='<%# Eval("postCode") %>' />
<br />
DOB:
<asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("DOB") %>' />
<br />
homePhone:
<asp:Label ID="homePhoneLabel" runat="server" Text='<%# Eval("homePhone") %>' />
<br />
MobilePhone:
<asp:Label ID="MobilePhoneLabel" runat="server"
Text='<%# Eval("MobilePhone") %>' />
<br />
WorkPhone:
<asp:Label ID="WorkPhoneLabel" runat="server" Text='<%# Eval("WorkPhone") %>' />
<br />
email:
<asp:Label ID="emailLabel" runat="server" Text='<%# Eval("email") %>' />
<br />
UserID:
<asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
<br />
Username:
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource10" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Student] WHERE [StudentID] = #StudentID"
InsertCommand="INSERT INTO [Student] ([CurrentSchoolID], [Gender], [Fname], [Lname], [ParentCarer], [StAddress], [suburb], [postCode], [DOB], [homePhone], [MobilePhone], [WorkPhone], [email], [UserID], [Username]) VALUES (#CurrentSchoolID, #Gender, #Fname, #Lname, #ParentCarer, #StAddress, #suburb, #postCode, #DOB, #homePhone, #MobilePhone, #WorkPhone, #email, #UserID, #Username)"
SelectCommand="SELECT * FROM [Student] WHERE ([Username] = #Username)"
UpdateCommand="UPDATE [Student] SET [CurrentSchoolID] = #CurrentSchoolID, [Gender] = #Gender, [Fname] = #Fname, [Lname] = #Lname, [ParentCarer] = #ParentCarer, [StAddress] = #StAddress, [suburb] = #suburb, [postCode] = #postCode, [DOB] = #DOB, [homePhone] = #homePhone, [MobilePhone] = #MobilePhone, [WorkPhone] = #WorkPhone, [email] = #email, [UserID] = #UserID, [Username] = #Username WHERE [StudentID] = #StudentID">
<DeleteParameters>
<asp:Parameter Name="StudentID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="CurrentSchoolID" Type="Int32" />
<asp:Parameter Name="Gender" Type="String" />
<asp:Parameter Name="Fname" Type="String" />
<asp:Parameter Name="Lname" Type="String" />
<asp:Parameter Name="ParentCarer" Type="String" />
<asp:Parameter Name="StAddress" Type="String" />
<asp:Parameter Name="suburb" Type="String" />
<asp:Parameter Name="postCode" Type="Int32" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="homePhone" Type="String" />
<asp:Parameter Name="MobilePhone" Type="String" />
<asp:Parameter Name="WorkPhone" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="UserID" Type="Int32" />
<asp:Parameter Name="Username" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="Username" SessionField="Username" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="CurrentSchoolID" Type="Int32" />
<asp:Parameter Name="Gender" Type="String" />
<asp:Parameter Name="Fname" Type="String" />
<asp:Parameter Name="Lname" Type="String" />
<asp:Parameter Name="ParentCarer" Type="String" />
<asp:Parameter Name="StAddress" Type="String" />
<asp:Parameter Name="suburb" Type="String" />
<asp:Parameter Name="postCode" Type="Int32" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="homePhone" Type="String" />
<asp:Parameter Name="MobilePhone" Type="String" />
<asp:Parameter Name="WorkPhone" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="UserID" Type="Int32" />
<asp:Parameter Name="Username" Type="String" />
<asp:Parameter Name="StudentID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
C# Code:
protected void Page_Load(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
if (!Page.IsPostBack)
{
if (User.IsInRole("Student"))
{
MultiView1.ActiveViewIndex = 2;
}
else if (User.IsInRole("Staff"))
{
MultiView1.ActiveViewIndex = 6;
}
else if (User.IsInRole("Admin"))
{
MultiView1.ActiveViewIndex = 7;
}
}
}
changed my code on page load to:
if(!User.Identity.IsAuthenticated){
MultiView1.ActiveViewIndex = 0;
}
Now works fine
Related
I use Visual Studio 2015 with a WebForm Webapplication.
I have add a ListView and a SQLDataSource.
The SQLDataSource is connected to a SQL-Server.
Using the ListView operations like Edit, Insert and Delete are working.
I have a textbox where the user can enter a City-Name and press a button that will update the SQLDataSource with a SQL-Query
With this I can search/filter for City-Names.
This works.
But if I click the "Edit" button after a search the search result in the Listview gets removed and all records are displayed
So I can not use "edit" after I have done a search.
How can I run a search and edit one of the found records in the ListView?
.apsx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="frmMain.aspx.cs" Inherits="DB_mit_GridView.frmMain" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Stadt"></asp:Label>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Suche" />
</div>
<asp:GridView ID="GridViewDatenAnzeigen" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" PageSize="5">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
</Columns>
</asp:GridView>
<br />
<asp:ListView ID="ListView1" runat="server" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="ListView1_SelectedIndexChanged" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>
<td>
<asp:Label ID="CustomerNameLabel" runat="server" Text='<%# Eval("CustomerName") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Label ID="PostalCodeLabel" runat="server" Text='<%# Eval("PostalCode") %>' />
</td>
<td>
<asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="CustomerIDLabel1" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
</td>
<td>
<asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
</td>
<td>
<asp:TextBox ID="PostalCodeTextBox" runat="server" Text='<%# Bind("PostalCode") %>' />
</td>
<td>
<asp:TextBox ID="CountryTextBox" runat="server" Text='<%# Bind("Country") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="CustomerIDTextBox" runat="server" Text='<%# Bind("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
</td>
<td>
<asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
</td>
<td>
<asp:TextBox ID="PostalCodeTextBox" runat="server" Text='<%# Bind("PostalCode") %>' />
</td>
<td>
<asp:TextBox ID="CountryTextBox" runat="server" Text='<%# Bind("Country") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>
<td>
<asp:Label ID="CustomerNameLabel" runat="server" Text='<%# Eval("CustomerName") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Label ID="PostalCodeLabel" runat="server" Text='<%# Eval("PostalCode") %>' />
</td>
<td>
<asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server"></th>
<th runat="server">CustomerID</th>
<th runat="server">CustomerName</th>
<th runat="server">City</th>
<th runat="server">PostalCode</th>
<th runat="server">Country</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server" >
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>
<td>
<asp:Label ID="CustomerNameLabel" runat="server" Text='<%# Eval("CustomerName") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Label ID="PostalCodeLabel" runat="server" Text='<%# Eval("PostalCode") %>' />
</td>
<td>
<asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="SELECT * FROM [Customers]" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = #original_CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CustomerName], [City], [PostalCode], [Country]) VALUES (#CustomerID, #CustomerName, #City, #PostalCode, #Country)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Customers] SET [CustomerName] = #CustomerName, [City] = #City, [PostalCode] = #PostalCode, [Country] = #Country WHERE [CustomerID] = #original_CustomerID">
<DeleteParameters>
<asp:Parameter Name="original_CustomerID" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="CustomerName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CustomerName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="original_CustomerID" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
</form>
</body>
</html>
.aspx.cs
public partial class frmMain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
//Search is done here
SqlDataSource1.SelectCommand = "SELECT* FROM[Customers] where City = '" + txtCity.Text +"'";
}
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
just chage the datasource select command and add select parameter
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="SELECT * FROM [Customers] WHERE City LiKE #City + '%'" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = #original_CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CustomerName], [City], [PostalCode], [Country]) VALUES (#CustomerID, #CustomerName, #City, #PostalCode, #Country)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Customers] SET [CustomerName] = #CustomerName, [City] = #City, [PostalCode] = #PostalCode, [Country] = #Country WHERE [CustomerID] = #original_CustomerID">
<DeleteParameters>
<asp:Parameter Name="original_CustomerID" Type="String" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="txtCity" Name="City" PropertyName="Text" Type="String" DefaultValue="%" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="CustomerName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CustomerName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="original_CustomerID" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
and after that remove the code behinde select query
protected void btnSearch_Click(object sender, EventArgs e)
{
//Search is done here
// SqlDataSource1.SelectCommand = "SELECT* FROM[Customers] where City = '" + txtCity.Text +"'";
}
I am trying to use a listview to display and update marks.
The data displays without any issues, however when I click edit, then insert data and try to save the changes, the listview fails to do so and loads in the same state is was prior to the update attempt. Any ideas what is going on?
<asp:SqlDataSource ID="StudentResultsDS" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
SelectCommand="SELECT Module_Info.ModuleTitle, Module_Info.UserID, Module_Info.ModuleCode, Modules.ModuleMarks, Modules.MarkID, Modules.ExamMark, Modules.AssignmentMark, Users.SNumber, Lecturer_Records.UserID AS Expr1 FROM Module_Info INNER JOIN Modules ON Module_Info.ModuleID = Modules.ModuleID INNER JOIN Users ON Modules.UserID = Users.UserID INNER JOIN Lecturer_Records ON Users.UserID = Lecturer_Records.UserID WHERE (Lecturer_Records.UserID = #UserID)" UpdateCommand="UPDATE [Modules] SET [ModuleMarks]=#ModuleMarks WHERE [MarkID]=#MarkID" >
<UpdateParameters>
<asp:FormParameter Name="ModuleMarks" Type="String" />
<asp:FormParameter Name="MarkID" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:SessionParameter Name="UserID" SessionField="grabUserID" />
</SelectParameters>
</asp:SqlDataSource>
<!-- list view to display results-->
<header class="jumbotron hero-spacer">
<asp:ListView runat="server" ID="listviewid" DataSourceID="StudentResultsDS" DataKeyNames="ModuleMarks">
<AlternatingItemTemplate>
<span style="">ModuleTitle:
<asp:Label ID="ModuleTitleLabel" runat="server" Text='<%# Eval("ModuleTitle") %>' />
<br />
UserID:
<asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
<br />
ModuleCode:
<asp:Label ID="ModuleCodeLabel" runat="server" Text='<%# Eval("ModuleCode") %>' />
<br />
ModuleMarks:
<asp:Label ID="ModuleMarksLabel" runat="server" Text='<%# Eval("ModuleMarks") %>' />
<br />
MarkID:
<asp:Label ID="MarkIDLabel" runat="server" Text='<%# Eval("MarkID") %>' />
<br />
ExamMark:
<asp:Label ID="ExamMarkLabel" runat="server" Text='<%# Eval("ExamMark") %>' />
<br />
AssignmentMark:
<asp:Label ID="AssignmentMarkLabel" runat="server" Text='<%# Eval("AssignmentMark") %>' />
<br />
SNumber:
<asp:Label ID="SNumberLabel" runat="server" Text='<%# Eval("SNumber") %>' />
<br />
Expr1:
<asp:Label ID="Expr1Label" runat="server" Text='<%# Eval("Expr1") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
<br />
</span>
</AlternatingItemTemplate>
<EditItemTemplate>
<span style="">ModuleTitle:
<asp:TextBox ID="ModuleTitleTextBox" runat="server" Text='<%# Bind("ModuleTitle") %>' />
<br />
UserID:
<asp:TextBox ID="UserIDTextBox" runat="server" Text='<%# Bind("UserID") %>' />
<br />
ModuleCode:
<asp:TextBox ID="ModuleCodeTextBox" runat="server" Text='<%# Bind("ModuleCode") %>' />
<br />
ModuleMarks:
<asp:TextBox ID="ModuleMarksTextBox" runat="server" Text='<%# Bind("ModuleMarks") %>' />
<br />
MarkID:
<asp:Label ID="MarkIDLabel1" runat="server" Text='<%# Eval("MarkID") %>' />
<br />
ExamMark:
<asp:TextBox ID="ExamMarkTextBox" runat="server" Text='<%# Bind("ExamMark") %>' />
<br />
AssignmentMark:
<asp:TextBox ID="AssignmentMarkTextBox" runat="server" Text='<%# Bind("AssignmentMark") %>' />
<br />
SNumber:
<asp:TextBox ID="SNumberTextBox" runat="server" Text='<%# Bind("SNumber") %>' />
<br />
Expr1:
<asp:TextBox ID="Expr1TextBox" runat="server" Text='<%# Bind("Expr1") %>' />
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
<br />
<br />
</span>
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">ModuleTitle:
<asp:TextBox ID="ModuleTitleTextBox" runat="server" Text='<%# Bind("ModuleTitle") %>' />
<br />
UserID:
<asp:TextBox ID="UserIDTextBox" runat="server" Text='<%# Bind("UserID") %>' />
<br />
ModuleCode:
<asp:TextBox ID="ModuleCodeTextBox" runat="server" Text='<%# Bind("ModuleCode") %>' />
<br />
ModuleMarks:
<asp:TextBox ID="ModuleMarksTextBox" runat="server" Text='<%# Bind("ModuleMarks") %>' />
<br />
ExamMark:
<asp:TextBox ID="ExamMarkTextBox" runat="server" Text='<%# Bind("ExamMark") %>' />
<br />
AssignmentMark:
<asp:TextBox ID="AssignmentMarkTextBox" runat="server" Text='<%# Bind("AssignmentMark") %>' />
<br />
SNumber:
<asp:TextBox ID="SNumberTextBox" runat="server" Text='<%# Bind("SNumber") %>' />
<br />
Expr1:
<asp:TextBox ID="Expr1TextBox" runat="server" Text='<%# Bind("Expr1") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
<br />
<br />
</span>
</InsertItemTemplate>
<ItemTemplate>
<span style="">ModuleTitle:
<asp:Label ID="ModuleTitleLabel" runat="server" Text='<%# Eval("ModuleTitle") %>' />
<br />
UserID:
<asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
<br />
ModuleCode:
<asp:Label ID="ModuleCodeLabel" runat="server" Text='<%# Eval("ModuleCode") %>' />
<br />
ModuleMarks:
<asp:Label ID="ModuleMarksLabel" runat="server" Text='<%# Eval("ModuleMarks") %>' />
<br />
MarkID:
<asp:Label ID="MarkIDLabel" runat="server" Text='<%# Eval("MarkID") %>' />
<br />
ExamMark:
<asp:Label ID="ExamMarkLabel" runat="server" Text='<%# Eval("ExamMark") %>' />
<br />
AssignmentMark:
<asp:Label ID="AssignmentMarkLabel" runat="server" Text='<%# Eval("AssignmentMark") %>' />
<br />
SNumber:
<asp:Label ID="SNumberLabel" runat="server" Text='<%# Eval("SNumber") %>' />
<br />
Expr1:
<asp:Label ID="Expr1Label" runat="server" Text='<%# Eval("Expr1") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style="">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">ModuleTitle:
<asp:Label ID="ModuleTitleLabel" runat="server" Text='<%# Eval("ModuleTitle") %>' />
<br />
UserID:
<asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
<br />
ModuleCode:
<asp:Label ID="ModuleCodeLabel" runat="server" Text='<%# Eval("ModuleCode") %>' />
<br />
ModuleMarks:
<asp:Label ID="ModuleMarksLabel" runat="server" Text='<%# Eval("ModuleMarks") %>' />
<br />
MarkID:
<asp:Label ID="MarkIDLabel" runat="server" Text='<%# Eval("MarkID") %>' />
<br />
ExamMark:
<asp:Label ID="ExamMarkLabel" runat="server" Text='<%# Eval("ExamMark") %>' />
<br />
AssignmentMark:
<asp:Label ID="AssignmentMarkLabel" runat="server" Text='<%# Eval("AssignmentMark") %>' />
<br />
SNumber:
<asp:Label ID="SNumberLabel" runat="server" Text='<%# Eval("SNumber") %>' />
<br />
Expr1:
<asp:Label ID="Expr1Label" runat="server" Text='<%# Eval("Expr1") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
<br />
</span>
</SelectedItemTemplate>
</asp:ListView>
Try changing it to a gridview which should allow the update to work. Try this code below:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="searchStudentResults" cssClass="table table-hover" AllowSorting="True" OnRowUpdated="GridView1_RowUpdated1" >
<Columns>
<asp:BoundField DataField="StudentNo" HeaderText="StudentNo" SortExpression="StudentNo" ReadOnly="true" />
<asp:BoundField DataField="ExamMark" HeaderText="Exam %" SortExpression="ExamMark" />
<asp:BoundField DataField="AssignmentMark" HeaderText="Assignment %" SortExpression="AssignmentMark" />
<asp:BoundField DataField="ModuleMarks" SortExpression="ModuleMarks" HeaderText="ModuleMarks" ReadOnly="true" DataFormatString="{0:0}" />
<asp:CommandField ShowEditButton="True" ButtonType="Button" EditText="Add Marks" ControlStyle-CssClass="btn btn-primary" ItemStyle-Width="100px" >
<ControlStyle CssClass="btn btn-primary"></ControlStyle>
<ItemStyle Width="100px"></ItemStyle>
</asp:CommandField>
<asp:BoundField DataField="MarkID" ControlStyle-CssClass="hidden" ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden" >
<ControlStyle CssClass="hidden"></ControlStyle>
<HeaderStyle CssClass="hidden"></HeaderStyle>
<ItemStyle CssClass="hidden"></ItemStyle>
</asp:BoundField>
</Columns>
<EmptyDataTemplate>
No marks to enter
</EmptyDataTemplate>
</asp:GridView>
Help:.....struggling with this null reference. Below is the code any ideas?
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<table class="auto-style1">
<tr>
<td> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="PID" DataSourceID="DetailsSqlDataSource" Height="50px" HorizontalAlign="Center" Width="589px" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="PID" HeaderText="PID" InsertVisible="False" ReadOnly="True" SortExpression="PID" Visible="False" />
<asp:TemplateField HeaderText="Address" SortExpression="PAddress">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PAddress") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PAddress") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="St" HeaderText="St" SortExpression="St" />
<asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:BoundField DataField="Beds" HeaderText="Beds" SortExpression="Beds" />
<asp:BoundField DataField="Bath" HeaderText="Bath" SortExpression="Bath" />
<asp:BoundField DataField="SqFt" HeaderText="SqFt" SortExpression="SqFt" />
<asp:BoundField DataField="MLS" HeaderText="MLS" SortExpression="MLS" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="AgentName" HeaderText="AgentName" SortExpression="AgentName" />
<asp:BoundField DataField="AgentEmail" HeaderText="AgentEmail" SortExpression="AgentEmail" />
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("PhotoPath") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("PhotoPath") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="175px" ImageUrl='<%# Eval("PhotoPath") %>' Width="175px" />
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>
</td>
<td valign="top">
<asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="RequestInfoSqlDataSource" DefaultMode="Insert">
<EditItemTemplate>
FromAddress:
<asp:TextBox ID="FromAddressTextBox" runat="server" Text='<%# Bind("FromAddress") %>' />
<br />
PropertyAddress:
<asp:TextBox ID="PropertyAddressTextBox" runat="server" Text='<%# Bind("PropertyAddress") %>' />
<br />
Phone:
<asp:TextBox ID="PhoneTextBox" runat="server" Text='<%# Bind("Phone") %>' />
<br />
Message:
<asp:TextBox ID="MessageTextBox" runat="server" Text='<%# Bind("Message") %>' />
<br />
FName:
<asp:TextBox ID="FNameTextBox" runat="server" Text='<%# Bind("FName") %>' />
<br />
LName:
<asp:TextBox ID="LNameTextBox" runat="server" Text='<%# Bind("LName") %>' />
<br />
Id:
<asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
<table style="width:100%;">
<tr>
<td colspan="2" style="text-align: center; font-weight: 700">Request Information:</td>
</tr>
<tr>
<td>First Name:</td>
<td>
<asp:TextBox ID="FNameTextBox" runat="server" Text='<%# Bind("FName") %>' Width="128px" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" controltovalidate="FNameTextBox" runat="server" ErrorMessage="Please enter your first name" style="color: #FF0000; font-weight: 700"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<asp:TextBox ID="LNameTextBox" runat="server" Text='<%# Bind("LName") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" controltovalidate="LNameTextBox" runat="server" ErrorMessage="Please enter your last name" style="font-weight: 700; color: #FF0000"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Email:</td>
<td>
<asp:TextBox ID="FromAddressTextBox" runat="server" Text='<%# Bind("FromAddress") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="FromAddressTextBox" runat="server" ErrorMessage="Please enter your email address" style="font-weight: 700; color: #FF0000"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Phone:</td>
<td>
<asp:TextBox ID="PhoneTextBox" runat="server" style="text-align: left" Text='<%# Bind("Phone") %>' />
</td>
</tr>
<tr>
<td>Message:</td>
<td>
<asp:TextBox ID="MessageTextBox" runat="server" Height="189px" Text='<%# "Please Send me Information about: "+Detailsview1.DataItem("paddress") + " " +Detailsview1.DataItem("City") %>' TextMode="MultiLine" Width="198px" />
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Send Request" />
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("PropertyAddress") %>'></asp:TextBox>
</td>
</tr>
</table>
<br />
<br />
<br />
</InsertItemTemplate>
<ItemTemplate>
FromAddress:
<asp:Label ID="FromAddressLabel" runat="server" Text='<%# Bind("FromAddress") %>' />
<br />
PropertyAddress:
<asp:Label ID="PropertyAddressLabel" runat="server" Text='<%# Bind("PropertyAddress") %>' />
<br />
Phone:
<asp:Label ID="PhoneLabel" runat="server" Text='<%# Bind("Phone") %>' />
<br />
Message:
<asp:Label ID="MessageLabel" runat="server" Text='<%# Bind("Message") %>' />
<br />
FName:
<asp:Label ID="FNameLabel" runat="server" Text='<%# Bind("FName") %>' />
<br />
LName:
<asp:Label ID="LNameLabel" runat="server" Text='<%# Bind("LName") %>' />
<br />
Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:SqlDataSource ID="RequestInfoSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GretnaRealtyConnectionString %>" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [Id], [FromAddress], [PropertyAddress], [Phone], [Message], [FName], [LName] FROM [RequestInfo] WHERE (([PropertyAddress] = #PropertyAddress) AND ([PropertyAddress] = #PropertyAddress2))" InsertCommand="INSERT INTO [RequestInfo] ([FName], [LName], [FromAddress], [Phone], [Message],[PropertyAddress]) VALUES (#FName, #LName, #FromAddress, #Phone, #Message, #Hiddenfield1)">
<InsertParameters>
<asp:Parameter Name="FName" />
<asp:Parameter Name="LName" />
<asp:Parameter Name="FromAddress" />
<asp:Parameter Name="Phone" />
<asp:Parameter Name="Message" />
<asp:Parameter Name="Hiddenfield1" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DetailsView1" Name="PropertyAddress" PropertyName="SelectedValue" Type="String" DefaultValue="0" />
<asp:QueryStringParameter Name="PropertyAddress2" QueryStringField="Paddress" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
<asp:SqlDataSource ID="DetailsSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GretnaRealtyConnectionString %>" SelectCommand="SELECT [PID], [PAddress], [City], [St], [Zip], [Price], [Beds], [Bath], [SqFt], [MLS], [Description], [AgentName], [AgentEmail], [Status], [PhotoPath] FROM [GretnaRealty] WHERE ([PID] = #PID)">
<SelectParameters>
<asp:QueryStringParameter Name="PID" QueryStringField="PID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
Codebehind:
Imports System.Web.UI.WebControls.GridView
Imports System.Web.UI.WebControls.FormView
Partial Class Details
Inherits System.Web.UI.Page
Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted
Dim MessageTextBox As TextBox = CType(Me.DetailsView1.FindControl("Paddress"), TextBox)
e.Values("MessageTextBox") = DetailsView1.DataItem
Dim Paddresstextbox As TextBox = CType(Me.DetailsView1.FindControl("Paddress"), TextBox)
e.Values("Textbox3") = DetailsView1.DataItem
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Automatically select the first row for
'the GridView control to cause the
'FormView control to display data.
If Not IsPostBack Then
GridView1.SelectedIndex = 0
End If
End Sub
I may be missing something, but it looks like you do not actually have a control called "PAddress". You have controls bound to the field PAddress, but not a control with the ID "PAddress". That is probably the source of your problem.
When you use the FindControl method of a control, you need to look for the ID of the inner control, not the field to which it is bound.
Similarly, you are looking for a value called "Textbox3" in the e.Values collection. That's where you should be looking for the field name, i.e. e.Values("PropertyAddress").
This is my markup
<%# Page Title="Daily Expenses" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Daily Expenses.aspx.cs" Inherits="Daily_Expenses" %>
function calendar_datechange() {
__doPostBack('txtCalendar', 'txtCalendar_TextChanged');
}
function Calculate_Difference() {
alert("Hello");
}
</script>
<asp:ToolkitScriptManager ID="AjaxScript" runat="server"></asp:ToolkitScriptManager>
<asp:TextBox ID="txtCalendar" runat="server"
ontextchanged="txtCalendar_TextChanged"></asp:TextBox>
<%--<asp:PopupControlExtender ID="txtCalendar_PopupControlExtender" runat="server"
DynamicServicePath="" Enabled="True" ExtenderControlID="" PopupControlID="Panel1"
TargetControlID="txtCalendar">
</asp:PopupControlExtender>
<asp:Panel ID="Panel1" runat="server">
<h2>Hello There</h2>
</asp:Panel>--%>
<asp:CalendarExtender ID="CalendarControl" runat="server" OnClientDateSelectionChanged="calendar_datechange" TargetControlID="txtCalendar" PopupButtonID="CalendarButton">
</asp:CalendarExtender>
<%--<ajaxToolkit:CalendarExtender ID="CalendarControl" runat="server" OnClientDateSelectionChanged="calendar_datechange" TargetControlID="txtCalendar" PopupButtonID="CalendarButton"></ajaxToolkit:CalendarExtender>--%>
<asp:Button ID="CalendarButton" runat="server" Text="Calendar" />
<br />
<br />
<br />
<asp:ListView ID="ListView1" runat="server" DataKeyNames="SNO"
DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<li style="background-color: #FFF8DC;">SNO:
<asp:Label ID="SNOLabel" runat="server" Text='<%# Eval("SNO") %>' />
<br />
Taxinreal:
<asp:Label ID="TaxinrealLabel" runat="server" Text='<%# Eval("Taxinreal") %>' />
<br />
Taxinpercent:
<asp:Label ID="TaxinpercentLabel" runat="server"
Text='<%# Eval("Taxinpercent") %>' />
<br />
Amount:
<asp:Label ID="AmountLabel" runat="server" Text='<%# Eval("Amount") %>' />
<br />
NetAmount:
<asp:Label ID="NetAmountLabel" runat="server" Text='<%# Eval("NetAmount") %>' />
<br />
DateTakenPlace:
<asp:Label ID="DateTakenPlaceLabel" runat="server"
Text='<%# Eval("DateTakenPlace") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="Differencebutton" runat="server" Text="Calculate Difference" OnClientClick="Calculate_Difference" />
</li>
</AlternatingItemTemplate>
<EditItemTemplate>
<li style="background-color: #008A8C;color: #FFFFFF;">SNO:
<asp:Label ID="SNOLabel1" runat="server" Text='<%# Eval("SNO") %>' />
<br />
Taxinreal:
<asp:TextBox ID="TaxinrealTextBox" runat="server"
Text='<%# Bind("Taxinreal") %>' />
<br />
Taxinpercent:
<asp:TextBox ID="TaxinpercentTextBox" runat="server"
Text='<%# Bind("Taxinpercent") %>' />
<br />
Amount:
<asp:TextBox ID="AmountTextBox" runat="server" Text='<%# Bind("Amount") %>' />
<br />
NetAmount:
<asp:TextBox ID="NetAmountTextBox" runat="server"
Text='<%# Bind("NetAmount") %>' />
<br />
DateTakenPlace:
<asp:TextBox ID="DateTakenPlaceTextBox" runat="server"
Text='<%# Bind("DateTakenPlace") %>' />
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
<asp:Button ID="Differencebutton" runat="server" CommandName="Difference" OnClientClick="Calculate_Difference" Text="Calculate Difference" />
</li>
</EditItemTemplate>
<EmptyDataTemplate>
No data was returned.
</EmptyDataTemplate>
<InsertItemTemplate>
<li style="">Taxinreal:
<asp:TextBox ID="TaxinrealTextBox" runat="server"
Text='<%# Bind("Taxinreal") %>' />
<br />
Taxinpercent:
<asp:TextBox ID="TaxinpercentTextBox" runat="server"
Text='<%# Bind("Taxinpercent") %>' />
<br />
Amount:
<asp:TextBox ID="AmountTextBox" runat="server" Text='<%# Bind("Amount") %>' />
<br />
NetAmount:
<asp:TextBox ID="NetAmountTextBox" runat="server"
Text='<%# Bind("NetAmount") %>' />
<br />
DateTakenPlace:
<asp:TextBox ID="DateTakenPlaceTextBox" runat="server"
Text='<%# Bind("DateTakenPlace") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
<asp:Button ID="Differencebutton" runat="server" CommandName="Difference" OnClientClick="Calculate_Difference" Text="Calculate Difference" />
</li>
</InsertItemTemplate>
<ItemSeparatorTemplate>
<br />
</ItemSeparatorTemplate>
<ItemTemplate>
<li style="background-color: #DCDCDC;color: #000000;">SNO:
<asp:Label ID="SNOLabel" runat="server" Text='<%# Eval("SNO") %>' />
<br />
Taxinreal:
<asp:Label ID="TaxinrealLabel" runat="server" Text='<%# Eval("Taxinreal") %>' />
<br />
Taxinpercent:
<asp:Label ID="TaxinpercentLabel" runat="server"
Text='<%# Eval("Taxinpercent") %>' />
<br />
Amount:
<asp:Label ID="AmountLabel" runat="server" Text='<%# Eval("Amount") %>' />
<br />
NetAmount:
<asp:Label ID="NetAmountLabel" runat="server" Text='<%# Eval("NetAmount") %>' />
<br />
DateTakenPlace:
<asp:Label ID="DateTakenPlaceLabel" runat="server"
Text='<%# Eval("DateTakenPlace") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="Differencebutton" runat="server" CommandName="Difference" OnClientClick="Calculate_Difference" Text="Calculate Difference" />
</li>
</ItemTemplate>
<LayoutTemplate>
<ul ID="itemPlaceholderContainer" runat="server"
style="font-family: Verdana, Arial, Helvetica, sans-serif;">
<li runat="server" id="itemPlaceholder" />
</ul>
<div style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<li style="background-color: #008A8C;font-weight: bold;color: #FFFFFF;">SNO:
<asp:Label ID="SNOLabel" runat="server" Text='<%# Eval("SNO") %>' />
<br />
Taxinreal:
<asp:Label ID="TaxinrealLabel" runat="server" Text='<%# Eval("Taxinreal") %>' />
<br />
Taxinpercent:
<asp:Label ID="TaxinpercentLabel" runat="server"
Text='<%# Eval("Taxinpercent") %>' />
<br />
Amount:
<asp:Label ID="AmountLabel" runat="server" Text='<%# Eval("Amount") %>' />
<br />
NetAmount:
<asp:Label ID="NetAmountLabel" runat="server" Text='<%# Eval("NetAmount") %>' />
<br />
DateTakenPlace:
<asp:Label ID="DateTakenPlaceLabel" runat="server"
Text='<%# Eval("DateTakenPlace") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="Differencebutton" runat="server" CommandName="Difference" OnClientClick="Calculate_Difference" Text="Calculate Difference" />
</li>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnString %>"
DeleteCommand="DELETE FROM [tblexpenses] WHERE [SNO] = #SNO"
InsertCommand="INSERT INTO [tblexpenses] ([Taxinreal], [Taxinpercent], [Amount], [NetAmount], [DateTakenPlace]) VALUES (#Taxinreal, #Taxinpercent, #Amount, #NetAmount, #DateTakenPlace)"
SelectCommand="SELECT * FROM [tblexpenses] WHERE ([DateTakenPlace] = #DateTakenPlace2)"
UpdateCommand="UPDATE [tblexpenses] SET [Taxinreal] = #Taxinreal, [Taxinpercent] = #Taxinpercent, [Amount] = #Amount, [NetAmount] = #NetAmount, [DateTakenPlace] = #DateTakenPlace WHERE [SNO] = #SNO">
<DeleteParameters>
<asp:Parameter Name="SNO" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Taxinreal" Type="Double" />
<asp:Parameter Name="Taxinpercent" Type="Decimal" />
<asp:Parameter Name="Amount" Type="Double" />
<asp:Parameter Name="NetAmount" Type="Int32"/>
<asp:Parameter DbType="Date" Name="DateTakenPlace" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="txtCalendar" DbType="Date"
Name="DateTakenPlace2" PropertyName="Text" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Taxinreal" Type="Double" />
<asp:Parameter Name="Taxinpercent" Type="Decimal" />
<asp:Parameter Name="Amount" Type="Double" />
<asp:Parameter Name="NetAmount" Type="Int32" />
<asp:Parameter DbType="Date" Name="DateTakenPlace" />
<asp:Parameter Name="SNO" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
<br />
<br />
<br />
I have a table tblexpense in my database. I have columns named Taxinreal , Taxinpercent,*Amount* NetAmount.*NetAmount* is computed column which is equal to*(Amount-Taxinreal-Taxinpercent)* Now what i want is at runtime in my aspx page the user must be able to insert amount,tax,taxinpercentage and according to the value supplied by user NetAmount must be calculated(Netamount=Amount-Tax-Taxpercentage) at runtime and inserted into the database.For this purpose i have a differencebutton which should perform the required mathematical operation. The problem i am having is i am using a listview. Now adding a new button is simple but how can i perform required mathematical operation in the click event of difference button so that when the listview is in update mode the user can change values of taxinreal ,taxinpercent and obtain the result at runtime and the result be updated in database ??? I need solution urgently(P.S. I need to use Listview only).
You want to use OnItemCommand of ListView.
Here is a sample.
You need to insert/update to database (using either ADO.Net, entity framework or linq-to-sql) in code behind if you want to perform calculation.
I have created 2 dropdownlists that are binded to a SQL DB and then pulling up a GridView. But, more specifically is there a way to have 3 user picked boxes and then have a Gridview show up? I would like to have a quick efficient way to do this without problems. I created the first 2 DDLs with autopostback and not working properly. any suggestions?
<asp:Label ID="CustomerNumberLabel" runat="server" Text="Customer Number " Width="125px"></asp:Label>
<asp:DropDownList ID="CustomerNumber" runat="server" AutoPostBack="True"
DataSourceID="ADDRDataSource" DataTextField="SACSNO"
DataValueField="SACSNO" AppendDataBoundItems="True">
<asp:ListItem Text="Select a Customer" Value="" Selected="True" />
</asp:DropDownList><br />
<asp:SqlDataSource ID="ADDRDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:iSeriesHistoryConnectionString %>"
SelectCommand="SELECT [SACSNO] FROM [ADDR] WHERE ([SARP#1] = #column1)">
<SelectParameters>
<asp:SessionParameter Name="column1" SessionField="userName" Type="Decimal" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="ShipToNumberLabel" runat="server" Text="Ship-to Location " Width="125px"></asp:Label>
<asp:DropDownList ID="ShipToNumber" runat="server"
DataSourceID="ADDRShipToNumberDataSource" DataTextField="SASHNM"
DataValueField="SASHNM" AutoPostBack="True">
<asp:ListItem Text="Select Ship-To Location" Value="" Selected="True" />
</asp:DropDownList>
<asp:SqlDataSource ID="ADDRShipToNumberDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:iSeriesHistoryConnectionString %>"
SelectCommand="SELECT [SASHNM], [SASAD1], [SASAD2], [SASCTY], [SACSNO], [SASHST], [SASZIP] FROM [ADDR] WHERE ([SACSNO] = #SACSNO)">
<SelectParameters>
<asp:ControlParameter ControlID="CustomerNumber" Name="SACSNO"
PropertyName="SelectedValue" Type="Decimal" />
</SelectParameters>
</asp:SqlDataSource>
<br /><br />
<asp:FormView ID="FormView1" runat="server"
DataSourceID="ADDRShippingDataSource" Width="400px">
<EditItemTemplate>
SACSNO:
<asp:TextBox ID="SACSNOTextBox" runat="server" Text='<%# Bind("SACSNO") %>' />
<br />
SASHNM:
<asp:TextBox ID="SASHNMTextBox" runat="server" Text='<%# Bind("SASHNM") %>' />
<br />
SASAD1:
<asp:TextBox ID="SASAD1TextBox" runat="server" Text='<%# Bind("SASAD1") %>' />
<br />
SASAD2:
<asp:TextBox ID="SASAD2TextBox" runat="server" Text='<%# Bind("SASAD2") %>' />
<br />
SASCTY:
<asp:TextBox ID="SASCTYTextBox" runat="server" Text='<%# Bind("SASCTY") %>' />
<br />
SASHST:
<asp:TextBox ID="SASHSTTextBox" runat="server" Text='<%# Bind("SASHST") %>' />
<br />
SASZIP:
<asp:TextBox ID="SASZIPTextBox" runat="server" Text='<%# Bind("SASZIP") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
SACSNO:
<asp:TextBox ID="SACSNOTextBox" runat="server" Text='<%# Bind("SACSNO") %>' />
<br />
SASHNM:
<asp:TextBox ID="SASHNMTextBox" runat="server" Text='<%# Bind("SASHNM") %>' />
<br />
SASAD1:
<asp:TextBox ID="SASAD1TextBox" runat="server" Text='<%# Bind("SASAD1") %>' />
<br />
SASAD2:
<asp:TextBox ID="SASAD2TextBox" runat="server" Text='<%# Bind("SASAD2") %>' />
<br />
SASCTY:
<asp:TextBox ID="SASCTYTextBox" runat="server" Text='<%# Bind("SASCTY") %>' />
<br />
SASHST:
<asp:TextBox ID="SASHSTTextBox" runat="server" Text='<%# Bind("SASHST") %>' />
<br />
SASZIP:
<asp:TextBox ID="SASZIPTextBox" runat="server" Text='<%# Bind("SASZIP") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
Customer Detail<br />
<br />
<table class="style1" width="35px">
<tr>
<td class="style2" align="center" >
Address Line 1</td>
<td class="style3" align="center" >
<asp:Label ID="SASAD1Label" runat="server" Text='<%# Bind("SASAD1") %>' />
</td>
</tr>
<tr>
<td class="style2" align="center" >
Address Line 2</td>
<td class="style3" align="center" >
<asp:Label ID="SASAD2Label" runat="server" Text='<%# Bind("SASAD2") %>' />
</td>
</tr>
<tr>
<td class="style2" align="center">
City</td>
<td class="style3" align="center" >
<asp:Label ID="SASCTYLabel" runat="server" Text='<%# Bind("SASCTY") %>' />
</td>
</tr>
<tr>
<td align="center" class="style2">
State</td>
<td class="style3" align="center" >
<asp:Label ID="SASHSTLabel" runat="server" Text='<%# Bind("SASHST") %>' />
</td>
</tr>
<tr>
<td align="center" class="style2">
Zip</td>
<td class="style3" align="center" >
<asp:Label ID="SASZIPLabel" runat="server" Text='<%# Bind("SASZIP") %>' />
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="ADDRShippingDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:iSeriesHistoryConnectionString %>"
SelectCommand="SELECT [SACSNO], [SASHNM], [SASAD1], [SASAD2], [SASCTY], [SASHST], [SASZIP] FROM [ADDR] WHERE ([SASHNM] = #SASHNM)">
<SelectParameters>
<asp:ControlParameter ControlID="ShipToNumber" Name="SASHNM"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
You can turn off AutoPostBack for all the DropDownLists, and add a Button called Lookup with something like the following in the Lookup Button_Click event:
ADDRShippingDataSource.SelectParameters.Clear()
If ShipToNumber.SelectedValue <> "" Then
ADDRShippingDataSource.SelectParameters.Add("SASHNM", ShipToNumber.SelectedValue)
End If
If CustomerNumber.SelectedValue <> "" Then
ADDRShippingDataSource.SelectParameters.Add("SACSNO", CustomerNumber.SelectedValue)
End If
ADDRShippingDataSource.DataBind()
Your select command would need to be updated to include the additional criteria in your DataSource:
SELECT [SACSNO], [SASHNM], [SASAD1], [SASAD2], [SASCTY], [SASHST], [SASZIP] FROM [ADDR] WHERE (#SASHNM IS NULL OR [SASHNM] = #SASHNM) OR (#SACSNO IS NULL OR [SACSNO] = #SACSNO)