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.
Related
I'm using the latest ajax toolkit version. I've tried to put the rating control in a datalist, but the OnChanged event for some reason isn't firing.
That's the normal ASPX.NET page:
<asp:DataList ID="DataListEpisodes" runat="server" Style="position: relative; bottom: 330px;" Visible="False" RepeatColumns="1" OnItemDataBound="DataListEpisodes_ItemDataBound" DataKeyField="EpisodeID" OnItemCommand="DataListEpisodes_ItemCommand">
<ItemTemplate>
<table>
<tr>
<td>
<td>
<asp:Image ID="ImageEpisodeImage" class="EpisodeImage" runat="server" ImageUrl='<%# Eval("EpisodeImage", "https://image.tmdb.org/t/p/original{0}") %>' />
</td>
</td>
<td>
<asp:Label ID="LabelEpisodeNumber" runat="server" Style="font-family: Gotham Ultra; font-size: 25px; font-weight: bold;"
Text='<%# Eval("SeasonNumber", "Season {0}") + ", " + Eval ("EpisodeNumber", "Episode {0}") %>'></asp:Label>
<br />
<asp:Label ID="LabelEpisodeName" Style="font-family: Proxima Nova Semibold; font-size: 20px; font-weight: bold;" runat="server" Text='<%# Bind("EpisodeName") %>'></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" class="LabelOverview" Text='<%# Bind("EpisodeOverview") %>' TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="ButtonAddEpisode" class="ButtonAddEpisode" runat="server" Text="Add Episode To Watch History" CommandName="AddEpisode" />
<asp:Button ID="ButtonRemoveEpisode" class="ButtonAddEpisode" runat="server" Text="Remove Episode From Watch History" CommandName="RemoveEpisode" />
<br />
<asp:Button ID="ButtonWatchListAdd" class="ButtonEpisodeWatchList" runat="server" Text="Add Episode To Future Watch List" CommandName="AddWatchList" />
**<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<ajaxToolkit:Rating ID="Rating1" OnChanged="OnRatingChanged" style="position: relative;right: 301px;top: 16px;" AutoPostBack="true" runat="server"
StarCssClass="Star" EmptyStarCssClass="Star"
FilledStarCssClass="FilledStar" WaitingStarCssClass="Star">
</ajaxToolkit:Rating>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Rating1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>**
</tr>
</table>
<br />
<br />
<br />
<br />
</ItemTemplate>
</asp:DataList>
and that's the codebehind:
protected void OnRatingChanged(object sender, AjaxControlToolkit.RatingEventArgs e)
{
int rowIndex = ((sender as AjaxControlToolkit.Rating).NamingContainer as DataListItem).ItemIndex;
int RatingChosed = Rating1.CurrentRating;
int EpisodeID = Convert.ToInt32(DataListEpisodes.DataKeys[rowIndex].ToString());
}
Thank you if someone can help me :D
I am using 2 link buttons (Coordinator and Staff) which on_Click action displays corresponding panels asking for login/signup/forgotpassword. If the user select signup/forgotpassword a popup windows is popped (using ModalPopupExtender) asking for information. Everything worked fine unless and until I used required field validator within the panel which pops up when the user click on signup/forgotpassword. If I use required field validator my page run fine and when I click on any of the link button (Coordinator/Staff) nothing happens.
This is aspx code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type = "text/css">
.modalBackground
{
background-color: gray;
}
.modalPopup
{
background-color: white;
border-width: 3px;
border-style: solid;
border-color: black;
padding-top: 10px;
padding-left: 10px;
width: 300px;
height: 140px;
}
.panel
{
background : gray;
padding : 10px;
}
div ul ul
{
display: none;
}
div ul li:hover > ul
{
display: block;
}
div ul li ul:hover > ul
{
display: block;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="XX-Large" Text="Automated Examination System"></asp:Label><br /> <br /><br />
<asp:Panel ID = "MainPanel" runat = "server" Height="181px" Width="275px">
<ul>
<li style="margin-left: 33px; width: 169px;"> <asp:LinkButton ID = "LinkAdmin" runat = "server" onclick="LinkAdmin_Click">Coordinator</asp:LinkButton></li>
<li style="width: 172px; margin-left: 33px"> <asp:LinkButton ID="LinkStaff" runat="server" onclick="LinkStaff_Click">Staff</asp:LinkButton></li>
<li style="margin-left: 33px"> <asp:LinkButton ID = "LinkStudent" runat = "server">Student</asp:LinkButton>
<ul>
<li><asp:LinkButton ID = "LinkButton1" runat = "server">Fresh Registration</asp:LinkButton></li>
<li><asp:LinkButton ID = "LinkButton2" runat = "server">Re Registration</asp:LinkButton></li>
</ul>
</li>
</ul>
</asp:Panel>
<asp:Panel ID="InfoPanel" runat="server" style="left: 290px; top: 104px; position: absolute; height: 375px; width: 611px"><br /><br /><br />
<asp:Label ID="Label2" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="Medium" Text="The Automated Examination system will allow post graduation student and under graduation students to register their respective courses for their respective semester. "></asp:Label>
<asp:Label ID="Label3" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="Medium" Text="Once the registration process is completed for a student the system sends information to the controller of examination."></asp:Label><br />
<asp:Label ID="Label4" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="Medium" Text="Each staff will be able to enter the CIE1 and CIE2 marks based on the privileges provided by coordinator of that respective department. "></asp:Label><br />
<asp:Label ID="Label5" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="Medium" Text="Once the staff enters marks for CIE1/CIE2, they won’t be allowed to make changes later on. "></asp:Label><br />
<asp:Label ID="Label6" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="Medium" Text="After submitting the marks, the staff will be given with print out of the entered marks for further reference. "></asp:Label><br />
<asp:Label ID="Label7" runat="server" Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Size="Medium" Text="At the end of a semester each student SGPA is calculated and the controller take a back up copy of grade cards and result sheet of all the students. "></asp:Label><br />
<asp:Label ID="Label9" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="Medium" Text="Since student grades are sensitive information certain measures are taken to prevent unauthorized access. "></asp:Label><br />
<asp:Label ID="Label8" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="Medium" Text="The system also generate the transcript of student which contains all semester marks, Course Title in that particular semester, SGPA of each semester and overall CGPA of that student. "></asp:Label>
</asp:Panel>
<asp:Panel ID="AdminLogin" runat="server" style= "left: 906px; top: 105px; position: absolute; " Visible="False" Width="430px"><br/>
<asp:Label ID="Label10" runat="server" Text="Coordinator Login"></asp:Label><br /><br />
<asp:Label ID="Label11" runat="server" Text="Select Department : "></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" ValidationGroup="AdminLogin"></asp:DropDownList>
<asp:RequiredFieldValidator ID="AdminReq1" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Department is blank" ForeColor="Red" ValidationGroup="AdminLogin"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label12" runat="server" Text="Select Branch : "></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server" ValidationGroup="AdminLogin"></asp:DropDownList>
<asp:RequiredFieldValidator ID="AdminReq2" runat="server" ControlToValidate="DropDownList2" ErrorMessage="Branch is blank" ForeColor="Red" ValidationGroup="AdminLogin"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label13" runat="server" Text="Username : "></asp:Label>
<asp:TextBox ID="AdminTxt1" runat="server" ValidationGroup="AdminLogin" style="height: 22px"></asp:TextBox>
<asp:RequiredFieldValidator ID="AdminReq3" runat="server" ControlToValidate="AdminTxt1" ErrorMessage="Username is blank" ForeColor="Red" ValidationGroup="AdminLogin"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label14" runat="server" Text="Password : "></asp:Label>
<asp:TextBox ID="AdminTxt2" runat="server" TextMode="Password" ValidationGroup="AdminLogin"></asp:TextBox>
<asp:RequiredFieldValidator ID="AdminReq4" runat="server" ControlToValidate="AdminTxt2" ErrorMessage="Password is blank" ForeColor="Red" ValidationGroup="AdminLogin"></asp:RequiredFieldValidator><br /><br />
<asp:Button ID="AdminLog" runat="server" Text="Login" Width="80px" ValidationGroup="AdminLogin" />
<asp:Button ID="AdminSignUp" runat="server" Text="New Coordinator" Width="110px"/>
<asp:Button ID="AdminForPwd" runat="server" Text="Forgot Password" />
</asp:Panel>
<asp:Panel ID="StaffLogin" runat="server" style="left: 906px; top: 105px; position: absolute;" Visible="False" Width="430px"><br />
<asp:Label ID="Label15" runat="server" Text="Staff Login"></asp:Label><br /><br />
<asp:Label ID="Label16" runat="server" Text="Username : "></asp:Label>
<asp:TextBox ID="StaffTxt1" runat="server" Width="127px" ValidationGroup="StaffLogin"></asp:TextBox>
<asp:RequiredFieldValidator ID="StaffReq1" runat="server" ControlToValidate=" StaffTxt1" ErrorMessage="Username is blank" ForeColor="Red" ValidationGroup="StaffLogin"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label17" runat="server" Text="Password : "></asp:Label>
<asp:TextBox ID="StaffTxt2" runat="server" TextMode="Password" ValidationGroup="StaffLogin"></asp:TextBox>
<asp:RequiredFieldValidator ID="StaffReq2" runat="server" ControlToValidate=" StaffTxt2" ErrorMessage="Password is blank" ForeColor="Red" ValidationGroup="StaffLogin"></asp:RequiredFieldValidator><br /><br />
<asp:Button ID="StaffLog" runat="server" Text="Login" Width="80px" ValidationGroup="StaffLogin" />
<asp:Button ID="StaffSignUp" runat="server" Text="New Staff" Width="82px"/>
<asp:Button ID="StaffForPwd" runat="server" Text="Forgot Password" Width="110px" />
</asp:Panel>
<asp:Panel ID="StaffSP" CssClass="modalPopup" style="display: none" runat="server">
<asp:Label ID="Label18" runat="server" Text="Staff Sign Up" Font-Size="XX-Large"></asp:Label><br /><br />
<asp:Label ID="Label19" runat="server" Text="Enter Course Code : "></asp:Label>
<asp:TextBox ID="StaffSPTxt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="StaffSPReq1" runat="server" ErrorMessage="Course Code is BLANK" ForeColor="Red" ControlToValidate="StaffSPTxt1"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label20" runat="server" Text="Enter Password : "></asp:Label>
<asp:TextBox ID="StaffSPTxt2" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="StaffSPReq2" runat="server" ErrorMessage="Password is BLANK" ForeColor="Red" ControlToValidate="StaffSPTxt2"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label21" runat="server" Text="Retype Password : "></asp:Label>
<asp:TextBox ID="StaffSPTxt3" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="StaffSPCom1" runat="server" ErrorMessage="Password Mismatch" ForeColor="Red" ControlToCompare="StaffSPTxt2" ControlToValidate="StaffSPTxt3"></asp:CompareValidator><br /><br />
<asp:Label ID="Label22" runat="server" Text="Enter Password Hint : "></asp:Label>
<asp:TextBox ID="StaffSPTxt4" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="StaffSPReq3" runat="server" ErrorMessage="Password Hint is BLANK" ForeColor="Red" ControlToValidate="StaffSPTxt4"></asp:RequiredFieldValidator><br /><br />
<asp:Button ID="Button3" runat="server" Text="Sign Up" />
<asp:Button ID="Button4" runat="server" Text="Cancel" />
</asp:Panel>
<asp:Panel ID="StaffFP" CssClass="modalPopup" style="display: none" runat="server">
<asp:Label ID="Label23" runat="server" Text="Staff Forgot Password" Font-Size="XX-Large"></asp:Label><br /><br />
<asp:Label ID="Label24" runat="server" Text="Enter Username : "></asp:Label>
<asp:TextBox ID="StaffFPTxt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="StaffFPReq1" runat="server" ErrorMessage="Username is BLANK" ForeColor="Red" ControlToValidate="StaffFPTxt1"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label25" runat="server" Text="Enter Password Hint : "></asp:Label>
<asp:TextBox ID="StaffFPTxt2" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="StaffFPReq2" runat="server" ErrorMessage="Hint is BLANK" ForeColor="Red" ControlToValidate="StaffFPTxt2"></asp:RequiredFieldValidator><br /><br />
<asp:Button ID="Button5" runat="server" Text="GetPassword" />
<asp:Button ID="Button6" runat="server" Text="Cancel" />
</asp:Panel>
<asp:Panel ID="AdminSP" CssClass="modalPopup" style="display: none" runat="server">
<asp:Label ID="Label26" runat="server" Text="Coordinator Sign Up" Font-Size="XX-Large"></asp:Label><br /><br />
<asp:Label ID="Label27" runat="server" Text="Select Department : "></asp:Label>
<asp:DropDownList ID="AdminSPDrp1" runat="server" ></asp:DropDownList>
<asp:RequiredFieldValidator ID="AdminSPReq1" runat="server" ErrorMessage="Department is BLANK" ForeColor="Red" ControlToValidate="AdminSPDrp1"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label28" runat="server" Text="Select Branch : "></asp:Label>
<asp:DropDownList ID="AdminSPDrp2" runat="server" ></asp:DropDownList>
<asp:RequiredFieldValidator ID="AdminSPReq2" runat="server" ErrorMessage="Branch is BLANK" ForeColor="Red" ControlToValidate="AdminSPDrp2"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label29" runat="server" Text="Enter Username : "></asp:Label>
<asp:TextBox ID="AdminSPTxt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="AdminSPReq3" runat="server" ErrorMessage="Username is BLANK" ForeColor="Red" ControlToValidate="AdminSPTxt1"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label30" runat="server" Text="Enter Password : "></asp:Label>
<asp:TextBox ID="AdminSPTxt2" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="AdminSPReq4" runat="server" ErrorMessage="Password is BLANK" ForeColor="Red" ControlToValidate="AdminSPTxt2"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label31" runat="server" Text="Retype Password : "></asp:Label>
<asp:TextBox ID="AdminSPTxt3" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="AdminSPCom1" runat="server" ControlToValidate = "AdminSPTxt3" ControlToCompare = "AdminSPTxt2" ErrorMessage="Password Mismatch" ForeColor="Red"></asp:CompareValidator><br /><br />
<asp:Label ID="Label32" runat="server" Text="Enter Password Hint : "></asp:Label>
<asp:TextBox ID="AdminSPTxt4" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="AdminSPReq5" runat="server" ErrorMessage="Hint is BLANK" ForeColor="Red" ControlToValidate="AdminSPTxt4"></asp:RequiredFieldValidator><br /><br />
<asp:Button ID="Button7" runat="server" Text="Sign Up" />
<asp:Button ID="Button8" runat="server" Text="Cancel" />
</asp:Panel>
<asp:Panel ID="AdminFP" CssClass="modalPopup" style="display: none" runat="server">
<asp:Label ID="Label33" runat="server" Text="Coordinator Forgot Password" Font-Size="XX-Large"></asp:Label><br /><br />
<asp:Label ID="Label34" runat="server" Text="Select Department : "></asp:Label>
<asp:DropDownList ID="AdminFPDrp1" runat="server" ></asp:DropDownList>
<asp:RequiredFieldValidator ID="AdminFPReq1" runat="server" ErrorMessage="Department is BLANK" ForeColor="Red" ControlToValidate="AdminFPDrp1"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label35" runat="server" Text="Select Branch : "></asp:Label>
<asp:DropDownList ID="AdminFPDrp2" runat="server" ></asp:DropDownList>
<asp:RequiredFieldValidator ID="AdminFPReq2" runat="server" ErrorMessage="Branch is BLANK" ForeColor="Red" ControlToValidate="AdminFPDrp2"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label36" runat="server" Text="Enter Username : "></asp:Label>
<asp:TextBox ID="AdminFPTxt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="AdminFPReq3" runat="server" ErrorMessage="Username is BLANK" ForeColor="Red" ControlToValidate="AdminFPTxt1"></asp:RequiredFieldValidator><br /><br />
<asp:Label ID="Label37" runat="server" Text="Enter Password Hint : "></asp:Label>
<asp:TextBox ID="AdminFPTxt2" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="AdminFPReq4" runat="server" ErrorMessage="Hint is BLANK" ForeColor="Red" ControlToValidate="AdminFPTxt2"></asp:RequiredFieldValidator><br /><br />
<asp:Button ID="Button9" runat="server" Text="Sign Up" />
<asp:Button ID="Button10" runat="server" Text="Cancel" />
</asp:Panel>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:ModalPopupExtender ID="ModalPopupExtender1" BackgroundCssClass="modalBackground" PopupControlID = "StaffSP" CancelControlID = "Button4" TargetControlID = "StaffSignUp" runat="server"></asp:ModalPopupExtender>
<asp:ModalPopupExtender ID="ModalPopupExtender2" BackgroundCssClass="modalBackground" PopupControlID = "StaffFP" CancelControlID = "Button6" TargetControlID = "StaffForPwd" runat="server"></asp:ModalPopupExtender>
<asp:ModalPopupExtender ID="ModalPopupExtender3" BackgroundCssClass="modalBackground" PopupControlID = "AdminSP" CancelControlID = "Button8" TargetControlID = "AdminSignUp" runat="server"></asp:ModalPopupExtender>
<asp:ModalPopupExtender ID="ModalPopupExtender4" BackgroundCssClass="modalBackground" PopupControlID = "AdminFP" CancelControlID = "Button10" TargetControlID = "AdminForPwd" runat="server"></asp:ModalPopupExtender>
</div>
</form>
</body>
</html>
This is code behind page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MasterFinal : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkAdmin_Click(object sender, EventArgs e)
{
AdminLogin.Visible = true;
StaffLogin.Visible = false;
}
protected void LinkStaff_Click(object sender, EventArgs e)
{
AdminLogin.Visible = false;
StaffLogin.Visible = true;
}
}
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 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
I had a login page. once user successfuly logged in, they can view and manage their profile/information. This would be done by retrieving data from database and display on a formview.
However this following error appeared inside my userprofile.aspx.cs file:
Exception Details: System.IndexOutOfRangeException: An SqlParameter with ParameterName '#UserId' is not contained by this SqlParameterCollection.
Source Error:
Line 44:
Line 45: // Assign the currently logged on user's UserId to the #UserId parameter
Line 46: e.Command.Parameters["#UserId"].Value = currentUserId;
Line 47:
Line 48: }
Userprofile.aspx:
<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1" DataKeyNames="UserId">
<EditItemTemplate>
UserId:
<asp:Label ID="UserIdLabel1" runat="server" Text='<%# Eval("UserId") %>' />
<br />
Password:
<asp:TextBox ID="PasswordTextBox" runat="server"
Text='<%# Bind("Password") %>' />
<br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
<br />
HomeTown:
<asp:TextBox ID="HomeTownTextBox" runat="server"
Text='<%# Bind("HomeTown") %>' />
<br />
HomepageUrl:
<asp:TextBox ID="HomepageUrlTextBox" runat="server"
Text='<%# Bind("HomepageUrl") %>' />
<br />
Signature:
<asp:TextBox ID="SignatureTextBox" runat="server"
Text='<%# Bind("Signature") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
UserId:
<asp:TextBox ID="UserIdTextBox" runat="server" Text='<%# Bind("UserId") %>' />
<br />
Password:
<asp:TextBox ID="PasswordTextBox" runat="server"
Text='<%# Bind("Password") %>' />
<br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
<br />
HomeTown:
<asp:TextBox ID="HomeTownTextBox" runat="server"
Text='<%# Bind("HomeTown") %>' />
<br />
HomepageUrl:
<asp:TextBox ID="HomepageUrlTextBox" runat="server"
Text='<%# Bind("HomepageUrl") %>' />
<br />
Signature:
<asp:TextBox ID="SignatureTextBox" runat="server"
Text='<%# Bind("Signature") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
UserId:
<asp:Label ID="UserIdLabel" runat="server" Text='<%# Eval("UserId") %>' />
<br />
Password:
<asp:Label ID="PasswordLabel" runat="server" Text='<%# Bind("Password") %>' />
<br />
Email:
<asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email") %>' />
<br />
HomeTown:
<asp:Label ID="HomeTownLabel" runat="server" Text='<%# Bind("HomeTown") %>' />
<br />
HomepageUrl:
<asp:Label ID="HomepageUrlLabel" runat="server"
Text='<%# Bind("HomepageUrl") %>' />
<br />
Signature:
<asp:Label ID="SignatureLabel" runat="server" Text='<%# Bind("Signature") %>' />
<br />
</ItemTemplate>
</asp:FormView>
</p>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SecurityTutorialsConnectionString %>"
onselecting="SqlDataSource1_Selecting"
SelectCommand="SELECT UserProfiles.UserId, aspnet_Membership.Password, aspnet_Membership.Email, UserProfiles.HomeTown, UserProfiles.HomepageUrl, UserProfiles.Signature FROM aspnet_Membership INNER JOIN UserProfiles ON aspnet_Membership.UserId = UserProfiles.UserId">
</asp:SqlDataSource>
</p>
<p>
</p>
</asp:Content>
Userprofile.aspx.cs:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
// Get a reference to the currently logged on user
MembershipUser currentUser = Membership.GetUser();
// Determine the currently logged on user's UserId value
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
// Assign the currently logged on user's UserId to the #UserId parameter
e.Command.Parameters["#UserId"].Value = currentUserId;
}
Create a new SqlParameter and add it to the collection.
SqlParameter param = new SqlParameter("#UserId", currentUserId);
e.Command.Parameters.Add(param);
Try the following:
DbParameter param = e.Command.CreateParameter();
param.ParameterName = "#UserId";
param.Value = currentUserId;
e.Command.Parameters.Add(param);
I didn't test this though
You must add the parameter with
e.Command.Parameters.AddWithValue("#UserId", currentUserId);
Once you have added it you could access it through the indexer as in your example.
UPDATE
If you are working with the System.Data.Common namespace, the AddWithValue method is not available. You will have to do something like
var param = e.Command.CreateParameter("#UserId", currentUserId);
e.Command.Parameters.Add(param);
This is a little bit more complicated but has the advantage that you do not have to implicitly create a parameter of a specific type like SqlParamter or OleDbParameter.
If you already have the parameter listed in your SqlDataSource, then simply point to it and change it's value ...
e.Command.Parameters["#UserId"].Value = currentUserId;
You do not need to create a parameter, unless you have not listed it back in your ASP page.