none
通一个界面怎么把参数传过去呢? RRS feed

  • 问题

  •     数据库中有两个表,分别是ContractTable(合同表)和RentTable(租金表),ContractTable中有一个ContractNumber(合同号)字段,RentTable中有ContractNumber(合同号)、riqi(日期)、zujin(租金)三个字段(RentTable中的ContractNumber和ContractTable中的ContractNumber相对应)。

    -

        前台代码中我放了GridView1、GridView2和一个SqlDatasource1,GridView1对应ContractTable(Select ContractNumber from ContractTable)。GridView2我希望是选择了GridView1中的ContractNumber,就能在GridView2上显示出RentTable中所有相应ContractNumber的数据。

     

    -

       这里附上了前台代码代码和后台代码,我考虑:1、前台给GridView1的“启用选定内容”打上勾,

      2、然后后台代码应该使用 SelectedIndexChanged  事件 ,每当点击某一个ContractNumber前的“选择”时就会得到GridView1中ContractNumber的值,

      3、然后把这个值ToString,赋予给GridView2的数据源。

     

    -

    问题是我是菜鸟,还没接触过这种难度的例子,写不出来!求各位帮忙看下!!谢谢!!

     

    ============前台=================

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="RentModify3.aspx.cs" Inherits="RentTable_RentModify3" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
      <table cellpadding="0" cellspacing="0" class="style1">
        <tr>
          <td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
              DataKeyNames="ContractNumber" DataSourceID="SqlDataSource1" 
              onselectedindexchanged="GridView1_SelectedIndexChanged">
              <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="ContractNumber" HeaderText="ContractNumber" 
                  ReadOnly="True" SortExpression="ContractNumber" />
              </Columns>
            </asp:GridView>
          </td>
        </tr>
        <tr>
          <td>
            <asp:GridView ID="GridView2" runat="server">
            </asp:GridView>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
          </td>
        </tr>
        <tr>
          <td>
            &nbsp;</td>
        </tr>
        <tr>
          <td>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
              ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" 
              SelectCommand="SELECT [ContractNumber] FROM [ContractTable]">
            </asp:SqlDataSource>
          </td>
        </tr>
        <tr>
          <td>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource>
          </td>
        </tr>
      </table>
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    </asp:Content>
    

    ==

    =====================后台=================

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class RentTable_RentModify3 : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
    
      }
      protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
        string s1 = GridView1.SelectedIndex.ToString("ContractNumber");
        Label1.Text = s1;
        //这里要把GridView1的ContactNumber传给GridView2,然后再利用
        //“Select * from RentTable where ContractNumber = 'GridView传过来的参数'”
        //之后还要绑定GridView2的数据源等工作,很茫然!求解!
      }
    }
    



    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
    2011年8月23日 4:31

答案

  • 你好!

    参考我下面的示例

    1 设置 GridView 的 DataKeyNames 的值为你的主键

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="OID"

    2 通过 DataKeys 获取到选择行当前主键的值

    var value = (int)this.GridView1.DataKeys[this.GridView1.SelectedIndex].Value;

    完整示例如下:

    <%@ Page Language="C#" AutoEventWireup="true"%>
    
    <script runat="server">
      
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
          this.BindMaster();
      }
    
      void BindMaster()
      {
        this.GridView1.DataSource = this.CreateMaster();
        this.GridView1.DataBind();
      }
    
      void BindDetial(int masterId)
      {
        var ds = this.CreateDetail().Where(item => item.PID == masterId);
        
        this.GridView2.DataSource = ds;
        this.GridView2.DataBind();
      }
    
      public List<MasterInfo> CreateMaster()
      {
        var ds = new List<MasterInfo>();
    
        ds.Add(new MasterInfo() { OID = 1, Name = "Group1" });
        ds.Add(new MasterInfo() { OID = 2, Name = "Group2" });
    
        return ds;
      }
    
      public List<DetailInfo> CreateDetail()
      {
        var ds = new List<DetailInfo>();
    
        ds.Add(new DetailInfo() { OID = 1, PID = 1, Name = "Item1" });
        ds.Add(new DetailInfo() { OID = 2, PID = 1, Name = "Item2" });
        ds.Add(new DetailInfo() { OID = 3, PID = 2, Name = "Item3" });
        ds.Add(new DetailInfo() { OID = 4, PID = 2, Name = "Item4" });
    
        return ds;
      }
    
      public class MasterInfo
      {
        public int OID { get; set; }
        public string Name { get; set; }
      }
    
      public class DetailInfo
      {
        public int OID { get; set; }
        public int PID { get; set; }
        public string Name { get; set; }
      }
    
      protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
        var value = (int)this.GridView1.DataKeys[this.GridView1.SelectedIndex].Value;
        this.BindDetial(value);
      }
    </script>
    <!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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="OID"
          onselectedindexchanged="GridView1_SelectedIndexChanged">
          <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="OID" HeaderText="OID" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
          </Columns>
        </asp:GridView>
        <asp:GridView ID="GridView2" AutoGenerateColumns="False" runat="server">
          <Columns>
            <asp:BoundField DataField="OID" HeaderText="OID" />
            <asp:BoundField DataField="PID" HeaderText="PID" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
          </Columns>
        </asp:GridView>
      </div>
      </form>
    </body>
    </html>
    
    


    知识改变命运,奋斗成就人生!
    2011年8月23日 5:44
    版主