65.9K
CodeProject 正在变化。 阅读更多。
Home

使用Code Behind和JavaScript处理多DataGridView中的相关数据

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (3投票s)

2006年11月3日

viewsIcon

25402

downloadIcon

194

一篇关于使用 JavaScript 和代码隐藏在多个 DataGrid 中处理数据持久性的文章。

Sample Image

引言

本文介绍了一种在多个 DataGrid 中在回发之间保持数据持久性的方法。本文还介绍了如何使用 HTML 隐藏字段通过代码隐藏在页面之间传递数据。保存到数据库临时表中的数据会再次填充到 DataGrid 中。

背景

本文主要涉及 DataGrid 以及如何在其 HTML 控件中操作以在页面之间传递数据。

使用代码

function CheckWeight(val,index)
{
    var temp;
    var wt;
    var indx;
    var currVal;
    var catindx="";    
    var a;    
    var com_index;
    com_index=index.substring(2);
    status=com_index;
    catindx=
      document.getElementById("sel_CategoryIndex").value;
    document.getElementById("checked_B").value=
      document.getElementById("checked_B").value + 
      '*' + index;
    var t=document.getElementById("checked_B").value;
    indx=document.getElementById("sel_areaIndex").value
    currVal=eval(document.getElementById(indx).value);
    wt=document.getElementById("sel_areaWeight").value
    temp=parseFloat((val * wt)*-1);
    A = document.getElementById(indx).value
    AA=document.getElementById('C' +catindx).value
    AA=Number(AA)
    //status=AA;
    B = temp
    D=Number(A-B)
    //D=(AA+D)
    if(document.getElementById(index).checked==true)
    {
        document.getElementById(indx).value=(A-B)
        document.getElementById('B'+com_index).disabled=false
        document.getElementById('B'+com_index).focus();
            
        switch(catindx)
        {
            case "1": document.getElementById("C1").value=(D);
                        break;
            case "2": document.getElementById("C2").value=(D);
                        break;
            case "3": document.getElementById("C3").value=(D);
                        break;
            case "4": document.getElementById("C4").value=(D);
                        break;
            case "5": document.getElementById("C5").value=(D);
                        break;
            case "6": document.getElementById("C6").value=(D);
                        break;
            case "7": document.getElementById("C7").value=(D);
                        break;
            case "8": document.getElementById("C8").value=(D);
                        break;
            case "9": document.getElementById("C9").value=(D);
                        break;
            
        }
    }
    else
    {        
        A = Number(A)
        AA = Number(AA)
        B = Number(B)
        C = (A + B)
        document.getElementById(indx).value=C;
        document.getElementById('B'+com_index).value="";
        document.getElementById('B'+com_index).disabled=true;
        
        E=AA-B
        switch(catindx)
        {
            case "1": document.getElementById("C1").value=E;
                break;
            case "2": document.getElementById("C2").value=E;
                a="inside c2";
                break;
            case "3": document.getElementById("C3").value=E;
                break;
            case "4": document.getElementById("C4").value=E;
                break;
            case "5": document.getElementById("C5").value=E;
                break;
            case "6": document.getElementById("C6").value=E;
                break;
            case "7": document.getElementById("C7").value=E;
                break;
            case "8": document.getElementById("C8").value=E;
                break;
            case "9": document.getElementById("C9").value=E;
                break;
                
        }
    }
    document.getElementById("currAreaVal").value=
             document.getElementById(indx).value;
    document.getElementById("txt_totalScore").value=
             document.getElementById(indx).value;
}

这个 JavaScript 函数通过隐藏字段从代码隐藏文件中获取存储的分解值。存储的评论以及针对特定区域选中的复选框存储在表 BreakDown_visited 中。复选框的 ID 存储为带有分隔符 "*" 的字符串,以及各自的评论。然后,基于“区域”DataGrid 的行号,获取数据并提取 ID 和评论到数组中,然后用于设置文本框的值并设置复选框的状态。在代码隐藏中,RegisterClientScriptBlock 函数发出客户端脚本。

Public Function LoadBreakDownValues(ByVal catid As Integer, _
                                    ByVal aid As Integer)
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    Dim c As SqlConnection
    Dim str As String
    Dim str_com As String
    Dim sbarr() As String
    Dim sbarr_com() As String
    Dim tempstr As String
    Dim sb As New StringBuilder
    Dim conStr As String = _
        ConfigurationSettings.AppSettings("conStr")
    Dim con As New SqlConnection(conStr)
    Try
        con.Open()
        With cmd
            .CommandText = "select id,comments from " & _ 
                           "Breakdown_visited where catid=" & _
                           catid & " and AreaID=" & aid
            .Connection = con
            .CommandType = CommandType.Text
        End With
        dr = cmd.ExecuteReader()
        If dr.HasRows Then
            While dr.Read
                    str = dr.GetString(0)
                    str_com = dr.GetString(1)
            End While

            sbarr = str.Split("*")
            sbarr_com = str_com.Split("*")
            sb.Append("")
            If Not IsClientScriptBlockRegistered("BK") Then
                RegisterClientScriptBlock("BK", sb.ToString)
            End If
        End If
        dr.Close()
        con.Close()
    Catch ex As Exception
        Response.Write(ex.Message)
    Finally
    End Try
End Function
© . All rights reserved.