如何从gridview中的行中选择一个值以用作新存储过程中的变量?

时间:2011-12-08 22:24:03

标签: c# asp.net sql-server-2005

我们的想法是用户在数据库中搜索记录。当记录返回时,他们可以单击记录以从anther表返回更多信息。感谢

这是我的代码: 这包含我的c#logic。

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;
using System.Globalization;

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

    public DataSet GetDataSource()
    {
        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.Parameters.AddWithValue("@fName", fNameTextbox.Text);
                //cmd.Parameters.AddWithValue("@State", State);
                //cmd.Parameters.AddWithValue("@Zip", Zip);
                cmd.Connection = con;

                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds);
                return ds;
            }
        }
    }
    public DataSet GetSpecificCustomerData(string cusId)
    {
        DataSet ds = new DataSet();

        using (SqlConnection con = new SqlConnection("SERVER=serverName;Trusted_Connection=Yes;DATABASE=dbName"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "getCustomerRecords";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@cusId", cusId);
                cmd.Connection = con;

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

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

        ds = GetDataSource();
        GridView1.PageIndex = 0;
        if (ds.Tables != null)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }                
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    {
      GridView1.PageIndex = e.NewPageIndex;
      GridView1.DataSource = GetDataSource();
      GridView1.DataBind(); 
    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        DataSet ds;
        string customerID =;

        customerID = GridView1.SelectedRow.Cells[0].Text;
        ds = GetSpecificCustomerData(customerID);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }        
}

这是我的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 style="text-align: left">
    <form id="form1" runat="server">
        <span style="font-size: 24pt; color: #0000cc; font-family: Euphemia"><strong>&nbsp;
            MSS Archiver&nbsp;</strong></span><table style="width: 1056px; height: 106px">
                <tr>
                    <td style="width: 159px; height: 72px">
        <asp:Label ID="Label1" Text="Last Name"  runat="server" />
        <asp:TextBox ID="lNameTextbox" runat="server"></asp:TextBox></td>
                    <td style="width: 176px; height: 72px">
        <asp:Label ID="Label2" Text="First Name"  runat="server" />
        <asp:TextBox ID="fNameTextbox" runat="server" ></asp:TextBox></td>
                    <td style="width: 157px; height: 72px">
        <asp:Label ID="Label5" Text="State" runat="server" />
        <asp:TextBox ID="StateTextbox" runat="server" ReadOnly="True">MA</asp:TextBox></td>
                    <td style="width: 178px; height: 72px">
        <asp:Label ID="Label6" Text="Zip"  runat="server" /><asp:TextBox ID="Zip" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 159px">
                    </td>
                    <td style="width: 176px">
                    </td>
                    <td style="width: 157px">
                    </td>
                    <td style="width: 178px; text-align: center">
        <asp:Button ID="Button1" runat="server" PostBackUrl="~/Default.aspx" Text="Search" OnClick="Button1_Click"  /></td>
                </tr>
            </table>
        <table style="width: 1086px">
            <tr>
                <td>
                </td>
                <td style="text-align: center">
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateSelectButton="True" OnPageIndexChanging="GridView1_PageIndexChanging" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />                
        </asp:GridView>
                </td>
                <td>
                </td>
            </tr>
        </table>        
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

将gridview的DataKeys属性设置为数据集中的主键字段

DataKeys="<YourPrimaryKeyField>"

将网格视图的OnSelectedIndexChanged属性设置为gridView_SelectedIndexChanged

在代码中添加一个方法来处理事件。

 protected void gridView_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
    {

        string key = gridView.DataKeys[e.NewSelectedIndex].Value.ToString();

        //Call your GetData function and pass in this value as a parameter
         GetSpecificCustomerData(key) ;
    }

答案 1 :(得分:0)

U最好使用AutoGenerateColumns = false,BoundFields / Templatefields和rowcommand事件。

sample here