I'm trying to update text in an SQL database using a text box. This is for an admin to overwrite the data in the database. Having trouble declaring one of the variables.
The itemnametext doesn't exist in the current context.
Code for itemediting page:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>elmtree - Admin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" />
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="../styles/mylist.css" />
</head>
<body>
<form id="form1" runat="server">
<img src="images/ELleft.png" style="width:226px; height:52px; margin-top: 3px; margin-left: 17px; text-align: justify; float: none;"/></a></li>
<div class="container">
<h1> Item Edit </h1> </div>
<div class="container">
<div class="form-group">
<label class="col-sm-2 control-label">Item name: </label>
<div class="col-md-4">
<asp:TextBox ID="itemnameedit" runat="server" Text="" CssClass="form-control">
</asp:TextBox>
</div>
<div class="pull-right">
<asp:Button CssClass="btn btn-primary btn-lg" ID="updatebutton" role="button" runat="server" Text="save" OnClick="updatebutton_Click" />
</div>
</div>
</div>
</form>
</body>
</html>
Code for itemediting.aspx.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class admin_itemediting : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
int row = 0;
if (Request.QueryString["itemID"] != null){
row = int.Parse(Request.QueryString["itemID"]);
}
else{
Response.Redirect("itemedit.aspx");
}
string connectionString = WebConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string query = "SELECT * FROM reports WHERE ID=#rowid";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("#rowid", row);
SqlDataReader rdr = myCommand.ExecuteReader();
while (rdr.Read())
{
string myname = rdr["itemname"].ToString();
itemnametext.Text = myname;
}
}
protected void updatebutton_Click(object sender, EventArgs e){
string connectionString = WebConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string itemnametextupdate = itemnametext.Text;
string query = "UPDATE reports SET itemname = #itemnewname";
}
}
}
Your textbox name is itemnameedit so itemnametext is not found
<asp:TextBox ID="itemnametext" runat="server" Text="" CssClass="form-control"></asp:TextBox>
EDIT: Your aspx page is Default and you are posting code for itemediting.aspx.cs.Post the code for itemediting.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Default" %>
Just change ID of your texbox from itemnameedit to itemnametext
Update: change your reference here to match the codefile and codefile class. I dont think you have right reference, so codefile class admin_itemediting don't know any of your control in the aspx file.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="itemediting.aspx.cs" Inherits="admin_itemediting" %>
Related
i have this little proyect that brings a little window to take the data from the "modal" window to the main but i have problems putting in to work.
The index:
%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebTest.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript" src="Ventana.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="valor1" runat="server" />
<asp:HiddenField ID="valor2" runat="server" />
<asp:TextBox ID="txtIdRutPersona" runat="server"></asp:TextBox>
<asp:Button ID="btnAbrirPopup" runat="server" Text="Abrir" />
</div>
</form>
</body>
</html>
the .cs part of index:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnAbrirPopup.Attributes.Add("onclick", "javascript:Asistencia('txtIdRutPersona');");
}
}
}
}
the "persona" window that is the page that bind the data to the index:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Persona.aspx.cs" Inherits="WebTest.Persona" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript" src="Ventana.js"></script>
<script type="text/javascript">
function cerrar()
{
self.close();
}
</script>
</head>
<body>
<form id="Buscar" method="post" runat="server">
<div>
<asp:TextBox ID="txtRut" runat="server"></asp:TextBox>
<asp:Button ID="btnOk" runat="server" Text="Aplicar" />
<asp:Button ID="btnCerrar" runat="server" Text="Cerrar" />
</div>
</form>
</body>
</html>
the .cs part of "persona":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest
{
public partial class Persona : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["form"] = Request.QueryString["formname"];
ViewState["txtRut"] = Request.QueryString["txtRut"];
ViewState["postBack"] = Request.QueryString["postBack"];
btnOk.Attributes.Add("onClick", $"window.opener.SetCodigo({ViewState["form"]},{ViewState["txtRut"]},{ViewState["postBack"]})");
btnCerrar.Attributes.Add("onClick", "cerrar()");
}
}
}
}
and the js:
var VentanaOrigen;
function Asistencia(txtRut) {
popUp = window.open('Persona.aspx?formname=' + document.forms[0].name + '&txtRut=' + txtRut, '', 'width=430,height=300,left=200,top=150,resizable=yes,status=yes,scrollbars=yes');
}
function SetCodigo(formulario, txtIdRutPersona, IdRutPersona, IPostback) {
eval('var theform = document.' + formulario + ';');
VentanaOrigen.close();
theform.elements[txtIdRutPersona].value = IdRutPersona;
if (IPostback)
__doPostBack(txtIdRutPersona, '');
}
sorry to bother with the large text but im kinda frustated right now, thanks!
It's difficult what you're trying to achieve. I am guessing:
index.aspx has a button to load a popup (which is not modal)
The button passes in the ID of textbox txtIdRutPersona
Index.aspx
<input type="button" value="Abrir" onclick="Asistencia('<%= txtIdRutPersona.ClientID %>')" />
Use client ID in case ASP.NET generates a different ID. Also you should get errors if you change ID (on a later date).
<script type="text/javascript">
function Asistencia(txtIdRutPersonaID) {
popUp = window.open('Persona.aspx?txtIdRutPersonaID=' + txtIdRutPersonaID, '', 'width=430,height=300,left=200,top=150,resizable=yes,status=yes,scrollbars=yes');
}
</script>
persona.aspx
<asp:TextBox ID="txtRut" runat="server"></asp:TextBox>
<input type="button" value="Aplicar" onclick="SetCodigo('<%= TxtIdRutPersonaID %>', '<%= txtRut.ClientID %>')" />
<input type="button" value="Cerrar" onclick="cerrar()" />
You can send the data to index.aspx using window.opener and the ID from index.aspx (txtIdRutPersonaID)
<script type="text/javascript">
function SetCodigo(txtIdRutPersonaID, txtRutID) {
if (window.opener != null && !window.opener.closed) {
var txtIdRutPersona = window.opener.document.getElementById(txtIdRutPersonaID);
// txtIdRutPersona is the textbox from index.aspx
txtIdRutPersona.value = document.getElementById(txtRutID).value;
}
window.close();
}
function cerrar()
{
self.close();
}
</script>
persona.aspx.cs
public string TxtIdRutPersonaID
{
get
{
return Request.QueryString["txtIdRutPersonaID"];
}
}
Alternatively you could use ASP.NET code behind to generate onclick event (but theres no good reason to). E.g:
btnOk.Attributes.Add("onclick", $"SetCodigo('{txtIdRutPersonaID}','{txtRut.ClientID}')");
Likewise, theres no reason to use ViewState["form"], ViewState["txtRut"] & ViewState["postBack"]
i have write a code to fetch the video from the server folder and play it on web page. i am using flowplayer.it shows the video player with controls but not playing the video.what could be the problem . code is attached
Code in aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebsiteTaskAsp.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="FlowPlayer/flowplayer-3.2.12.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 73px">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Label ID="lblFilename" runat="server" Text="File Name :"></asp:Label>
<asp:Label ID="lblUploadMsg" runat="server"></asp:Label>
<asp:TextBox ID="videoNameTextbox" runat="server"></asp:TextBox>
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /><br /><br />
</div>
<div>
<hr />
<asp:DataList ID="DataList1" Visible="true" runat="server" AutoGenerateColumns="false"
RepeatColumns="2" CellSpacing="5">
<ItemTemplate>
<u>
<%# Eval("vname") %></u>
<hr />
<a class="player" style="height: 300px; width: 300px; display: block" href='<%# Eval("vpath") %>'>
</a>
</ItemTemplate>
</asp:DataList>
<script src="FlowPlayer/flowplayer-3.2.12.min.js" type="text/javascript"></script>
<script type="text/javascript">
flowplayer("a.player", "FlowPlayer/flowplayer-3.2.16.swf", {
plugins: {
pseudo: { url: "FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf" }
},
clip: { provider: 'pseudo', autoPlay: false },
});
</script>
</div>
</form>
</body>
</html>
**Code in aspx.cs**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebsiteTaskAsp
{
public partial class index : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=Khawaja\\SQLEXPRESS;Initial Catalog=TaskDB;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection("Data Source=Khawaja\\SQLEXPRESS;Initial Catalog=TaskDB;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from videos";
cmd.Connection = con;
con.Open();
DataList1.DataSource = cmd.ExecuteReader();
DataList1.DataBind();
con.Close();
}
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if(FileUpload1.HasFile)
{
string str = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//UploadedVideos//"+str);
string path = "~//UploadedVideos//"+str.ToString();
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO videos VALUES('"+videoNameTextbox.Text+"','"+path+"')",con);
cmd.ExecuteNonQuery();
con.Close();
lblUploadMsg.Text = "Video Uploaded Successfully";
}
}
}
}
Getting an error that has crashed my entire site. The wait operation is timing out, i have read the documentation on using{} blocks but cannot understand why this is happening. Any help is appreciated.
[Win32Exception (0x80004005): The wait operation timed out]
The page i have been working on most recently is
itemediting.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="itemediting.aspx.cs" Inherits="admin_itemediting" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>elmtree - Admin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" />
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="../styles/mylist.css" />
</head>
<body>
<form id="form1" runat="server">
<img src="images/ELleft.png" style="width:226px; height:52px; margin-top: 3px; margin-left: 17px; text-align: justify; float: none;"/></a></li>
<div class="container">
<h1> Item Edit </h1> </div>
<div class="container">
<div class="form-group">
<label class="col-sm-2 control-label">Item name: </label>
<div class="col-md-4">
<asp:TextBox ID="itemnametext" runat="server" Text="" CssClass="form-control">
</asp:TextBox>
</div>
<div class="pull-right">
<asp:Button CssClass="btn btn-primary btn-lg" ID="updatebutton" role="button" runat="server" Text="save" OnClick="updatebutton_Click" />
</div>
</div>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class admin_itemediting : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int row = 0;
if (Request.QueryString["itemID"] != null)
{
row = int.Parse(Request.QueryString["itemID"]);
}
else
{
Response.Redirect("itemedit.aspx");
}
}
string connectionString = WebConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string query = "SELECT * FROM reports WHERE ID=#rowid";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("#rowid", row);
SqlDataReader rdr = myCommand.ExecuteReader();
while (rdr.Read())
{
string myname = rdr["itemname"].ToString();
itemnametext.Text = myname;
}
}
protected void updatebutton_Click(object sender, EventArgs e){
string connectionString = WebConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string itemnametextupdate = itemnametext.Text;
string query = "UPDATE reports SET itemname = #itemnewname";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("#itemnewname", itemnametextupdate);
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Redirect("updateimage.aspx");
}
public object row { get; set; }
}
Just a suggestion.
if (!IsPostBack)
{
int row = 0;
if (Request.QueryString["itemID"] != null)
{
row = int.Parse(Request.QueryString["itemID"]);
}
else
{
// here even if you make redirect the code continue to run
// and you do not know whats going on then... a call to db and a redirect.
Response.Redirect("itemedit.aspx");
// so here add a return;
return;
}
}
Next possible bug is that on post back the row is not saved on viewstate and take a default value. Make your row variable as:
int cRow
{
set
{
ViewState["cRow"] = value;
}
get
{
if (ViewState["cRow"] != null)
return Convert.ToInt32(ViewState["cRow"]);
else
return -1;
}
}
When adding the JQuery JS include the InnetHTML is not working:
My HTML code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="aaa.aspx.cs" Inherits="aaa" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css"
/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="ppdiv" runat="server">
</div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
</form>
</body>
</html>
My C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class aaa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ppdiv.InnerHtml = "aaa";
}
}
when include JQUery the InnetHTML is not working. Where is the error and how to fix it.
Thanks in advance
i believe it is because you are using runat="server" in head within which you add the jquery. jquery and javascript for client side, and runat="server" posts the data to server. remove runat="server" from head and check.
I am new to stackoverflow and am needing help.
Last night I was working on an ASP.NET webpage using C# [I'm all very new to it] and after losing a lot of my progress after a flash drive failure, I had to rewrite from a backup I stored on google drive. After putting it onto my computer, I received problems with one of my webpages. At first it did not recognize objects "existing in the current context" but I rewrote the page from hand, because I thought it was some trouble caused by copy-pasting things back. Now I get these errors:
Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends t he correct base class (e.g. Page or UserControl).
'ASP.index_aspx.GetTypeHashCode();: no suitable method found to override
'ASP.index_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override
'ASP.index_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable'
I did not have these problems at all when working with the version that I lost. Here is my code:
This is my "upload.aspx" page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>BSHUpload</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css"/>
</head>
<body>
<!-- Menu -->
<div class="pure-menu pure-menu-open pure-menu-horizontal">
<ul>
<li>Home</li>
<li class="pure-menu-selected">Upload</li>
<li>Requests</li>
</ul>
</div>
<!-- Server-side Upload -->
<form id="form1" runat="server" style="padding-left: 2em">
<div>
<h1>Upload a File</h1>
</div>
<div>
<asp:Label ID="lblStatus1" runat="server" Text="---"></asp:Label>
<asp:FileUpload ID="fdFileDrop1" runat="server" />
<asp:Button ID="btnFileDrop1" runat="server" Text="upload" OnClick="btnFileDrop1_Click" />
</div>
</form>
</body>
</html>
And my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFileDrop1_Click(object sender, EventArgs e)
{
string strSavePath1 = "C:\\UploadBin\\";
if (fdFileDrop1.HasFile)
{
string strFileName = fdFileDrop1.FileName;
strSavePath1 += strFileName;
fdFileDrop1.SaveAs(strSavePath1);
lblStatus1.Text = "Your file was saved as " + strFileName;
}
else
{
lblStatus1.Text = "You did not specify a file to upload";
}
}
}
And because I think the bottom 3 errors are for the "index.aspx" page here is that also:
Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>BSHUpload</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
</head>
<body style="height: 228px">
<!-- Menu -->
<div class="pure-menu pure-menu-open pure-menu-horizontal">
<ul>
<li>Home</li>
<li class="pure-menu-selected">Upload</li>
<li>Requests</li>
</ul>
</div>
<form id="form1" runat="server" style="padding-left: 2em">
</form>
<div>
<button id="button1">Testing</button>
<p id="toggle1">
Wala
</p>
<script>
$( "#button1" ).click(function() {
$( "#toggle1" ).slideToggle( "slow" );
})
</script>
</div>
</body>
</html>
Code Behind for index.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class index: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
I really hope you guys can help because I'm stumped here. I'm pretty sure that both the inherits and the "System.Web.UI.Page" is correct but it says it is not.
The problem is on index.aspx. Check this at the very top of the file
CodeFile="upload.aspx.cs" Inherits="index"
That's wrong. You're pointing to the incorrect code file. It should be
CodeFile="index.aspx.cs" Inherits="index"