任意位置选择DataGrid的行

 在DataGrid中要选种某一行必须要设置个选择列,下面介绍的方法可能在某些情况下有用

.aspx代码:

<%@ Page language="c#" Codebehind="WhList.aspx.cs" AutoEventWireup="false" Inherits="Logease.CarryOn.Web.airIn.bussOperation.WhList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
               <title>WhList</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema><base target=_self>
<meta http-equiv=pragma content=no-cache><LINK href="../../Styles/green.css" type=text/css rel=stylesheet >
<script lang=javascript>
               var returnValues; // 回车返回值
               var backColors;   // 目标行的背景色
               var colors;            // 目标行的前景色
               var chooseRow;  // 前次选中行
               // 双击返回
               function coporationinfo(wh_no,warehouse_no)
               {
                       try
                       {
                               var d=dialogArguments;
                               d.document.all("hid_wh_no").value = wh_no;
                               d.document.all("hid_warehouse_no").value = warehouse_no;
                       }
                       catch(e)
                       {
                               alert(" 操作错误,无法返回查询结果!");
                       }
                       window.close();
                       return false;
               }
               // 确认返回
               function doClick(str)
               {
                       var obj_wh_no = document.getElementById("hid_returnwh_no");
                       var obj_warehouse_no = document.getElementById("hid_returnwarehouse_no");
                       if (obj_wh_no.value != "" && obj_warehouse_no.value != "")
                               coporationinfo(obj_wh_no.value,obj_warehouse_no.value);
                       else
                               alert(" 未选择");
                       return false;
               }
               function initRow(){
                       if (chooseRow == null){
                               chooseRow = document.getElementById("DataGrid1").rows[1];
                               if (chooseRow.cells[1] != null && chooseRow.cells[2] != null){
                                      document.getElementById("hid_returnwh_no").value = chooseRow.cells[1].innerText;
                                      document.getElementById("hid_returnwarehouse_no").value = chooseRow.cells[2].innerText;
                               }
                               else
                                      document.getElementById("btn_cancel").focus();
                       }
               }
               // 回车事件
               function enterControl(){
                       if (event.keyCode == 13){
                               if(event.srcElement.name != "btn_cancel")
                                      document.getElementById("btn_Submit").click();
                               else
                                      document.getElementById("btn_cancel").click();
                       }
                       if(event.keyCode==38){ // 方向键向上
                       }
                       if(event.keyCode==40){ // 方向键向下
                       }
                       if(event.keyCode==37){ // 方向键向左
                               document.getElementById("btn_Submit").focus();
                       }
                       if(event.keyCode==39){ // 方向键向右
                               document.getElementById("btn_cancel").focus();
                       }
               }
               // 设置原有色
               function setColor(backColor,color)
               {
                       backColors = backColor;
                       colors = color;
               }
               function setClickColor(obj,wh_no,warehouse_no){
                       if (chooseRow != null){
                               var index = chooseRow.rowIndex;
                               var rows = chooseRow.parentElement.rows;
                               var color1 = rows[index - 1].style.backgroundColor;
                               var color2 = rows[index + 1].style.backgroundColor;
                               var row = rows[index];
                               if (color1 == "" || color1 == "#ffffff" || color2 == "" || color2 == "#ffffff"){
                                      row.style.backgroundColor = "#f5faf5";
                                      row.color="blank";
                               }
                               if (color1 == "#f5faf5" || color2 == "#f5faf5"){
                                      row.style.backgroundColor = "#ffffff";
                                      row.color="blank";
                               }
                       }
                       chooseRow = obj;
                       document.getElementById("hid_returnwh_no").value = wh_no;
                       document.getElementById("hid_returnwarehouse_no").value = warehouse_no;
               }
               function doCancel()
               {
                       window.close();
                       return false;
               }
               </script>
</HEAD>
<body οnkeydοwn=enterControl() οnlοad=initRow()
MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
<table width=800 align=center>
 <tr>
    <td><asp:datagrid id=DataGrid1 runat="server" AutoGenerateColumns="False" AllowPaging="True" Width="784px" CssClass="DataGrid">
        <SelectedItemStyle CssClass="SelectItemStyle" BackColor="#15B77E"></SelectedItemStyle>
        <EditItemStyle CssClass="EditItemStyle"></EditItemStyle>
        <AlternatingItemStyle CssClass="AlterItemStyle"></AlternatingItemStyle>
        <ItemStyle CssClass="ItemStyle"></ItemStyle>
        <HeaderStyle CssClass="HeaderStyle"></HeaderStyle>
        <Columns>
               <asp:BoundColumn DataField="rownum" HeaderText=" 序号"></asp:BoundColumn>
               <asp:BoundColumn DataField="wh_no" HeaderText=" 单证编号"></asp:BoundColumn>
               <asp:BoundColumn DataField="warehouse_no" HeaderText=" 本仓编号"></asp:BoundColumn>
               <asp:BoundColumn DataField="operation_id" HeaderText=" 平台编号"></asp:BoundColumn>
               <asp:BoundColumn DataField="customer_to_name" HeaderText=" 客户"></asp:BoundColumn>
               <asp:BoundColumn DataField="MBL" HeaderText=" 总单"></asp:BoundColumn>
               <asp:BoundColumn DataField="hbl" HeaderText=" 分单"></asp:BoundColumn>
               <asp:BoundColumn DataField="pieces" HeaderText=" 件数"></asp:BoundColumn>
               <asp:BoundColumn DataField="weight" HeaderText=" 重量"></asp:BoundColumn>
        </Columns>
        <PagerStyle NextPageText=" 下一页" PrevPageText="上一页" CssClass="PagerStyle" Mode="NumericPages"></PagerStyle>
        </asp:datagrid></TD></TR>
 <tr>
    <td align=right><INPUT id=hid_returnwh_no type=hidden
       runat="server"><INPUT id=hid_returnwarehouse_no
      type=hidden runat="server"> <asp:button id=btn_Submit runat="server" Text=" 确认"></asp:Button>&nbsp;&nbsp;
<asp:button id=btn_cancel Text=" 取消" Runat="server"></asp:button>&nbsp;&nbsp;
    </TD></TR></TABLE></FORM>
        </body>
</HTML>

.cs代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
namespace Logease.CarryOn.Web.airIn.bussOperation
{
        /// <summary>
        /// WhList 的摘要说明。
        /// </summary>
        public class WhList : System.Web.UI.Page
        {
               protected System.Web.UI.WebControls.DataGrid DataGrid1;
               protected System.Web.UI.HtmlControls.HtmlInputHidden hid_returnwh_no;
               protected System.Web.UI.HtmlControls.HtmlInputHidden hid_returnwarehouse_no;
               protected System.Web.UI.WebControls.Button btn_Submit;
               protected System.Web.UI.WebControls.Button btn_cancel;
       
               private void Page_Load(object sender, System.EventArgs e)
               {
                       // 在此处放置用户代码以初始化页面
                       if(!IsPostBack)
                       {
                               this.btn_Submit.Attributes.Add("onclick","return doClick();");
                               this.btn_cancel.Attributes.Add("onclick","return doCancel();");
                               this.initBindData();
                       }
               }
 
               #region Web 窗体设计器生成的代码
               override protected void OnInit(EventArgs e)
               {
                       //
                       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                       //
                       InitializeComponent();
                       base.OnInit(e);
               }
              
               /// <summary>
               /// 设计器支持所需的方法 - 不要使用代码编辑器修改
               /// 此方法的内容。
               /// </summary>
               private void InitializeComponent()
               {   
                       this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
                       this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
                       this.btn_Submit.Click += new System.EventHandler(this.btn_Submit_Click);
                       this.btn_cancel.Click += new System.EventHandler(this.btn_cancel_Click);
                       this.Load += new System.EventHandler(this.Page_Load);
 
               }
               #endregion
 
               #region   绑定表格数据
               void initBindData()
               {
                       string filt = "1=1";
                       if (Request.QueryString["filt"] != null && Request.QueryString["filt"].ToString() !="" )
                       {
                               //filt = System.Web.HttpUtility.UrlDecode(Request.QueryString["filt"].Trim());
                               filt = Request.QueryString["filt"].Trim();
                       }
                       else
                       {
                               filt = Session["ComplexSearchFilt"].ToString().Trim();
                       }
                       Logease.CarryOn.Business.AIManager _airiManager = new Logease.CarryOn.Business.AIManager();
                       DataTable dt = _airiManager.GetAIRI_WH_ALL(filt);
                       DataColumn DataCol = new DataColumn();
                       DataCol.ColumnName ="CHECKED";
                       DataCol.DefaultValue = "false";
                       DataCol.DataType = System.Type.GetType("System.String");
                       dt.Columns.Add(DataCol);
                       ViewState["ASNSource"] = dt;
                       this.DataGrid1.DataSource = dt;
                       this.DataGrid1.DataBind();
                       this.DataGrid1.SelectedIndex = 0;
               }
               #endregion
 
               private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
               {
                       string ser_no = e.Item.Cells[0].Text;
                       try
                       {
                               if (ser_no != null && ser_no != " 序号")
                               {
                                      e.Item.Attributes.Add("Ondblclick", "javascript:coporationinfo('" + e.Item.Cells[1].Text + "','" + e.Item.Cells[2].Text + "')");
                                      e.Item.Attributes.Add("Onclick", "setClickColor(this,'" + e.Item.Cells[1].Text + "','" + e.Item.Cells[2].Text + "');this.style.backgroundColor='#15B77E';this.style.color='white';this.style.cursor='hand';setColor(this.style.backgroundColor,this.style.color);");
                                      e.Item.Attributes.Add("OnMouseOver", "setColor(this.style.backgroundColor,this.style.color);this.style.backgroundColor='#15B77E';this.style.color='white';this.style.cursor='hand';") ;
                                      e.Item.Attributes.Add("OnMouseOut", "this.style.backgroundColor=backColors;this.style.color=colors") ;
                               }
                       }
                       catch
                       {}
               }
 
               #region 确认事件
               private void btn_Submit_Click(object sender, System.EventArgs e)
               {
              
               }
               #endregion
 
               #region 取消事件
               private void btn_cancel_Click(object sender, System.EventArgs e)
               {
              
               }
               #endregion
 
               private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
               {
                       this.DataGrid1.DataSource = (DataTable)ViewState["ASNSource"];
                       this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
                       this.DataGrid1.DataBind();
               }
        }

 

这个方法有一定的局限性,首先你在后台没办法直接通过DataGrid获取行了,其次如果DataGrid一多..........后面就不用说,不知道谁有更好的方法或想法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值