将文本框数据传递给存储过程并将其返回到gridview

时间:2011-12-07 22:24:07

标签: c# asp.net sql

想知道我在哪里错了。我是页面加载,但是当我点击搜索时(在我在姓氏字段中输入内容后),它只是时钟和时钟。

这是我的aspx.cs页面:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

  public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();

        using (SqlConnection con = new SqlConnection("SERVER=ServerName;Trusted_Connection=Yes;DATABASE=DBNAME"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "customerSearchStoredProc";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@lName", lNameTextbox.Text);
                cmd.Connection = con;

                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds);

            }
            con.Close();
        }
        GridView1.DataSource = ds;
        GridView1.DataBind();

        /*SqlDataAdapter adp = new SqlDataAdapter("customerSearchStoredProc", con);
        adp.SelectCommand.CommandType = CommandType.StoredProcedure;
        adp.SelectCommand.Parameters.Add(new SqlParameter("@lName", System.Data.SqlDbType.Text));
        //adp.SelectCommand.Parameters.Add(new SqlParameter("@State", System.Data.SqlDbType.Text));
        adp.SelectCommand.Parameters["@lName"].Value = lNameTextbox.Text;
        //adp.SelectCommand.Parameters["@State"].Value = StateTextbox.Text;

        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();*/


    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

这是我的aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %> <%@ OutputCache Duration="1" VaryByParam="none" %>

<!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>MSS Archive Page</title> </head> <body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" Text="Last Name"  runat="server" />
        <asp:TextBox ID="lNameTextbox" runat="server"></asp:TextBox>
        <asp:Label ID="Label2" Text="First Name"  runat="server" />
        <asp:TextBox ID="fName" runat="server" ></asp:TextBox>
       <!-- <asp:Label ID="Label3" Text="Street"  runat="server" />
        <asp:TextBox ID="Street" runat="server" AutoPostBack="True"></asp:TextBox><br />
        <asp:Label ID="Label4" Text="City"  runat="server" />
        <asp:TextBox ID="City" runat="server" AutoPostBack="True" ></asp:TextBox> -->
        <asp:Label ID="Label5" Text="State" runat="server" />&nbsp;<!-- <asp:TextBox ID="State" runat="server" AutoPostBack="True"></asp:TextBox> -->
        <asp:TextBox ID="StateTextbox" runat="server" ReadOnly="True">MA</asp:TextBox>
        &nbsp;&nbsp;
        <asp:Label ID="Label6" Text="Zip"  runat="server" />
        <asp:TextBox ID="Zip" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" PostBackUrl="~/Default.aspx" Text="Search" OnClick="Button1_Click"  /><br />
        OR<br />
        <asp:Label ID="Label7" Text="Policy"  runat="server" />
        <asp:TextBox ID="Policy" runat="server"></asp:TextBox>
        <asp:Label ID="Label8" Text="Account"  runat="server" />
        <asp:TextBox ID="Account" runat="server"></asp:TextBox>&nbsp;<br />
        &nbsp;<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" OnDataBinding="Button1_Click" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >

        </asp:GridView>
        &nbsp;&nbsp;<br />
        &nbsp; &nbsp;
        &nbsp;&nbsp;

    </form> </body> </html>

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您的数据绑定正在调用点击。

OnDataBinding="Button1_Click"

在点击时,您调用数据绑定。

GridView1.DataBind();   

对我来说,这看起来像是一个永远存在的循环。

答案 1 :(得分:2)

我看到GridView没有显式定义列,并且你有AutoGenerateColumns = false。

这是代码中的遗漏,还是你的代码真的那样?如果是这样,那么GridView将不会显示任何内容。尝试设置AutoGenerateColuns =“true”,看看是否有任何显示。如果是这样,那么问题是您需要明确定义列。