2011-06-03 71 views
0

假设我有在我的数据库如下表:什么使用,而不是`命令参数属性`在`gridview`当我在客户端使用jQuery创建gridview?

Tbl_Persons: 
Id Country Name  
1 Australia Ben 
2 Japan  John 
3 Korea  Libby 
4 Australia Raymond 

我用下面的查询和使用DataTableGridview结果绑定。

select id,Country,Name 
     from tbl_persons 
      where country='Australia' 

gridview我有两个Bound Columns和1 template column。 在模板列中,我通常会使用ImageButton并使用Command Argumnet propertyID field指定为image button。当用户点击浏览器中的imagebutton时,我会让command argument value切断对它的一些操作。

<asp:GridView ID="GridView1" runat="server"> 
      <Columns> 
       <asp:BoundField DataField="ountry" HeaderText="Country" /> 
       <asp:BoundField DataField="Name" HeaderText="Name" /> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:ImageButton ID="ImageButton1" runat="server" 
          CommandArgument='<%# Eval("Id") %>' /> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

正如你在运行模式知道什么时候用户在浏览器中查看的代码的源代码,因为他们在服务器上创建的command argument值是无法观测。用户不能使用浏览器更改值,并将更改后的值发送到服务器。

但是gridview是服务器端有回发。所以我使用jquery并填写My gridview,并在客户端添加trtd标签。 对于ID field of my database,因为我在客户端没有command argument,我将Id的每个TD标签分配为Id field。 但用户可以在浏览器中更改Id values,并将无效数据发送到服务器,这是一场灾难。 这是一张我的代码:

script type="text/javascript"> 
    function BindGridView() { 
    $.ajax({ 
      type: "POST", 
      url: "Default.aspx/GetNames", 
      data: "{}", 
      contentType: "application/json", 
      dataType: "json", 
      success: function (data) { 
     for (var i = 0; i < data.d.length; i++) { 
       $("#NamesGridView").append("<tr><td id="+data.d[i].Id+">" + data.d[i].Country + 
              "</td><td>" + data.d[i].Name + "</td></tr>"); 
      } 
      } 
      }) 
     } 
</script> 

所以我能做些什么来防止用户更改值? 在客户端有没有什么方法像Command Argument?

+0

任何想法......? – 2011-06-03 13:01:36

回答

2

进口的System.Web 进口System.Web.Services 进口的System.Web。 Services.Protocols 进口System.Collections.Generic System.Collections中进口进口 System.Web.UI.Page

_ _ _ _ 公共类AutoCompleteService 继承System.Web.Services.WebService

<WebMethod(EnableSession:=True)> _ 
<System.Web.Script.Services.ScriptMethod()> _ 
Public Function GetPhysicianCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String() 
    Dim lstItem As New List(Of String) 
    Dim pag As New Page 
    Dim oCommon As Common = CType(Context.Session("CommonSetting"), Common) 
    Dim oDataSet As New System.Data.DataSet 
    Dim SQLConn As System.Data.SqlClient.SqlConnection = Nothing 
    Try 
     SQLConn = New System.Data.SqlClient.SqlConnection(General.ConnectionString) 
     SQLConn.Open() 
     Dim param As System.Data.SqlClient.SqlParameter() = SqlHelperParameterCache.GetSpParameterSet(SQLConn, "TempAutocompletePhysician") 
     ' @AccountID AS INT 

     param(0).Value = oCommon.AccountID 'CInt(contextKey) 

     ' @prefixText AS VARCHAR(50) 
     param(1).Value = prefixText 

     oDataSet = SqlHelper.ExecuteDataset(SQLConn, Data.CommandType.StoredProcedure, "TempAutocompletePhysician", param) 

     For Each oRow As System.Data.DataRow In oDataSet.Tables(0).Rows 
      lstItem.Add(oRow("Context").ToString) 
      If lstItem.Count = count Then 
       Exit For 
      End If 
     Next 
     Return lstItem.ToArray() 
    Catch ex As Exception 
     Return lstItem.ToArray() 
    Finally 
     If SQLConn IsNot Nothing AndAlso SQLConn.State = System.Data.ConnectionState.Open Then 
      SQLConn.Close() 
      SQLConn = Nothing 
     End If 
     oDataSet = Nothing 
    End Try 

    a 
    'Return lstItem.ToArray() 
End Function 

末级

+0

检查这个代码我在这里使用会话时,数据来自用户,你没有任何办法..总是发送到服务器 – 2011-06-03 14:49:37

+0

? – 2011-06-03 14:50:40

+0

你对模拟类似数字签名的想法返回特定的数据 – 2011-06-03 14:56:50

1

这时您必须使用Web服务,jQuery的调用Web服务,发送和使用JSON接收数据..

+0

如果错误的ID发送,只是显示消息,ID是错误的....或发送一些事情,表明ID是错的... – 2011-06-03 14:04:56

+0

根据我的选择声明网格视图显示'1本,4雷蒙德。用户点击图像按钮我可以根据ID显示一些细节信息。但是如果有人更改了标识并将其发送到服务器,最终用户可以看到其他记录的详细信息。例如,如果将id = 1更改为id = 2,可以看到John的详细信息,这是一场灾难。所以价值没有错,这是被禁止看到的。 – 2011-06-03 14:17:12

+0

严其复杂的东西,但你可以把一些证书有效数据访问特定用户 – 2011-06-03 14:33:08

相关问题