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
Related
I am receiving the error 'TextBox1 does not exist in this context'
I am new to c# and totally lost as to the problem.
As you can see in my code I would like to use Textbox rather than all the labels.
At present I am working on localhost while testing; not sure if this makes any difference.
My thoughts are the code file is being executed before the Formview has created the controls.
my ASPX file
<%# Page language="C#" masterpagefile="WebBuilder.master" CodeFile="~/Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Bodyhead" ContentPlaceHolderID="BodyHead" runat="server">
<div align="center" style="background-position: center; background-image: url('Images/Rishton-waterfall-Edited.png'); height: 235px; background-repeat: no-repeat; background-color: #FFFFFF; top: 10px;">
</div>
</asp:Content>
<asp:Content ID="NavTop" ContentPlaceHolderID="NavTop" runat="server">
<p align="center" style="font-weight: bold"> <%:DateTime.Now.ToString("ddd, dd MMM yyy HH':'mm':'ss 'GMT'") %></p>
<div align="center">
<asp:Menu ID="Menu3"
runat="server"
Orientation="Horizontal"
RenderingMode="Table"
BackColor="Blue"
ForeColor="Yellow"
Font-Size="14pt"
Width="100%" >
<DynamicHoverStyle ForeColor="Black" Font-Size="16pt" />
<DynamicMenuItemStyle ForeColor="Black" />
<Items>
<asp:MenuItem Text="Home" Value="Home" NavigateUrl="~/Default.aspx"></asp:MenuItem>
<asp:MenuItem Text="Rishton History" Value="Rishton History" NavigateUrl="~/Default.aspx?Page=History"></asp:MenuItem>
<asp:MenuItem Text="Living in Rishton" Value="Living in Rishton" NavigateUrl="">
<asp:MenuItem Text="Streets" Value="Streets" ToolTip="All the streets found in Rishton">
<asp:MenuItem Text="Spring St" Value="Spring St" NavigateUrl="Default.aspx?Page=Spring"></asp:MenuItem>
<asp:MenuItem Text="Livesey St" Value="Livesey St" NavigateUrl="Default.aspx?Page=Livesey"></asp:MenuItem>
<asp:MenuItem Text="High St" Value="High St" NavigateUrl="Default.aspx?Page=High"></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Weather" Value="Weather" NavigateUrl=""></asp:MenuItem>
</Items>
<StaticHoverStyle Font-Size="16pt" ForeColor="Black" />
</asp:Menu>
</div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" SiteMapProvider="MySiteMap" />
<asp:Menu
ID="Menu2" runat="server" CssClass="menu" Visible="false"
EnableViewState="false" IncludeStyleBlock="false" StaticDisplayLevels="2"
Orientation="Horizontal" DataSourceID="SiteMapDataSource1" >
</asp:Menu>
<asp:SiteMapPath ID="SiteMapPath1" runat="server" SiteMapProvider="MySiteMap" Font-Names="Verdana" Font-Size="14pt" PathSeparator=" > ">
<CurrentNodeStyle ForeColor="Black" />
<NodeStyle Font-Bold="True" ForeColor="Blue" />
<PathSeparatorStyle Font-Bold="True" ForeColor="Red" />
<RootNodeStyle Font-Bold="True" ForeColor="Red" />
</asp:SiteMapPath>
<p align="center" style="border-style: outset">Interactive Website Information Bar</p>
</asp:Content>
<asp:Content ID="Content" ContentPlaceHolderID="Content" runat="server">
<h1 align="center">Rishton</h1>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [AllSite] WHERE ([Keyword] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="Keyword" QueryStringField="Page" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView runat="server" id="FormView1" DataKeyNames="ID" DataSourceID="SqlDataSource1" >
<ItemTemplate>
<h2><asp:Label id="TitleLabel" runat="server" Text='<%# Bind("Title") %>' /></h2>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server" Text="Test" ></asp:TextBox>
<br />
<asp:Label id="Para1Label" runat="server" Text='<%# Bind("Para1") %>' />
<br /> <br />
<asp:Label id="Label1" runat="server" Text='<%# Bind("Para2") %>' />
<br /> <br />
<asp:Label id="Label2" runat="server" Text='<%# Bind("Para3") %>' />
<br /> <br />
<asp:Label id="Label3" runat="server" Text='<%# Bind("Para4") %>' />
<br /> <br />
<asp:Label id="Label4" runat="server" Text='<%# Bind("Para5") %>' />
<br /> <br />
<asp:Label id="Label5" runat="server" Text='<%# Bind("Para6") %>' />
<br /> <br />
<asp:Label id="Label6" runat="server" Text='<%# Bind("Para7") %>' />
<br /> <br />
<asp:Label id="Label7" runat="server" Text='<%# Bind("Para8") %>' />
<br /> <br />
<asp:Label id="Label10" runat="server" Text='<%# Bind("Para9") %>' />
<br /> <br />
<asp:Label id="Label11" runat="server" Text='<%# Bind("Para10") %>' />
<br /> <br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("ImageLink") %>' Width="640px" Height="320px" />
<br /> <br />
</ItemTemplate>
</asp:FormView>
</asp:Content>
<asp:Content ID="Footer" ContentPlaceHolderID="Foot" runat="server">
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [AllSite] WHERE ([Keyword] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="Keyword" QueryStringField="Page" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<h1>References</h1>
<asp:FormView runat="server" id="FormView2" DataKeyNames="ID" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:Label id="Refs1" runat="server" Text='<%# Bind("Refs1") %>' />
<br /> <br />
<asp:Label id="Refs2" runat="server" Text='<%# Bind("Refs2") %>' />
<br /> <br />
<asp:Label id="Refs3" runat="server" Text='<%# Bind("Refs3") %>' />
<br /> <br />
<asp:Label id="Refs4" runat="server" Text='<%# Bind("Refs4") %>' />
<br /> <br />
<asp:Label id="Refs5" runat="server" Text='<%# Bind("Refs5") %>' />
<br /> <br />
<asp:Label id="Refs6" runat="server" Text='<%# Bind("Refs6") %>' />
<br /> <br />
<asp:Label id="Refs7" runat="server" Text='<%# Bind("Refs7") %>' />
<br /> <br />
<asp:Label id="Refs8" runat="server" Text='<%# Bind("Refs8") %>' />
<br /> <br />
<asp:Label id="Refs9" runat="server" Text='<%# Bind("Refs9") %>' />
<br /> <br />
<asp:Label id="Refs10" runat="server" Text='<%# Bind("Refs10") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<p align="center">© <%: DateTime.Now.Year %> - Website Builder Ver 1.0 </p>
</asp:Content>
my aspx.cs file
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void btn_Click(object sender, EventArgs e,)
{
string txt = File.ReadAllText(Server.MapPath("~/Text/RishtonHistory.txt"));
TextBox1 = txt;
}
}
Thanks in advance for any help.
You should access to FormView1 then its children named TextBox1. You can access like this
string txt = File.ReadAllText(Server.MapPath("~/Text/RishtonHistory.txt"));
//TextBox1 = txt;
TextBox textBox1 = ((TextBox)FormView1.FindControl("TextBox1"));
textBox1.Text = txt;
It does not matter your are on localhost or server.
I'm new to coding, so sorry if some of my jargon is wrong.
I'm trying to make an update page that will update database values. All other fields update fine, however whenever I attempt to update the database without an image upload, it will replace the current field value with a null value. Any help would be appreciated to remedy this issue. Exact code would be excellent, as I said before I'm new to coding, especially asp.net and C# so some terms are new to me.
Below is my code behind file, I imagine this is the cause of my problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class admin_updatenews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void displayedit_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
info.Text = "Item Updated";
FileUpload fileupdate = displayedit.EditItem.FindControl("imageupdate") as FileUpload;
Label recordid = displayedit.EditItem.FindControl("idlabel1") as Label;
Int32 id = Convert.ToInt32(recordid.Text);
if (fileupdate.HasFile)
{
String fupload = fileupdate.FileName;
Random r = new Random();
int rInt = r.Next(0, 10000);
String imgpath = "../images/" + rInt + fupload;
fileupdate.SaveAs(Server.MapPath(imgpath));
String newimage = rInt + fupload;
string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString;
SqlConnection myConnection = new SqlConnection(newsconnection);
//myConnection.ConnectionString is now set to connectionString.
myConnection.Open();
String query = "UPDATE News SET postimage ='" + newimage + "', Image ='" + newimage + "' WHERE id='" + id + "'";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
protected void displayedit_ItemEditing(object sender, ListViewEditEventArgs e)
{
info.Text = "I am editing";
}
protected void displayedit_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
info.Text = "Not Updating";
}
}
This is the front end code in case that is required
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Copy of updatenews.aspx.cs" Inherits="admin_updatenews" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="../css/responsive.css" rel='stylesheet' type='text/css' />
<link href="../css/gui.css" rel='stylesheet' type='text/css' />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="newseditrows" runat="server"
ConnectionString='<%$ ConnectionStrings:newsconnection %>'
SelectCommand="SELECT News.id, News.headline, News.Image, News.paragraph, Sportslist.sportname, News.Sport, News.date, News.lead, News.authorID, Sportslist.id, News.postheadline, News.postimage, News.postparagraph, News.postsport, News.postdate, News.postlead, News.postauthorid
FROM News
INNER JOIN Sportslist ON News.Sport = Sportslist.id
ORDER BY News.id DESC"
UpdateCommand="UPDATE [News] SET [headline]=#headline, [Image]=#Image, [paragraph]=#paragraph, [Sport]=#sport, [date]=#date, [lead]=#lead, [authorid]=#authorid, [postheadline]=#headline, [postimage]=#Image, [postparagraph]=#paragraph, [postsport]=#sport, [postdate]=#date, [postlead]=#lead, [postauthorid]=#authorid WHERE [id]=#id">
<UpdateParameters>
<asp:Parameter Name="headline" Type="String" />
<asp:Parameter Name="Image" Type="String" />
<asp:Parameter Name="paragraph" Type="String" />
<asp:Parameter Name="Sport" Type="Int32" />
<asp:Parameter Name="date" Type="DateTime" />
<asp:Parameter Name="lead" Type="String" />
<asp:Parameter Name="authorid" Type="Int32" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sportlist" runat="server"
ConnectionString='<%$ ConnectionStrings:newsconnection %>'
SelectCommand="SELECT [id], [sportname] FROM [Sportslist]">
</asp:SqlDataSource>
<asp:Label ID="info" runat="server" Text="Not Updating"></asp:Label>
<br />
<asp:ListView ID="displayedit" runat="server"
DataSourceID="newseditrows"
DataKeyNames="id"
OnItemUpdated="displayedit_ItemUpdated"
OnItemEditing="displayedit_ItemEditing"
OnItemCanceling="displayedit_ItemCanceling">
<AlternatingItemTemplate>
<span style="">id:
<asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
headline:
<asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
Image:
<asp:Image ID="ImageLabel" runat="server" ImageURL='<%# "../images/" + Eval("Image") %>' Width="100px" />
<br />
paragraph:
<asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
Sport:
<asp:Label Text='<%# Eval("sportname") %>' runat="server" ID="SportLabel" /><br />
date:
<asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
lead:
<asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
authorID:
<asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
<asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
<br />
<br />
</span>
</AlternatingItemTemplate>
<EditItemTemplate>
<span style="">id:
<asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel1" /><br />
headline:
<asp:TextBox Text='<%# Bind("headline") %>' runat="server" ID="headlineTextBox" /><br />
Image:
<asp:Image ID="ImageTextBox1" runat="server" ImageUrl='<%# "../images/" + Eval("Image") %>' Width="100px"/>
<asp:FileUpload ID="imageupdate" runat="server" />
<br />
paragraph:
<asp:TextBox Text='<%# Bind("paragraph") %>' runat="server" ID="paragraphTextBox" /><br />
Sport:
<%--<asp:TextBox Text='<%# Bind("Sport") %>' runat="server" ID="SportTextBox" />--%>
<asp:DropDownList ID="SportsDropdown" runat="server" SelectedValue='<%# Bind("Sport") %>'>
<asp:ListItem Value="1">Football</asp:ListItem>
<asp:ListItem Value="2">Rugby</asp:ListItem>
<asp:ListItem Value="3">Basketball</asp:ListItem>
<asp:ListItem Value="4">Motorsport</asp:ListItem>
<asp:ListItem Value="5">NFL</asp:ListItem>
<asp:ListItem Value="6">Cricket</asp:ListItem>
<asp:ListItem Value="7">Tennis</asp:ListItem>
<asp:ListItem Value="8">Golf</asp:ListItem>
<asp:ListItem Value="9">Other</asp:ListItem>
</asp:DropDownList>
<br />
lead:
<asp:TextBox Text='<%# Bind("lead") %>' runat="server" ID="leadTextBox" /><br />
<asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" /><asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
<br />
</span>
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">headline:
<asp:TextBox Text='<%# Bind("headline") %>' runat="server" ID="headlineTextBox" /><br />
Image:
<asp:TextBox Text='<%# Bind("Image") %>' runat="server" ID="ImageTextBox" /><br />
paragraph:
<asp:TextBox Text='<%# Bind("paragraph") %>' runat="server" ID="paragraphTextBox" /><br />
Sport:
<asp:TextBox Text='<%# Bind("Sport") %>' runat="server" ID="SportTextBox" /><br />
date:
<asp:TextBox Text='<%# Bind("date") %>' runat="server" ID="dateTextBox" /><br />
lead:
<asp:TextBox Text='<%# Bind("lead") %>' runat="server" ID="leadTextBox" /><br />
authorID:
<asp:TextBox Text='<%# Bind("authorID") %>' runat="server" ID="authorIDTextBox" /><br />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
<br />
</span>
</InsertItemTemplate>
<ItemTemplate>
<span style="">id:
<asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
headline:
<asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
Image:
<asp:Image ID="ImageLabel" runat="server" ImageURL='<%# "../images/" + Eval("Image") %>' Width="100px" />
<br />
paragraph:
<asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
Sport:
<asp:Label Text='<%# Eval("sportname") %>' runat="server" ID="SportLabel" /><br />
date:
<asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
lead:
<asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
authorID:
<asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
<asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
<br />
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
<div style="">
<asp:DataPager runat="server" ID="DataPager1">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
<asp:NumericPagerField></asp:NumericPagerField>
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">id:
<asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
headline:
<asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
Image:
<asp:Label Text='<%# Eval("Image") %>' runat="server" ID="ImageLabel" /><br />
paragraph:
<asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
Sport:
<asp:Label Text='<%# Eval("Sport") %>' runat="server" ID="SportLabel" /><br />
date:
<asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
lead:
<asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
authorID:
<asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
<asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
<br />
<br />
</span>
</SelectedItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
Here is example of the update statement using ur query.
UPDATE News
SET postimage = ISNULL(YourNewValue, postimage), Image = ISNULL(YourNewValue, Image)
WHERE id = YourID
EDIT :
try adding following in your datasource updateCommand. if this doesn't solve than try stepping through to see where this is adding null.
UpdateCommand="UPDATE [News] SET [headline]=#headline, [Image]= ISNULL(#Image, [Image]), [paragraph]=#paragraph, [Sport]=#sport, [date]=#date, [lead]=#lead, [authorid]=#authorid, [postheadline]=#headline, [postimage]=#Image, [postparagraph]=#paragraph, [postsport]=#sport, [postdate]=#date, [postlead]=#lead, [postauthorid]=#authorid WHERE [id]=#id">
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="<< First" LastPageText="Last >>"
NextPageText="Next >" PreviousPageText="< 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?
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/
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"