How to make this DropDownList works inside the ListView? - c#

I have two tables in my database as following:
Employee Table: Username, Name, Job, DivisonCode
Division Table: DivisionCode, DivisionName
I am using ListView to show the information of the employee. Instead of showing the DivisionCode, I put a DropDownList that for showing the DivsionNames and of course I put this DropDownList inside the EditItemTemplate in the ListView. Everything works fine except editing the employee information. When I tried to change the division of the employee, I got the following error:
Cannot insert the value NULL into column 'DivisionCode', table
'psspdbTest.dbo.employee'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
My Code in ASP.NET:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Username"
DataSourceID="SqlDataSource1" 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="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server"
Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</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:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel1" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:TextBox ID="JobTitleTextBox" runat="server"
Text='<%# Bind("JobTitle") %>' />
</td>
<td>
<asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
</td>
<td>
<asp:TextBox ID="EmpOrgTypeTextBox" runat="server"
Text='<%# Bind("EmpOrgType") %>' />
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="DivisionName"
DataValueField="SapCode">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
DeleteCommand="DELETE FROM [Divisions] WHERE [SapCode] = #SapCode"
InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionName]) VALUES (#SapCode, #DivisionName)"
SelectCommand="SELECT * FROM [Divisions]"
UpdateCommand="UPDATE [Divisions] SET [DivisionName] = #DivisionName WHERE [SapCode] = #SapCode">
<DeleteParameters>
<asp:Parameter Name="SapCode" Type="Double" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="SapCode" Type="Double" />
<asp:Parameter Name="DivisionName" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="DivisionName" Type="String" />
<asp:Parameter Name="SapCode" Type="Double" />
</UpdateParameters>
</asp:SqlDataSource>
</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="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
</td>
<td>
<asp:TextBox ID="UsernameTextBox" runat="server"
Text='<%# Bind("Username") %>' />
</td>
<td>
<asp:TextBox ID="JobTitleTextBox" runat="server"
Text='<%# Bind("JobTitle") %>' />
</td>
<td>
<asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
</td>
<td>
<asp:TextBox ID="EmpOrgTypeTextBox" runat="server"
Text='<%# Bind("EmpOrgType") %>' />
</td>
<td>
<asp:TextBox ID="DivisionCodeTextBox" runat="server"
Text='<%# Bind("DivisionCode") %>' />
</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="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server" Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</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">
Name</th>
<th runat="server">
Username</th>
<th runat="server">
JobTitle</th>
<th runat="server">
BadgeNo</th>
<th runat="server">
EmpOrgType</th>
<th runat="server">
DivisionCode</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"
ShowLastPageButton="True" />
</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="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server"
Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.employee.Name, dbo.employee.Username, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.employee.EmpOrgType, dbo.employee.DivisionCode
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode"
DeleteCommand="DELETE FROM [employee] WHERE [Username] = #Username"
InsertCommand="INSERT INTO [employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (#Name, #Username, #JobTitle, #BadgeNo, #EmpOrgType, #DivisionCode)"
UpdateCommand="UPDATE [employee] SET [Name] = #Name, [JobTitle] = #JobTitle, [BadgeNo] = #BadgeNo, [EmpOrgType] = #EmpOrgType, [DivisionCode] = #DivisionCode WHERE [Username] = #Username">
<DeleteParameters>
<asp:Parameter Name="Username" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Username" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="Double" />
<asp:Parameter Name="EmpOrgType" Type="Double" />
<asp:Parameter Name="DivisionCode" Type="Double" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="Double" />
<asp:Parameter Name="EmpOrgType" Type="Double" />
<asp:Parameter Name="DivisionCode" Type="Double" />
<asp:Parameter Name="Username" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
So how to solve this problem?

Add this to your DropDownList SelectedValue='<%# Bind("DivisionCode") %>'
and remove AutoPostBack="true"

Related

.NET Delete in ListView isn't passing any values

For some reason, all my ListView functions work, except my delete button. I have debugged, and it points to the right method for deletion, but it's just passing all null values. I have no idea what the issue is, everything else seems to be working just fine. here is my listview code:
<asp:ListView ID="EntReqListView" runat="server" InsertItemPosition="LastItem" DataSourceID="EntranceReqODS" Visible="false">
<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="ProgramIDLabel" runat="server" Text='<%# Bind("ProgramID") %>' />
</td>
<td>
<asp:Label ID="CourseIDLabel" runat="server" Text='<%# Bind("CourseID") %>' />
</td>
<td>
<asp:Label ID="CourseNameLabel" runat="server" Text='<%# Bind("CourseName") %>' />
</td>
<td>
<asp:Label ID="MarkLabel" runat="server" Text='<%# Bind("Mark") %>' />
</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="ProgramIDTextBox" runat="server" Text='<%# Bind("ProgramID") %>' />
</td>
<td>
<asp:Label ID="CourseIDTextBox" runat="server" Text='<%# Bind("CourseID") %>' />
</td>
<td>
<asp:Label ID="CourseNameTextBox" runat="server" Text='<%# Bind("CourseName") %>' />
</td>
<td>
<asp:TextBox ID="MarkTextBox" runat="server" Text='<%# Bind("Mark") %>' />
</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="ProgramIDTextBox" runat="server" value="<%# ProgramID.Text %>" Text='<%# Bind("ProgramID")%>' onkeydown="javascript:return false"/>
</td>
<td>
<asp:Label ID="CourseIDTextBox" runat="server" Text='<%# Bind("CourseID") %>' />
</td>
<td>
<asp:DropDownList ID="EntReqCourses" runat="server"
DataSourceID="CourseListODS"
DataTextField="SubjectName"
DataValueField="CourseID"
SelectedValue='<%# Bind("CourseID") %>' AppendDataBoundItems="True">
<asp:ListItem Text="--Select One--" Value="" />
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="MarkTextBox" runat="server" Text='<%# Bind("Mark") %>' />
</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="ProgramIDLabel" runat="server" Text='<%# Bind("ProgramID") %>' />
</td>
<td>
<asp:Label ID="CourseIDLabel" runat="server" Text='<%# Bind("CourseID") %>' />
</td>
<td>
<asp:Label ID="CourseNameLabel" runat="server" Text='<%# Bind("CourseName") %>' />
</td>
<td>
<asp:Label ID="MarkLabel" runat="server" Text='<%# Bind("Mark") %>' />
</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">ProgramID</th>
<th runat="server">CourseID</th>
<th runat="server">CourseName</th>
<th runat="server">Mark</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" ShowLastPageButton="True" />
</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="ProgramIDLabel" runat="server" Text='<%# Bind("ProgramID") %>' />
</td>
<td>
<asp:Label ID="CourseIDLabel" runat="server" Text='<%# Bind("CourseID") %>' />
</td>
<td>
<asp:Label ID="CourseNameLabel" runat="server" Text='<%# Bind("CourseName") %>' />
</td>
<td>
<asp:Label ID="MarkLabel" runat="server" Text='<%# Bind("Mark") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
I set up all my CRUD methods through the wizard, here is the delete method it goes to:
[DataObjectMethod(DataObjectMethodType.Delete, false)]
public void EntReq_Delete(EntranceRequirementInfo item)
{
using (Pathway_Model context = new Pathway_Model())
{
EntranceRequirement existing = context.EntranceRequirements.Find(item.CourseID, item.ProgramID);
context.EntranceRequirements.Remove(existing);
context.SaveChanges();
}
}
My ODS:
<asp:ObjectDataSource ID="EntranceReqODS" runat="server" DataObjectTypeName="Pathway_System.Entities.DTOs.EntranceRequirementInfo" DeleteMethod="EntReq_Delete" InsertMethod="EntReq_Add" OldValuesParameterFormatString="original_{0}" SelectMethod="EntReqList_byID" TypeName="Pathway_System.BLL.PathwaysController" UpdateMethod="EntReq_Update">
<SelectParameters>
<asp:ControlParameter ControlID="CertificateList" Name="programID" PropertyName="SelectedValue" Type="Int32" DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>

UPDATE command property for asp:SqlDataSource does not recognize parameter values

The error occurs when the asp:ListView object tries to call the update command built in to the datasource. Specifically the error states:
Cannot insert the value NULL into column 'FirstName', table
'SqlCatalog.dbo.Members'; column does not allow nulls. UPDATE fails.
The statement has been terminated
I'm using the following code in the ItemUpdateEventHandler to verify NULL's are not being inserted into fields that are set to NOT NULL.
protected void Members_VerifyChanges(Object sender, ListViewItemUpdateEventArgs eArgs)
{
if(eArgs.NewValues["ID"] == null || eArgs.NewValues["FirstName"] == null || eArgs.NewValues["LastName"] == null)
{
eArgs.Cancel = true;
}
}
Here is the UPDATE command specified for the datasource:
UPDATE Members SET [ID]=original_ID, [Approved]=#App, [FirstName]=#First, [MiddleInitial]=#Mid, [LastName]=#Last WHERE ID=#original_ID
Since the event handler function does not cancel the UPDATE command I can be sure the NewValues dictionary has something other than NULL to be inserted into the FirstName field.
Here is the specific test case I have been working with.
Old Values
________________________________________________________________________________
|____ID____|____Approved____|____First Name____|____Initial____|____Last Name____|
|__1001____|______[x]_______|_______Test_______|_______A_______|______Test_______|
New Values
________________________________________________________________________________
|____ID____|____Approved____|____First Name____|____Initial____|____Last Name____|
|__1001____|______[x]_______|______Bruce_______|_______A_______|______Wayne______|
To me it seems like the NewValues Dictionary is not being added to the parameters but I don't know how I could check that. I've thought about manually creating and executing the update command in the event handler but that's a hack i'd prefer not to do.
Here is the asp markup for the SqlDataSource:
<asp:SqlDataSource ID="WaterFacultySqlDataSrc" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [ID], [Approved], [FirstName], [MiddleInitial], [LastName] FROM Members ORDER BY [ID] DESC"
InsertCommand="INSERT INTO [Members] (ID, Approved, FirstName, MiddleInitial, LastName VALUES (#ID, #App, #First, #Mid, #Last)"
UpdateCommand="UPDATE [Members] SET [ID]=#original_ID, [Approved]=#App, [FirstName]=#First, [MiddleInitial]=#Mid, [LastName]=#Last WHERE ID=#original_ID"
DeleteCommand="DELETE FROM [Members] WHERE ID=#original_ID">
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="App" Type="Boolean" />
<asp:Parameter Name="First" Type="String" />
<asp:Parameter Name="Mid" Type="Char" />
<asp:Parameter Name="Last" Type="String" />
</UpdateParameters>
/**For Brevity I've excluded the other commands parameters*/
and finally the markup for the ListView controller:
<asp:ListView ID="PendingUpdates" runat="server" DataKeyNames="ID" DataSourceID="WaterFacultySqlDataSrc" OnItemUpdating="Members_VerifyChanges">
<LayoutTemplate>
<table id="PendingUpdatesTable" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" 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="TableHeaders" runat="server" style="background-color: #E0FFFF;color: #333333;">
<th id="ApproveOrDeny" runat="server">
Approve/Deny</th>
<th id="WF_ID_Header" runat="server">
ID</th>
<th id="Approved_Header" runat="server">
Approved</th>
<th id="First_Header" runat="server">
First Name</th>
<th id="Middle_Header" runat="server">
Initial</th>
<th id="Last_Header" runat="server">
Last Name</th>
</tr>
<tr ID="itemPlaceHolder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Button ID="Edit_Button" runat="server" CommandName="Edit" Text="Edit"/>
<asp:Button ID="Delete_Button" runat="server" CommandName="Delete" Text="Delete"/>
</td>
<td>
<asp:Label ID="ID_Val" runat="server" Text='<%# Bind("ID") %>' />
</td>
<td>
<asp:Label ID="Approve_Val" runat="server" Text='<%# Bind("Approved") %>' />
</td>
<td>
<asp:Label ID="First_Val" runat="server" Text='<%# Bind("FirstName") %>' />
</td>
<td>
<asp:Label ID="Middle_Val" runat="server" Text='<%# Bind("MiddleInitial") %>' />
</td>
<td>
<asp:Label ID="Last_Val" runat="server" Text='<%# Bind("LastName") %>' />
</td>
</tr>
</ItemTemplate>
<EditTemplate>
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Button ID="Save_Button" runat="server" CommandName="Update" Text="Save"/>
<asp:Button ID="Cancel_Button" runat="server" CommandName="Cancel" Text="Cancel"/>
</td>
<td>
<asp:Label ID="ID_Val" runat="server" Text='<%# Bind("ID") %>' />
</td>
<td>
<asp:checkBox ID="Approve_Val" runat="server" checked='<%# Bind("Approved") %>' />
</td>
<td>
<asp:textBox ID="First_Val" runat="server" Text='<%# Bind("FirstName") %>' />
</td>
<td>
<asp:textBox ID="Middle_Val" runat="server" Text='<%# Bind("MiddleInitial") %>' />
</td>
<td>
<asp:textBox ID="Last_Val" runat="server" Text='<%# Bind("LastName") %>' />
</td>
</tr>
</EditTemplate>
</asp:ListView>
Not sure why but changing the names of 2 of the parameters fixed this error.
UPDATE Members SET [ID]=original_ID, [Approved]=#App, [FirstName]=#FirstName, [MiddleInitial]=#Mid, [LastName]=#LastName WHERE ID=#original_ID}
I'm guessing Microsoft SQL Server 2008 R2 is parsing First and Last as keywords to indicate the chronological First and Last parameters rather than the parameters named First and Last. I've not been able to confirm that, but I'll keep digging and post the answer when I find it.

query string and response.redirect issue

I Have a listView and I want to create a button or link that when user click,goes to a page that have a formview and it's datasourse configured :
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:akhbarrConnectionString %>"
SelectCommand="SELECT [DarkhastId], [shakhs], [nam], [Idgharardad], [elat], [hamrah], [sabet], [karshenas], [tarikh] FROM [darkhastezam] WHERE ([DarkhastId] = #DarkhastId)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="DarkhastId"
QueryStringField="DarkhastId" Type="Decimal" />
</SelectParameters>
so I need a link in page 1 for paasing DarkhastId with query string to page 2.
I tried these one after another but not responsible:
<asp:HyperLinkField DataNavigateUrlFields="DarkhastId" DataNavigateUrlFormatString="showprofile.aspx?DarkhastId={0}" HeaderText="پاسخ دهی" Text="پاسخ دهی" />
protected void ListView1_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
if (e.Exception == null && e.AffectedRows > 0)
{
string url="showprofile.aspx?DarkhastId="+e.OldValues["DarkhastId"];
Response.Redirect(url);
}
}
my listview codes:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="DarkhastId"
DataSourceID="SqlDataSource1" GroupItemCount="2"
onitemupdated="ListView1_ItemUpdated">
<AlternatingItemTemplate>
<td runat="server" style="" >
<div id="printarea">
شناسه ی درخواست :
<asp:Label ID="DarkhastIdLabel" runat="server"
Text='<%# Eval("DarkhastId") %>' />
<br />
شخص :
<asp:Label ID="shakhsLabel" runat="server" Text='<%# Eval("shakhs") %>' />
<br />
نام و نام خانوادگی :
<asp:Label ID="namLabel" runat="server" Text='<%# Eval("nam") %>' />
<br />
شماره قرارداد :
<asp:Label ID="IdgharardadLabel" runat="server"
Text='<%# Eval("Idgharardad") %>' />
<br />
شرح درخواست :
<asp:Label ID="elatLabel" runat="server" Text='<%# Eval("elat") %>' />
<br />
شماره موبایل :
<asp:Label ID="hamrahLabel" runat="server" Text='<%# Eval("hamrah") %>' />
<br />
شماره تلفن ثابت :
<asp:Label ID="sabetLabel" runat="server" Text='<%# Eval("sabet") %>' />
<br />
وضعیت پیگیری :
<asp:Label ID="vaziatLabel" runat="server" Text='<%# Eval("vaziat") %>' />
<br />
کارشناس (های) اعزامی :
<asp:Label ID="karshenasLabel" runat="server" Text='<%# Eval("karshenas") %>' />
<br />
تاریخ ثبت درخواست :
<asp:Label ID="tarikhLabel" runat="server" Text='<%# Eval("tarikh") %>' />
<br />
</div>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="حذف درخواست" />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="ویرایش درخواست" />
<br />
<asp:HyperLinkField DataNavigateUrlFields="DarkhastId" DataNavigateUrlFormatString="showprofile.aspx?DarkhastId={0}" HeaderText="پاسخ دهی" Text="پاسخ دهی" />
<input id="btnprint" type="button" onclick="PrintDiv()" value="print" />
</td>
</AlternatingItemTemplate>
<EditItemTemplate>
<td runat="server" class="testak">
<table>
<tr>
<td>تاریخ ثبت درخواست : </td>
<td><asp:Label ID="tarikhTextBox" runat="server" Text='<%# Bind("tarikh") %>' /></td>
<td></td>
</tr>
<tr>
<td>شناسه درخواست : </td>
<td> <asp:Label ID="DarkhastIdLabel1" runat="server"
Text='<%# Eval("DarkhastId") %>' /></td>
<td></td>
</tr>
<tr>
<td> شخص : </td>
<td> <asp:TextBox ID="shakhsTextBox" runat="server" Text='<%# Bind("shakhs") %>' /></td>
<td></td>
</tr>
<tr>
<td>نام و نام خانوادگی :</td>
<td> <asp:TextBox ID="namTextBox" CssClass="field" Width="315px" runat="server" Text='<%# Bind("nam") %>' /></td>
<td></td>
</tr>
<tr>
<td>شماره قرارداد : </td>
<td><asp:TextBox CssClass="field" Width="315px" ID="IdgharardadTextBox" runat="server"
Text='<%# Bind("Idgharardad") %>' /></td>
<td></td>
</tr>
<tr>
<td>شرح درخواست : </td>
<td> <asp:TextBox CssClass="field" Height="194px" TextMode="MultiLine" Width="315px" ID="elatTextBox" runat="server" Text='<%# Bind("elat") %>' /></td>
<td></td>
</tr>
<tr>
<td>شماره موبایل : </td>
<td><asp:TextBox ID="hamrahTextBox" CssClass="field" Width="315px" runat="server" Text='<%# Bind("hamrah") %>' /></td>
<td></td>
</tr>
<tr>
<td>شماره تلفن ثابت : </td>
<td><asp:TextBox ID="sabetTextBox" CssClass="field" Width="315px" runat="server" Text='<%# Bind("sabet") %>' /></td>
<td></td>
</tr>
<tr>
<td>وضعیت پیگیری : </td>
<td><asp:TextBox ID="vaziatTextBox" runat="server" Text='<%# Bind("vaziat") %>' /></td>
<td></td>
</tr>
<tr>
<td> کارشناس (های) اعزامی : </td>
<td><asp:TextBox CssClass="field" Height="194px" TextMode="MultiLine" Width="315px" ID="karshenasTextBox" runat="server"
Text='<%# Bind("karshenas") %>' /></td>
<td></td>
</tr>
<tr>
<td> <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="به روز رسانی" /></td>
<td><asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="لغو ویرایش" /></td>
<td></td>
</tr>
</table>
</td>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
اطلاعاتی برای نمایش موجود نیست.</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="">
شخص :
<asp:TextBox CssClass="Test" ID="shakhsTextBox" runat="server" Text='<%# Bind("shakhs") %>' />
<br />
نام و نام خانوادگی :
<asp:TextBox CssClass="Test" ID="namTextBox" runat="server" Text='<%# Bind("nam") %>' />
<br />
شماره قرارداد :
<asp:TextBox CssClass="Test" ID="IdgharardadTextBox" runat="server"
Text='<%# Bind("Idgharardad") %>' />
<br />
شرح درخواست :
<asp:TextBox CssClass="Test" ID="elatTextBox" runat="server" Text='<%# Bind("elat") %>' />
<br />
شماره موبایل :
<asp:TextBox CssClass="Test" ID="hamrahTextBox" runat="server" Text='<%# Bind("hamrah") %>' />
<br />
شماره تلفن ثابت :
<asp:TextBox CssClass="Test" ID="sabetTextBox" runat="server" Text='<%# Bind("sabet") %>' />
<br />
وضعیت پیگیری :
<asp:TextBox CssClass="Test" ID="vaziatTextBox" runat="server" Text='<%# Bind("vaziat") %>' />
<br />
کارشناس (های) اعزامی :
<asp:TextBox CssClass="Test" ID="karshenasTextBox" runat="server"
Text='<%# Bind("karshenas") %>' />
<br />
تاریخ ثبت درخواست :
<asp:TextBox CssClass="Test" ID="tarikhTextBox" runat="server" Text='<%# Bind("tarikh") %>' />
<br />
<asp:Button CssClass="Test" ID="InsertButton" runat="server" CommandName="Insert"
Text="ثبت درخواست" />
<br />
<asp:Button CssClass="Test" ID="CancelButton" runat="server" CommandName="Cancel"
Text="پاک کردن فرم" />
<br />
</td>
</InsertItemTemplate>
<ItemTemplate>
<td runat="server" style="">
شناسه درخواست :
<asp:Label CssClass="Test" ID="DarkhastIdLabel" runat="server"
Text='<%# Eval("DarkhastId") %>' />
<br />
شخص :
<asp:Label CssClass="Test" ID="shakhsLabel" runat="server" Text='<%# Eval("shakhs") %>' />
<br />
نام و نام خانوادگی :
<asp:Label CssClass="Test" ID="namLabel" runat="server" Text='<%# Eval("nam") %>' />
<br />
شماره قرارداد :
<asp:Label CssClass="Test" ID="IdgharardadLabel" runat="server"
Text='<%# Eval("Idgharardad") %>' />
<br />
شرح درخواست :
<asp:Label CssClass="Test" ID="elatLabel" runat="server" Text='<%# Eval("elat") %>' />
<br />
شماره موبایل :
<asp:Label CssClass="Test" ID="hamrahLabel" runat="server" Text='<%# Eval("hamrah") %>' />
<br />
شماره تلفن ثابت :
<asp:Label CssClass="Test" ID="sabetLabel" runat="server" Text='<%# Eval("sabet") %>' />
<br />
وضعیت پیگیری :
<asp:Label CssClass="Test" ID="vaziatLabel" runat="server" Text='<%# Eval("vaziat") %>' />
<br />
کارشناس (های) اعزامی :
<asp:Label CssClass="Test" ID="karshenasLabel" runat="server" Text='<%# Eval("karshenas") %>' />
<br />
تاریخ ثبت درخواست :
<asp:Label CssClass="Test" ID="tarikhLabel" runat="server" Text='<%# Eval("tarikh") %>' />
<br />
<asp:Button CssClass="Test" ID="DeleteButton" runat="server" CommandName="Delete"
Text="حذف درخواست" />
<br />
<asp:Button CssClass="Test" ID="EditButton" runat="server" CommandName="Edit" Text="ویرایش درخواست" />
<br />
</td>
</ItemTemplate>
<LayoutTemplate>
<table runat="server" class="testa" style=" ">
<tr runat="server" style="">
<td runat="server" >
<table ID="groupPlaceholderContainer" runat="server" border="0" >
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server" PageSize="12">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<td runat="server" style="">
شناسه درخواست :
<asp:Label CssClass="Test" ID="DarkhastIdLabel" runat="server"
Text='<%# Eval("DarkhastId") %>' />
<br />
شخص :
<asp:Label CssClass="Test" ID="shakhsLabel" runat="server" Text='<%# Eval("shakhs") %>' />
<br />
نام و نام خانوادکی :
<asp:Label CssClass="Test" ID="namLabel" runat="server" Text='<%# Eval("nam") %>' />
<br />
شماره قرارداد :
<asp:Label CssClass="Test" ID="IdgharardadLabel" runat="server"
Text='<%# Eval("Idgharardad") %>' />
<br />
شرح درخواست :
<asp:Label CssClass="Test" ID="elatLabel" runat="server" Text='<%# Eval("elat") %>' />
<br />
شماره موبایل :
<asp:Label CssClass="Test" ID="hamrahLabel" runat="server" Text='<%# Eval("hamrah") %>' />
<br />
شماره تلفن ثابت :
<asp:Label CssClass="Test" ID="sabetLabel" runat="server" Text='<%# Eval("sabet") %>' />
<br />
وضعیت پیگیری :
<asp:Label CssClass="Test" ID="vaziatLabel" runat="server" Text='<%# Eval("vaziat") %>' />
<br />
کارشناس (های) اعزامی :
<asp:Label CssClass="Test" ID="karshenasLabel" runat="server" Text='<%# Eval("karshenas") %>' />
<br />
تاریخ ثبت درخواست :
<asp:Label CssClass="Test" ID="tarikhLabel" runat="server" Text='<%# Eval("tarikh") %>' />
<br />
<asp:Button CssClass="Test" ID="DeleteButton" runat="server" CommandName="Delete"
Text="حذف درخواست" />
<br />
<asp:Button CssClass="Test" ID="EditButton" runat="server" CommandName="Edit" Text="ویرایش درخواست" />
<br />
</td>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:akhbarrrConnectionString %>"
DeleteCommand="DELETE FROM [darkhastezam] WHERE [DarkhastId] = #DarkhastId"
InsertCommand="INSERT INTO [darkhastezam] ([shakhs], [nam], [Idgharardad], [elat], [hamrah], [sabet], [vaziat], [karshenas], [tarikh]) VALUES (#shakhs, #nam, #Idgharardad, #elat, #hamrah, #sabet, #vaziat, #karshenas, #tarikh)"
SelectCommand="SELECT * FROM [darkhastezam]"
UpdateCommand="UPDATE [darkhastezam] SET [shakhs] = #shakhs, [nam] = #nam, [Idgharardad] = #Idgharardad, [elat] = #elat, [hamrah] = #hamrah, [sabet] = #sabet, [vaziat] = #vaziat, [karshenas] = #karshenas, [tarikh] = #tarikh WHERE [DarkhastId] = #DarkhastId">
<DeleteParameters>
<asp:Parameter Name="DarkhastId" Type="Decimal" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="shakhs" Type="String" />
<asp:Parameter Name="nam" Type="String" />
<asp:Parameter Name="Idgharardad" Type="Decimal" />
<asp:Parameter Name="elat" Type="String" />
<asp:Parameter Name="hamrah" Type="String" />
<asp:Parameter Name="sabet" Type="String" />
<asp:Parameter Name="vaziat" Type="String" />
<asp:Parameter Name="karshenas" Type="String" />
<asp:Parameter Name="tarikh" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="shakhs" Type="String" />
<asp:Parameter Name="nam" Type="String" />
<asp:Parameter Name="Idgharardad" Type="Decimal" />
<asp:Parameter Name="elat" Type="String" />
<asp:Parameter Name="hamrah" Type="String" />
<asp:Parameter Name="sabet" Type="String" />
<asp:Parameter Name="vaziat" Type="String" />
<asp:Parameter Name="karshenas" Type="String" />
<asp:Parameter Name="tarikh" Type="String" />
<asp:Parameter Name="DarkhastId" Type="Decimal" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
you are doing right just get it on the page load of showprofile.aspx like this:
protected void Page_Load(object sender, EventArgs e)
{
string v = Request.QueryString["DarkhastId"];
if (v != null)
{
Response.Write("param is ");
Response.Write(v);
}
}
}
*Update: *
you need to access the DarkhastId from ListView Keys like this in the ListView1_ItemUpdated event:
protected void ListView1_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
if ((sender as ListView) != null)
{
int id = Convert.ToInt32(ListView1.DataKeys[Index]["DarkhastId"]);
string url="showprofile.aspx?DarkhastId="+id
Response.Redirect(url);
}
}
here is reference answer:
http://forums.asp.net/t/1412741.aspx?Get+the+key+value+of+an+updated+ListView+item+in+the+ItemUpdated+event
you can see example here:
http://asp-net-example.blogspot.com/2009/01/aspnet-querystring-example-how-to-use.html

Can't delete record in formview when a value is NULL

I am having problems deleting a record in a FormView that is wired up to a LinqDataSource. The table the LinqDataSource is using allows NULL values for almost all fields. If I create a new record in the FormView and fill in all the values, then try to delete it it works fine. However, if I leave any of the fields blank then save and try to delete, it throws the following exception:
Failed to set one or more properties on type
PRIDE.Info___Employee_Health_History. is not a valid value for
Int16.
The same happens for the real columns in the table. If I leave one of them blank, then save and try to delete I get the same exception with a different type.
My biggest question is, why is it trying to set ANY values when deleting?
Here's my table:
My FormView code:
<asp:FormView ID="fvHealthHistory" runat="server" CssClass="full"
AllowPaging="True" DataSourceID="ldsHealthHistory" DataKeyNames="ID"
oniteminserting="fvHealthHistory_ItemInserting">
<EmptyDataTemplate>
<p>No records found.</p>
<p>
<asp:Button ID="btnNew" runat="server" Text="New Record" CommandName="New" />
</p>
</EmptyDataTemplate>
<InsertItemTemplate>
<table class="pad5">
<tr>
<td class="field-name">Date:</td>
<td>
<asp:TextBox ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate" Format="dd MMM yyyy" />
</td>
</tr>
<tr>
<td class="field-name">Weight:</td>
<td><asp:TextBox ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
</tr>
<tr>
<td class="field-name">Height:</td>
<td><asp:TextBox ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
</tr>
<tr>
<td class="field-name">Pulse:</td>
<td><asp:TextBox ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
</tr>
<tr>
<td class="field-name">Systolic:</td>
<td><asp:TextBox ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
</tr>
<tr>
<td class="field-name">Diastolic:</td>
<td><asp:TextBox ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
</tr>
</table>
<p>
<asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Insert" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</p>
</InsertItemTemplate>
<EditItemTemplate>
<table class="pad5">
<tr>
<td class="field-name">Date:</td>
<td>
<asp:TextBox ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate" Format="dd MMM yyyy" />
</td>
</tr>
<tr>
<td class="field-name">Weight:</td>
<td><asp:TextBox ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
</tr>
<tr>
<td class="field-name">Height:</td>
<td><asp:TextBox ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
</tr>
<tr>
<td class="field-name">Pulse:</td>
<td><asp:TextBox ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
</tr>
<tr>
<td class="field-name">Systolic:</td>
<td><asp:TextBox ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
</tr>
<tr>
<td class="field-name">Diastolic:</td>
<td><asp:TextBox ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
</tr>
</table>
<p>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</p>
</EditItemTemplate>
<ItemTemplate>
<table class="pad5">
<tr>
<td class="field-name">Date:</td>
<td>
<asp:Label ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
</td>
</tr>
<tr>
<td class="field-name">Weight:</td>
<td><asp:Label ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
</tr>
<tr>
<td class="field-name">Height:</td>
<td><asp:Label ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
</tr>
<tr>
<td class="field-name">Pulse:</td>
<td><asp:Label ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
</tr>
<tr>
<td class="field-name">Systolic:</td>
<td><asp:Label ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
</tr>
<tr>
<td class="field-name">Diastolic:</td>
<td><asp:Label ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
</tr>
</table>
<p>
<asp:Button ID="btnNew" runat="server" Text="New" CommandName="New" />
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</p>
</ItemTemplate>
<PagerSettings Mode="NextPreviousFirstLast"
FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;"
NextPageText="Next &gt;" PreviousPageText="&lt; Previous" />
<PagerStyle CssClass="pager" />
</asp:FormView>
The LinqDataSoure's ItemInserting event:
protected void fvHealthHistory_ItemInserting(object sender, FormViewInsertEventArgs e)
{
e.Values["Worker_ID"] = Convert.ToInt32(Request.QueryString["id"]);
}
And finally my LinqDataSource:
<asp:LinqDataSource ID="ldsHealthHistory" runat="server"
ContextTypeName="PRIDE.PRIDEDataContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" EntityTypeName="" OrderBy="Date desc"
TableName="Info___Employee_Health_Histories" Where="Worker_ID == #Worker_ID">
<WhereParameters>
<asp:QueryStringParameter DefaultValue="0" Name="Worker_ID"
QueryStringField="id" Type="Int32" />
</WhereParameters>
<InsertParameters>
<asp:Parameter Name="Date" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Weight" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Height" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Pulse" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Systolic" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Diastolic" ConvertEmptyStringToNull="true" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Date" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Weight" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Height" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Pulse" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Systolic" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Diastolic" ConvertEmptyStringToNull="true" />
</UpdateParameters>
</asp:LinqDataSource>
Does anybody know why it would only let me delete a record if ALL the nullable fields are filled in?

How to insert an image in this ListView that is used for inserting something else?

I am a new ASP.NET developer and I have to create a quiz engine application. I am using a ListView for inserting the questions under specific quiz.
I have the following database design:
QuizContent Table: ID, QuizID, QuestionID, AnswerID
Quiz Table: QuizID, Title, Description
Question Table: QuestionID, Question, QuestionOrder, AnswerExplanation
Answers Table: AnswerID, Answer
QuestionImage: ID, QuestionID, URL
What I want now is making the admin being able to embedd an image to any one of these questions. How can I do that since I have two different tables in the database? One for the questions and one for the images as shown in the design above. I have this design because not all of the questions will have images. Besides that, I may have two or three questions or more having the same image.
I know I should not post too much code or information, but I have to do it to make it clear:
<div align="center">
<asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2"
DataKeyNames="QuestionID" InsertItemPosition="LastItem" OnSelectedIndexChanged="ListView2_SelectedIndexChanged">
<EditItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="UpdateButton" ImageUrl="Images/icons/update24.png" ToolTip="Update" runat="server" CommandName="Update" />
<asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/cancel324.png" ToolTip="Cancel" runat="server" CommandName="Cancel" />
</td>
<td>
<asp:TextBox ID="QuestionTextBox" runat="server"
Text='<%# Bind("Question") %>' />
</td>
<td>
<asp:TextBox ID="QuestionOrderTextBox" runat="server"
Text='<%# Bind("QuestionOrder") %>' />
</td>
<td>
<asp:TextBox ID="AnswerExplanationTextBox" runat="server"
Text='<%# Bind("AnswerExplanation") %>' />
</td>
<%--<td>
<asp:TextBox ID="UrlTextBox" runat="server" Text='<%# Bind("Url") %>' />
</td>--%>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server"
style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="InsertButton" ImageUrl="Images/icons/add24.png" ToolTip="Add" runat="server" CommandName="Insert" />
<asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/clear24.png" ToolTip="Cancel" runat="server" CommandName="Cancel" />
</td>
<td>
<asp:TextBox ID="QuestionTextBox" runat="server"
Text='<%# Bind("Question") %>' />
</td>
<td>
<asp:TextBox ID="QuestionOrderTextBox" runat="server"
Text='<%# Bind("QuestionOrder") %>'/>
</td>
<td>
<asp:TextBox ID="AnswerExplanationTextBox" runat="server"
Text='<%# Bind("AnswerExplanation") %>' />
</td>
<%--<td>
<asp:FileUpload ID="ImageIDUploader" runat="server" size="10" />
</td>--%>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="delete" runat="server" CommandName="Delete" />
<asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />
<asp:ImageButton ID="SelectButton" ImageUrl="Images/icons/select.png" ToolTip="Select" runat="server" CommandName="Select" />
</td>
<td>
<asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
</td>
<td>
<asp:Label ID="QuestionOrderLabel" runat="server"
Text='<%# Eval("QuestionOrder") %>' />
</td>
<td>
<asp:Label ID="AnswerExplanationLabel" runat="server"
Text='<%# Eval("AnswerExplanation") %>' />
</td>
<%--<td>
<a href='<%# Eval("URL") %>' target="_blank">
<asp:Label ID="ImageURLlbl" runat="server" Text='<%# Eval("URL") %>' />
</a>
</td>--%>
</tr>
</ItemTemplate>
<LayoutTemplate>
<div ><table id="thetable" width="97%" cellpadding="0px" cellspacing="0px" style="margin:0px 0px 0px 0px; border:2px solid #003366; font-size:13px; font-weight:bold;">
<thead>
<tr style="background-color:#C6D7B5;">
<th style="border-bottom:2px solid #003366; ">...</th>
<th style="border-bottom:2px solid #003366; ">Question</th>
<th style="border-bottom:2px solid #003366; ">Question Order</th>
<th style="border-bottom:2px solid #003366; ">Answer Explanation</th>
<th style="border-bottom:2px solid #003366; ">Image (URL)</th>
</tr>
</thead>
<tbody><tr id="itemPlaceholder" runat="server"></tr></tbody>
</table></div>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="Delete" runat="server" CommandName="Delete" />
<asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />
</td>
<td>
<asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
</td>
<td>
<asp:Label ID="QuestionOrderLabel" runat="server"
Text='<%# Eval("QuestionOrder") %>' />
</td>
<td>
<asp:Label ID="AnswerExplanationLabel" runat="server"
Text='<%# Eval("AnswerExplanation") %>' />
</td>
<%--<td>
<asp:Label ID="ImageURLlbl" runat="server"
Text='<%# Eval("URL") %>' />
</td>--%>
</tr>
</SelectedItemTemplate>
</asp:ListView>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>"
SelectCommand="SELECT QuestionID, Question, QuestionOrder, AnswerExplanation
FROM dbo.Question
WHERE (QuestionID IN
(SELECT DISTINCT QuestionID
FROM dbo.QuizContent
WHERE (QuizID = #QuizID)))"
DeleteCommand="DELETE FROM [Question] WHERE [QuestionID] = #QuestionID"
InsertCommand="INSERT INTO [Question] ([Question], [QuestionOrder], [AnswerExplanation]) VALUES (#Question, #QuestionOrder, #AnswerExplanation)"
UpdateCommand="UPDATE [Question] SET [Question] = #Question, [QuestionOrder] = #QuestionOrder, [AnswerExplanation] = #AnswerExplanation WHERE [QuestionID] = #QuestionID">
<DeleteParameters>
<asp:Parameter Name="QuestionID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QuestionOrder" Type="Int32" />
<asp:Parameter Name="AnswerExplanation" Type="String" />
<asp:ControlParameter ControlID="ListView1" Name="QuizID" PropertyName="SelectedValue" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QuestionOrder" Type="Int32" />
<asp:Parameter Name="AnswerExplanation" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="ListView1" Name="QuizID" DefaultValue="0"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
So how to do that?
Don't add the image in the same list view, but show a selection of existing images with an option to add another one that opens a popup with the list of all images and an option to upload a new one.
Create a view to combine questions and image:
CREATE VIEW QuestionsView AS
SELECT Question.*, QuestionImage.ID AS ImageID
FROM Question LEFT OUTER JOIN QuestionImage ON Question.QuestionID = QuestionImage.QuestionID
Use this view to select records for your ListView.
To show a DropDownList:
<td>
<asp:DropDownList runat="server" DataSourceID="imagesDataSource" SelectedValue="<%# Bind("ImageID") %>" DataTextField="URL" DataValueField="ID" />
<asp:SqlDataSource runat="server" ID="imagesDataSource" SelectCommand="SELECT * FROM QuestionImage" />
</td>
Change your ListView data source to use a stored procedure for update that gets a parameter for each Bind use.
Add a link to open the images list popup. For the popup you can use one of the samples here: http://smashingspy.com/32-best-jquery-popup-window-dialog-box-example/

Categories

Resources