很简单的一个功能,提交按钮保存文本框中内容到数据库,可是我太笨了,看了2月书了,自己一操作还是不会,贴出代码,求指点,谢谢了<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1.aspx.cs" Inherits="_1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="姓名"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server"  Text="提交"  OnCommand="InsertCommand" />
        
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                    SortExpression="id" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            onselecting="SqlDataSource1_Selecting" SelectCommand="SELECT * FROM [1]"
            InsertCommand="INSERT 1(name) VALUE (@name)"
            >
        <InsertParameters>
            <asp:ControlParameter ControlID="TextBox1" Name="name" />
        </InsertParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>请问哪里错了?正确的做法如何写呢?另外想找个师傅,本人28岁了...

解决方案 »

  1.   

    然后呢?你在InsertCommand中写写入数据库的代码不就可以了?
      

  2.   

    你参考这篇文章改一下就好了,这是微软的网站,绝对权威!
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insert.aspx也就是在网页前面加上
    <script runat="server">
    private void InsertShipper (object source, EventArgs e) {
      SqlDataSource1.Insert();
    }
    </script>在按钮里加上onclick="InsertShipper"就应该可以了。
    当然你可以把函数都改成InsertName。
    试一下吧!
      

  3.   

    你用的是数据源控件SqlDataSource,只要在单击按钮事件代码里写一句:SqlDataSource1.Insert();就可以了,其他什么都不要。
      

  4.   

    看看Scott Mitchell 的ASP.NET 2.0数据教程
    ASP.NET 2.0数据教程
      

  5.   

    ASP.net一夜速成 - 网易学院
      

  6.   

    看ASP.NET基础教程。数据源绑定后,直接写sql语句就行了。
      

  7.   

    这个你如果用SQLDATASOURCE好想,操作很麻烦,还是自己写代码来搞定吧,这个sqldatasoruce还是用来提取数据吧。
      

  8.   

    建议你看看视频 http://student.csdn.net/  还有浪曦网 都有一些基础的免费视频,我就是通过这些视频自学的
      

  9.   

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  Debug="true"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
    private void InsertShipper (object source, EventArgs e) {
      SqlDataSource1.Insert();
    }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title></head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <table >
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="标题:"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        
                        <br />
                        <br />
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                            DataKeyNames="id" DataSourceID="SqlDataSource1">
                            <Columns>
                                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                                    ReadOnly="True" SortExpression="id" />
                                <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                            </Columns>
                        </asp:GridView>
                        
                     <asp:sqldatasource
            id="SqlDataSource1"
            runat="server"
            connectionstring="<%$ ConnectionStrings:ConnectionString %>"
            selectcommand="SELECT * FROM [919]"
            insertcommand="insert (title) values (@title)" 
            >
    <InsertParameters>
        <asp:FormParameter Name="title" FormField="TextBox1" />
    </InsertParameters>
          </asp:sqldatasource>
                    </td>
                </tr>
            </table>
        <br />
            <asp:button
               id="Button1"
               runat="server"
               text="保存到数据库"
               onclick="InsertShipper" />    </div>
        </form>
    </body>
    </html>提示:第6行有语法错误行 6:    SqlDataSource1.Insert();