简体   繁体   English

更新ASP.NET文本框中的文本

[英]Update Text in ASP.NET text box

I'm trying to update text in an SQL database using a text box. 我正在尝试使用文本框更新SQL数据库中的文本。 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. itemnametext在当前上下文中不存在。

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 : 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";

    }













    }

}

Just change ID of your texbox from itemnameedit to itemnametext 只需将texbox的ID从itemnameedit更改为itemnametext
Update: change your reference here to match the codefile and codefile class . 更新:在此处更改您的引用以匹配codefile和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. 我认为您没有正确的参考,因此代码文件 admin_itemediting不知道aspx文件中的任何控件。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="itemediting.aspx.cs" Inherits="admin_itemediting" %>

Your textbox name is itemnameedit so itemnametext is not found 您的文本框名称为itemnameedit,因此找不到itemnametext

 <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 编辑 :您的aspx页面是默认页面,您正在发布itemediting.aspx.cs的代码。发布关于itemediting.aspx的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Default" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM