AJAX无刷新回调,根据参数判断执行回调

在做网页时,同一页面上通常我们会不止一次的用到回调,那么我们用方法能实现这样的效果呢?是定义多个回传接口还是通过参数判断来执行相应的语句块呢!

下面我用的是通过参数判断来实现

.aspx页面的head部分加入以下代码:

< script language  = " javascript "  type  = " text/javascript "   >
function  selectpro(f,context)  {
var val =f;
<%=cbReference%>
  }
              
function  cart(f,context)  {
var val =f;
<%=cbReference%>
  }
                  
function  fava(f,context)  {
var val =f;
<%=cbReference%>
  }
                  

function  ReceiveServerData(rValue,context) {
var context_obj=document.getElementById(context);
//context_obj.style.width=60;
//
context_obj.style.height=40;
context_obj.innerHTML  =rValue;
//startChange(context);
}


</ script >

上面代码定义了几个Button的OnClick函数,通过它把参数传到后台,在后台通过参数的判断来执行相应的语句块,具体如下:

.aspx.vb页代码:

Implements System.Web.UI.ICallbackEventHandler    ' 继承接口
    Public cbReference As String
    Public returnvalue As String       ' 定义两个公共变量,以便在后面代码中使用


’ 接受处理结果函数
Public   Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
        
Return returnvalue
    
End Function



‘ 事件处理
    
Public   Sub RaiseCallbackEvent(ByVal eventArgument As StringImplements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        
If IsNumeric(eventArgument) Then     ‘ 判断参数
            
Dim pagesource As New PagedDataSource
            pagesource.AllowPaging 
= True
            pagesource.PageSize 
= 10
            pagesource.DataSource 
= reader.Rows
            
Dim first As Integer = pagesource.FirstIndexInPage

-------------------------------------------------------------------------------------------------------------------------------
            
Dim sw1 As StringWriter = New System.IO.StringWriter()
            
Dim writer1 As HtmlTextWriter = New HtmlTextWriter(sw1)
            writer1.AddAttribute(
"href""#")
            writer1.AddAttribute(
"onclick""selectpro('0','context')")
            writer1.RenderBeginTag(HtmlTextWriterTag.A)
            writer1.Write(
"[首页]")
            writer1.RenderEndTag()

            
Dim j, i As Integer
            i 
= CInt(eventArgument)
            
If i < 6 Then
                
For j = 1 To 10
                    writer1.AddAttribute(
"href""#")
                    writer1.AddAttribute(
"onclick""selectpro(" + (j - 1).ToString + ",'context')")
                    writer1.RenderBeginTag(HtmlTextWriterTag.A)
                    writer1.Write(
"&nbsp;" + j.ToString)
                    writer1.RenderEndTag()
                    
If j = pagesource.PageCount Then
                        
Exit For
                    
End If
                
Next
            
Else
                
For j = 1 To 10
                    writer1.AddAttribute(
"href""#")
                    writer1.AddAttribute(
"onclick""selectpro('" + (i - 4 - 1).ToString + "','context')")
                    writer1.RenderBeginTag(HtmlTextWriterTag.A)
                    writer1.Write(
"&nbsp;" + (i - 4).ToString)
                    writer1.RenderEndTag()
                    i 
= (i - 4+ 1
                    
If i > pagesource.PageCount Then
                        
Exit For
                    
End If
                
Next
            
End If
            writer1.AddAttribute(
"href""#")
            writer1.AddAttribute(
"onclick""selectpro('" + pagesource.PageCount.ToString + "','context')")
            writer1.RenderBeginTag(HtmlTextWriterTag.A)
            writer1.Write(
"&nbsp;[尾页]")
            writer1.RenderEndTag()

            writer1.AddAttribute(
"width""576")
            writer1.AddAttribute(
"style""font-size:13px")
            writer1.AddAttribute(
"cellpadding""0")
            writer1.AddAttribute(
"cellspacing""0")
            writer1.RenderBeginTag(HtmlTextWriterTag.Table)
            writer1.RenderBeginTag(HtmlTextWriterTag.Tr)
            writer1.AddAttribute(
"width""80")
            writer1.RenderBeginTag(HtmlTextWriterTag.Th)
            writer1.RenderEndTag()

            writer1.AddAttribute(
"width""206")
            writer1.RenderBeginTag(HtmlTextWriterTag.Th)
            writer1.Write(
"产品名称")
            writer1.RenderEndTag()

            writer1.AddAttribute(
"width""60")
            writer1.RenderBeginTag(HtmlTextWriterTag.Th)
            writer1.Write(
"批发价")
            writer1.RenderEndTag()

            writer1.AddAttribute(
"width""60")
            writer1.RenderBeginTag(HtmlTextWriterTag.Th)
            writer1.Write(
"品  牌")
            writer1.RenderEndTag()

            writer1.AddAttribute(
"width""70")
            writer1.RenderBeginTag(HtmlTextWriterTag.Th)
            writer1.Write(
"产  地")
            writer1.RenderEndTag()

            writer1.AddAttribute(
"width""50")
            writer1.RenderBeginTag(HtmlTextWriterTag.Th)
            writer1.RenderEndTag()

            writer1.AddAttribute(
"width""50")
            writer1.RenderBeginTag(HtmlTextWriterTag.Th)
            writer1.RenderEndTag()
            writer1.RenderEndTag()

            
Dim row As Integer
            
For row = 0 To 9
                pagesource.CurrentPageIndex 
= CInt(eventArgument)
                writer1.RenderBeginTag(HtmlTextWriterTag.Tr)
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                writer1.AddAttribute(
"src""upload/" + reader.Rows(pagesource.FirstIndexInPage + row).Item("s_imgurl").ToString)
                writer1.AddAttribute(
"style""border: 1px solid Silver")
                writer1.AddAttribute(
"width""78")
                writer1.RenderBeginTag(HtmlTextWriterTag.Img)
                writer1.RenderEndTag()
                writer1.RenderEndTag()

                writer1.AddAttribute(
"width""206")
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                
Dim proid As Integer = CType(reader.Rows(pagesource.FirstIndexInPage + row).Item("productid").ToString, Integer)
                writer1.AddAttribute(
"href""productdetailsinfo.aspx?productid=" & proid)
                writer1.RenderBeginTag(HtmlTextWriterTag.A)
                writer1.Write(
"&nbsp;&nbsp; " + reader.Rows(pagesource.FirstIndexInPage + row).Item("productname").ToString)
                writer1.RenderEndTag()
                writer1.RenderEndTag()

                writer1.AddAttribute(
"width""60")
                writer1.AddAttribute(
"align""center")
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                writer1.Write(reader.Rows(pagesource.FirstIndexInPage 
+ row).Item("price").ToString)
                writer1.RenderEndTag()

                writer1.AddAttribute(
"width""60")
                writer1.AddAttribute(
"align""center")
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                writer1.Write(reader.Rows(pagesource.FirstIndexInPage 
+ row).Item("pinpai").ToString)
                writer1.RenderEndTag()

                writer1.AddAttribute(
"width""70")
                writer1.AddAttribute(
"align""center")
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                writer1.Write(reader.Rows(pagesource.FirstIndexInPage 
+ row).Item("changdi").ToString)
                writer1.RenderEndTag()

                writer1.AddAttribute(
"width""50")
                writer1.AddAttribute(
"align""center")
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                writer1.AddAttribute(
"type""button")
                writer1.AddAttribute(
"value""购买")
                
Dim aaaa As String
                aaaa 
= Trim(reader.Rows(pagesource.FirstIndexInPage + row).Item("productid").ToString) + "*" + Trim(reader.Rows(pagesource.FirstIndexInPage + row).Item("productname").ToString) + "*" + Trim(reader.Rows(pagesource.FirstIndexInPage + row).Item("price").ToString)
                
'writer1.AddAttribute("onclick", "cart('12*34*56')")

                writer1.AddAttribute(
"onclick""cart('" + aaaa + "')")
                writer1.RenderBeginTag(HtmlTextWriterTag.Input)
                writer1.RenderEndTag()
                writer1.RenderEndTag()

                writer1.AddAttribute(
"width""50")
                writer1.AddAttribute(
"align""center")
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                writer1.AddAttribute(
"type""Button")
                writer1.AddAttribute(
"value""收藏")
                
Dim bbbb As String
                bbbb 
= Trim(reader.Rows(pagesource.FirstIndexInPage + row).Item("productid").ToString) + "*" + "bbbb"
                writer1.AddAttribute(
"onclick""fava('" + bbbb + "')")
                writer1.RenderBeginTag(HtmlTextWriterTag.Input)
                writer1.RenderEndTag()
                writer1.RenderEndTag()
                writer1.RenderEndTag()

                writer1.RenderBeginTag(HtmlTextWriterTag.Tr)
                writer1.AddAttribute(
"height""3")
                writer1.AddAttribute(
"colspan""7")
                writer1.RenderBeginTag(HtmlTextWriterTag.Td)
                writer1.RenderBeginTag(HtmlTextWriterTag.Hr)
                writer1.RenderEndTag()
                writer1.RenderEndTag()
------------------------------------------------------------------------------------------------------------------------------通过后台代码生成一个表格
                
If (pagesource.FirstIndexInPage + row) = pagesource.Count Then
                    
Exit For
                
End If
            
Next
            writer1.RenderEndTag()
            returnvalue 
= sw1.ToString
        
End If

        
If Split(eventArgument, "*").Length = 3 Then '断定是购物车触发
            If My.User.IsAuthenticated Then
                
Dim cart()
                
ReDim cart(2)
                cart(
0= Split(eventArgument, "*")(0'取得产品ID
                cart(1= Split(eventArgument, "*")(1'取得产品名称 
                cart(2= CDec(Split(eventArgument, "*")(2)) '取得产品批发价 
                Profile.Cart.AddItem(cart(0), cart(1), cart(2))
            
Else
                
MsgBox("您尚未登陆,不能使用此功能!")
            
End If
        
End If

        
If Split(eventArgument, "*").Length = 2 Then '断定是加入衣柜触发
            If My.User.IsAuthenticated Then
                
Dim id As String
                id 
= Split(eventArgument, "*")(0).ToString '取得产品ID
                Profile.Favorites.AddProduct(id)
                
'returnvalue = "商品成功加入您的衣柜中!"
            Else
                
MsgBox("您尚未登陆,不能使用此功能!")
            
End If
        
End If

    
End Sub

在这里,还有一个想法,是不是能定义多个这样的接口呢?还得再研究下,是不是不行呢????

呵呵。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值