2011-12-08 51 views
1

思想是用戶在數據庫中搜索記錄。當記錄返回時,他們可以點擊記錄以從另一個表中返回更多信息。謝謝如何從gridview中的一行中選擇一個值用作新存儲過程中的變量?

這是我的代碼: 這包含我的c#邏輯。

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> 
+0

如果我的答案是您接受的答案,請您指出。謝謝,GS。 – gsirianni

回答

0

妳使用的AutoGenerateColumns =假,綁定列/模板列和rowcommand事件的更好。

sample here

+0

爲什麼downvotes? – imAbhi

+0

不知道誰給了你一個投票,但我會讓你回到零。 – gsirianni

+0

thnx! 15個字符.. – imAbhi

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) ; 
    } 
相關問題