1
using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;


我最终想要做的是更新一个 sql 数据库..
我被困在这一步.. 我的网页可以将 sqldatabase 内容打印到一个 div 中.. 现在,我正在尝试放一些内容到文本框中。但是每当我调试时,它都会向我抛出这个错误。我得到的错误
卡在这部分,请对我的情况有所了解。
另外..我这样做对吗?有没有更有效的方法来做到这一点?好的教程/演练的意见和链接也将不胜感激!提前致谢。

我的html

<input runat="server" class="hexen" id="investigate1"/><br />
<input type="text" class="hexen" id="investigate2"/><br />
<input type="text" class="hexen" id="investigate3"/><br />
<input type="text" class="hexen" id="investigate4"/><br />
<input type="text" class="hexen" id="investigate5"/><br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


我的C#

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace WebApplication1
{
    public partial class Default1 : System.Web.UI.Page
    {
        protected void SimpleRead(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=AZUES-336\\JDOESQLSERVER;Initial Catalog=Northwind;Integrated Security=SSPI");
            SqlDataReader rdr = null;

            try
            {

                conn.Open();


                SqlCommand cmd = new SqlCommand("select * from Customers", conn);

                rdr = cmd.ExecuteReader();


                if (rdr.Read())
                {
                    investigate1.Text = rdr.GetValue(0).ToString;//Presumably where the error is happening
                }
            }

            finally
            {
                if (rdr != null)
                { rdr.Close(); }

                if (conn != null)
                { conn.Close(); }
            }
        }
    }
}







@Seany84 默认
.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default1" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<script type="text/javascript">
    $(document).ready(function () {

        $('.hexen').after('<span class="ui-state-default ui-corner-all ui-icon-disk ui-icon saveButton" title="Save" style="float:left"></span>')// ui icon

    .keypress(function () {$(this).next('.saveButton').show();}); //adds ui icon

     $('.ui-state-default').hover(
     function () {$(this).addClass('ui-state-hover');},
     function () {$(this).removeClass('ui-state-hover');}
     ); //ui icon hover

        $('.saveButton').click(function () {
            var id = $(this).prev().attr('id'); //used in the "data" attribute of the ajax call
            var value = $(this).prev().val(); //used in the "data" attribute of the ajax call

            $.ajax({
                type: "POST",
                url: "Default.aspx",
                data: "{Id: " + id + ", Value: " + value + "}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    console.log(data);
                }
            });
            $(this).hide();
        }); //runs ajax call and removes ui-icon after .saveButton is clicked

    }); //end .ready
</script> 

<input runat="server" class="hexen" id="investigate1"/><br />
<input type="text" class="hexen" id="investigate2"/><br />
<input type="text" class="hexen" id="investigate3"/><br />
<input type="text" class="hexen" id="investigate4"/><br />
<input type="text" class="hexen" id="investigate5"/><br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>
4

2 回答 2

2

您需要使用 ASP 服务器控件而不是标准的 HTML 输入。

代替,

<input runat="server" class="hexen" id="investigate1"/>

<asp:TextBox ID="investigate1" runat="server" CssClass="hexen" />

然后尝试一下。

此外,应该行:

investigate1.Text = rdr.GetValue(0).ToString;

改为:

investigate1.Text = rdr.GetValue(0).ToString();

http://www.asp.net有很好的 ASP.net 网页表单、MVC 和网页教程。

于 2012-03-29T22:47:00.160 回答
0

这是您必须使用的属性:

HtmlInputControl.Value 属性

具有 runat="server" 属性的 Html Input 被转换为 HtmlInputControl。而那些没有 Text 属性,而是 Value 属性。因此,将文本更改为值。

于 2012-03-29T22:50:23.667 回答