2011-06-03 70 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

相關問題