[原创]用户控件中使用Js,如何在客户端引用用户控件中某个控件的ID


以下是我写的一个DropDownList可以帮定多个列的一个用户控件

<TABLE id="Table6" cellSpacing="0" cellPadding="0" border="0">
    
<TBODY>
        
<TR>
            
<TD style="DISPLAY: none" width="1"><asp:textbox id="tbValue" runat="server" Width="10px">0</asp:textbox></TD>
            
<TD id="TD1" width="400" runat="server"><asp:textbox id="tbDes" runat="server" Width="100%"  ReadOnly="True"
                    CssClass
="noRightBorder"></asp:textbox></TD>
            
<TD>
                
<DIV style="BORDER-RIGHT: thin inset; BORDER-TOP: thin inset; WIDTH: 18px; POSITION: relative; HEIGHT: 18px"
                    ms_positioning
="GridLayout">
                    
<DIV onmouseup="this.className='link_arrow0'" class='link_arrow0'  onselectstart="return false" onmousedown="this.className='link_arrow1'" style="POSITION: relative" onclick="show('<%=DG.ClientID%>','<%=DataGrid1.ClientID%>','<%=tbValue.ClientID%>');">6</DIV>
                
</DIV>
            
</TD>
        
</TR>
        
<TR>
            
<TD style="DISPLAY: none" width="1"></TD>
            
<TD id="TD2" width="400" runat="server">
                
<DIV id="DG" onblur="hide();" style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; DISPLAY: none; Z-INDEX: 200; OVERFLOW: auto; WIDTH: 100%; BORDER-BOTTOM: gray 1px solid; POSITION: absolute; HEIGHT: 175px"
                    onmouseout
="lostFocus();" runat="server" ms_positioning="GridLayout"><iframe 
      
style="FILTER: progid: DXImageTransform.Microsoft.Alpha(style=0,opacity=100); VISIBILITY: inherit" 
      frameBorder
=0 width="<%=DataGrid1.width%>" height="<%=DataGrid1.height%>" 
      
> </iframe>
                    
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 0px" runat="server"
                        Width
="750px"  Height="200px" ShowHeader="False" BorderWidth="1px"
                        BorderColor
="Gainsboro"></asp:datagrid></DIV>
            
</TD>
            
<td></td>
        
</TR>
    
</TBODY>
</TABLE>
<script language="javascript">initWidth('<%=DataGrid1.ClientID%>','<%=tbDes.ClientID%>','<%=tbValue.ClientID%>');</script>


Imports System.Data.SqlClient
Public Class DrpDataGrid
    
Inherits System.Web.UI.UserControl

Web Form Designer Generated Code
    
Private tbWidth As Integer
    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
Dim Str As String
        
If Not Page.IsStartupScriptRegistered("Key1"Then
            
Str = "<script language =javascript >" + vbCrLf
            
Str += "var gbID;" + vbCrLf
            
Str += "var inLayer=false;" + vbCrLf
            
Str += "function show(DGID,GridID,VID) {" + vbCrLf
            
Str += "var tab=document.all(GridID);" + vbCrLf
            
Str += "var val=document.all(VID).value;" + vbCrLf
            
Str += "document.all(DGID).style.display='block';" + vbCrLf
            
Str += "document.all(DGID).focus();" + vbCrLf
            
Str += "gbID=DGID;" + vbCrLf
            
Str += "if (tab!=null){" + vbCrLf
            
Str += "for(var i=0;i<tab.rows.length;i++) {" + vbCrLf
            
'Str += "var k=i/2+'';" + vbCrLf
            'Str += "if (k.indexOf('.')!=-1)" + vbCrLf
            'Str += "tab.rows(i).style.backgroundColor='OldLace';" + vbCrLf
            'Str += "else" + vbCrLf
            'Str += "tab.rows(i).style.backgroundColor='';" + vbCrLf
            Str += "if (tab.rows(i).cells(0).innerText==val) {" + vbCrLf
            
Str += "tab.rows(i).style.backgroundColor='#0a246a';" + vbCrLf
            
Str += "tab.rows(i).style.color='white';" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "else {" + vbCrLf
            
Str += "tab.rows(i).style.backgroundColor='white';" + vbCrLf
            
Str += "tab.rows(i).style.color='black';" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "function hide() {" + vbCrLf
            
Str += "if (inLayer==false) {" + vbCrLf
            
Str += "document.all(gbID).style.display='none';}" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "function lostFocus() {" + vbCrLf
            
Str += "inLayer=false;}" + vbCrLf
            
Str += "function getfocus() {" + vbCrLf
            
Str += "inLayer=true;}" + vbCrLf
            
Str += "function setVal(DGID,DID,VID,des,val) {" + vbCrLf
            
Str += "if (des!='&nbsp;') {" + vbCrLf
            
Str += "document.all(VID).value=val;" + vbCrLf
            
Str += "document.all(DID).value=des;" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "else {" + vbCrLf
            
Str += "document.all(VID).value=0;" + vbCrLf
            
Str += "document.all(DID).value='';" + vbCrLf
            
Str += "}" + vbCrLf
            
'Str += "document.all(DID).style.backgroundColor='#0a246a';" + vbCrLf
            'Str += "document.all(DID).style.color='white';" + vbCrLf
            Str += "document.all(DGID).style.display='none';" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "function initWidth(GridID,DID,VID) {" + vbCrLf
            
Str += "var tab=document.all(GridID);" + vbCrLf
            
Str += "var val=document.all(VID).value;" + vbCrLf
            
Str += "if (tab!=null)" + vbCrLf
            
Str += "{" + vbCrLf
            
Str += "for(var i=0;i<tab.rows.length;i++) {" + vbCrLf
            
Str += "tab.rows(i).cells(0).style.display='none';" + vbCrLf
            
Str += "if (tab.rows(i).cells(0).innerText==val)" + vbCrLf
            
Str += "document.all(DID).value=tab.rows(i).cells(1).innerText;" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "}" + vbCrLf
            
Str += "</script>" + vbCrLf
            Page.RegisterClientScriptBlock(
"Key1"Str)
        
End If
        tbDes.Attributes.Add(
"onclick""show('" + DG.ClientID + "','" + DataGrid1.ClientID + "','" + tbValue.ClientID + "')")
        tbDes.Attributes.Add(
"onmouseover""this.style.cursor='Default';")

    
End Sub

    
Public Sub BindDataGrid(ByVal dt As DataTable)
        DataGrid1.AutoGenerateColumns 
= False
        
Dim ID As New BoundColumn
        ID.DataField 
= dt.Columns(0).ColumnName
        
'ID.Visible = False
        'ID.ItemStyle = New CssStyleCollection("Display:none")
        DataGrid1.Columns.Add(ID)

        
Dim ItemCn As New BoundColumn
        ItemCn.DataField 
= dt.Columns(1).ColumnName
        DataGrid1.Columns.Add(ItemCn)
        DataGrid1.Columns(
1).ItemStyle.Width = Unit.Pixel(cW1)
        
Dim ItemEn As New BoundColumn
        ItemEn.DataField 
= dt.Columns(2).ColumnName
        DataGrid1.Columns.Add(ItemEn)

        DataGrid1.DataSource 
= dt
        DataGrid1.DataBind()
        
If tbWidth <> 0 Then
            TD1.Width 
= tbWidth
            TD2.Width 
= tbWidth
        
End If
        
If DWidth <> 0 Then
            DataGrid1.Width 
= Unit.Pixel(DWidth)
        
End If
        
If DHeight <> 0 Then
            DataGrid1.Height 
= Unit.Pixel(DHeight)
        
End If
        
If _DivHeight <> 0 Then
            DG.Style.Item(
"Height"= _DivHeight
        
End If
    
End Sub

    
Public Property BoxWidth()
        
Get
            
Return tbWidth
        
End Get
        
Set(ByVal Value)
            tbWidth 
= Value
        
End Set
    
End Property

    
Private cW1 As Integer
    
Public Property ColumnWidth1()
        
Get
            
Return cW1
        
End Get
        
Set(ByVal Value)
            cW1 
= Value
        
End Set
    
End Property

    
Private cW2 As Integer
    
Public Property ColumnWidth2()
        
Get
            
Return cW2
        
End Get
        
Set(ByVal Value)
            cW2 
= Value
        
End Set
    
End Property

    
Public Property SelectedValue()
        
Get
            
Return tbValue.Text
        
End Get
        
Set(ByVal Value)
            tbValue.Text 
= Value
        
End Set
    
End Property

    
Private DWidth As Integer
    
Public Property DataGridWidth()
        
Get
            
Return DWidth
        
End Get
        
Set(ByVal Value)
            DWidth 
= Value
        
End Set
    
End Property

    
Private DHeight As Integer
    
Public Property DataGridHeight()
        
Get
            
Return DHeight
        
End Get
        
Set(ByVal Value)
            DHeight 
= Value
        
End Set
    
End Property

    
Private _DivHeight As Integer
    
Public Property DivHeight()
        
Get
            
Return _DivHeight
        
End Get
        
Set(ByVal Value)
            _DivHeight 
= Value
        
End Set
    
End Property

    
Private Sub DataGrid1_ItemDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        
Dim txt As Stringval
        
Dim i As Int16
        
Dim temp As String
        
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
            
'e.Item.Attributes("id") = e.Item.ItemType.ToString() + e.Item.ItemIndex.ToString()
            'If e.Item.ItemType <> ListItemType.AlternatingItem Then
            '    e.Item.Attributes.Add("OnMouseOut", "this.style.backgroundColor='';")
            'Else
            '    e.Item.Attributes.Add("OnMouseOut", "this.style.backgroundColor='oldlace';")
            'End If
            e.Item.Attributes.Add("OnMouseOut""this.style.backgroundColor='white';this.style.color='black'")
            e.Item.Attributes.Add(
"OnMouseOver""getfocus();this.style.backgroundColor='#0a246a';this.style.color='white';this.style.cursor='Default';")

            txt 
= e.Item.Cells(1).Text
            
val = e.Item.Cells(0).Text

            e.Item.Attributes.Add(
"onclick""setVal('" + DG.ClientID + "','" + tbDes.ClientID + "','" + tbValue.ClientID + "','" + txt + "','" + val + "');")
        
End If
    
End Sub

End Class


posted @ 2005-11-21 12:39  PointNet  阅读(2237)  评论(2编辑  收藏  举报