能用的注册MOSS页面事件且联动两个GRIDVIEW

HTML如下:

<%@ Page Language="C#" masterpagefile="~masterurl/default.master" title="无标题 1" inherits="DLLCS.WebForm1, DLLCS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a34b431d3cbdcd48" meta:progid="SharePoint.WebPartPage.Document" %><asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">
   <script  language="javascript" type="text/javascript" >
         var prevselitem=null;
       function selectx(row)
      {
              if(prevselitem!=null)
             {
                  prevselitem.style.backgroundColor='#ffffff';
               }
              row.style.backgroundColor='PeachPuff';
              prevselitem=row;
             
      }
      function setvalue(obj)
      {
      document.getElementById("ctl00_PlaceHolderMain_Hidden1liutao").value=obj;// ctl00_PlaceHolderMain_Hidden1liutao 在编译后的页面中的名字
 
       document.all('ctl00_PlaceHolderMain_Button1').click();;// 'ctl00_PlaceHolderMain_Button1'在编译后的页面中的名字
 

 
      }
   </script>
<asp:HiddenField ID="Hidden1liutao" runat="server"/>
<table style="100%">
<tr><td  style="background-color: ghostwhite; height:35px; font-weight: bold; font-size: 20px; color: #3300ff; font-family: 微软雅黑;">公司项目 </td></tr>
<tr><td>
<asp:GridView runat="server" id="GridView1"      Width="100%" AutoGenerateColumns="False" DataKeyNames="ID">
 <RowStyle Height="25px" />
 <Columns>
  <asp:boundfield DataField="Title" HeaderText="项目名称" ItemStyle-Width="40%">
  </asp:boundfield>
  <asp:boundfield DataField="principal" HeaderText="项目负责人" ItemStyle-Width="30%">
  </asp:boundfield>
  <asp:boundfield DataField="money" HeaderText="合同金额" ItemStyle-Width="30%">

  </asp:boundfield>
 </Columns>
 <HeaderStyle Height="30px" />

</asp:GridView>

</td></tr>
<tr><td  style="background-color: ghostwhite; height:35px; font-weight: bold; font-size: 20px; color: #3300ff; font-family: 微软雅黑;">项目合同</td></tr>
<tr><td>
<asp:GridView runat="server" id="GridView2"     Width="100%" AutoGenerateColumns="False">
 <RowStyle Height="25px" />
 <Columns>
  <asp:boundfield DataField="Title" HeaderText="合同名称" ItemStyle-Width="40%">
  </asp:boundfield>
  <asp:boundfield DataField="htintro" HeaderText="合同简介" ItemStyle-Width="30%">
  </asp:boundfield>
  <asp:boundfield DataField="hmoney" HeaderText="合同金额" ItemStyle-Width="30%">
  </asp:boundfield>
 </Columns>
 <HeaderStyle Height="30px" />
 
</asp:GridView>
   </td></tr>
   <tr><td>
    <asp:Button runat="server" Text="Button" id="Button1" Height="0px" Width="0px"></asp:Button>
    </td></tr>
   </table>
   
  
</asp:Content>


 

代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
namespace DLLCS
{
    public partial class WebForm1 : PublishingLayoutPage
    {
        protected GridView GridView1;

        protected Button Button1;
        protected GridView GridView2;
        protected HiddenField Hidden1liutao;
        protected override void OnLoad(EventArgs e)
        {
           
            GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
            Button1.Click += new EventHandler(Button1_Click);
            base.OnLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {

                //Response.Write("ddd");PublishingLayoutPage
                NewMethod();
                //string key = this.Hidden1liutao.Value.ToString();

                //NewMethodA(key);
            }
        }

        private void NewMethod()
        {
            SPSite site = new SPSite("http://mosslt");
            SPList list = site.RootWeb.Lists["公司项目"];
            GridView1.DataSource = list.Items.GetDataTable();
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string key = this.GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#Efefef';this.style.cursor='hand';");//当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");//当鼠标移开时还原背景色
               // e.Row.Attributes.Add("onclick", "document.getElementById('ctl00_PlaceHolderMain_Hidden1liutao').value=" + key + ";");
                e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this);setvalue('" + key + " ')");

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Response.Write(DateTime.Now.ToString());
            NewMethod();
            string key = this.Hidden1liutao.Value.ToString();

            NewMethodA(key);
        }

        private void NewMethodA(string key)
        {
            SPSite site = new SPSite("http://mosslt");
            SPList list = site.RootWeb.Lists["项目合同"];
            SPQuery query = new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='Pid'/><Value Type='Text'>" + key + "</Value></Eq></Where>";

            GridView2.DataSource = list.GetItems(query).GetDataTable();
            GridView2.DataBind();
        }


    }
}

原文地址:https://www.cnblogs.com/IsNull/p/1718266.html