2017-01-11 85 views
0

我使用與SqlDataSource的選擇/插入/更新命令一個gridview。它適用於使用Gridview更新或更改值。然而,最近我添加了另一個使用Javascript來檢索數據的插件,我需要檢索數據作爲SQL的源數據。任何方式我可以做到這一點?VB.net - 傳遞自定義參數在SQL更新命令

事情是這樣的:

<form id="form1" runat="server"> 
    <div> 
    <input type="hidden" id="newname" runat="server" /> 
    <br /> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID_AM" DataSourceID="SqlDataSource1"> 
     <Columns> 
      <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> 
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
      <asp:BoundField DataField="StaffID" HeaderText="StaffID" SortExpression="StaffID" /> 
     </Columns> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT [ID], [Name], [StaffID] FROM [Record]"   
     ConflictDetection="CompareAllValues" 
     UpdateCommand="UPDATE [Record] SET [Name] = @Name, [StaffID] = @StaffID WHERE [ID] = @original_ID AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([StaffID] = @original_StaffID) OR ([StaffID] IS NULL AND @original_StaffID IS NULL))"> 
     <UpdateParameters> 
      <asp:Parameter Name="Name" Type="String" /> 
      <asp:Parameter Name="StaffID" Type="String" /> 
      <asp:Parameter Name="original_ID" Type="Int32" /> 
      <asp:Parameter Name="original_Name" Type="String" /> 
      <asp:Parameter Name="original_StaffID" Type="String" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 
</div> 
</form> 

當我使用的GridView更新行中,插件會在隱藏輸入「newname.value」生成一個新的名稱和存儲。我怎樣才能把這個值放入更新參數?

我想到了幾個方法可以做到:

  • 找到一些方法,以便SQL參數會以某種方式採取價值
  • 覆蓋使用正常的SQL更新的更新命令。但我想保持樂觀的併發功能。

回答

0

感謝您的回答。後來我找到了一個更直接的方法,看起來更容易,似乎適合我的情況。

我只是添加的更新事件的參數:在執行SQL更新之前

Protected Sub SQL_Update(source As Object, e As SqlDataSourceCommandEventArgs) Handles SqlDataSource.Updating 

    dim staffid as string = newname.valie 
    // presudo for other passing values 

    e.Command.Parameters("@StaffID").Value = staffid 
    //and for other parameters 

End Sub 

然後它可以添加這些自定義參數

0

您正在使用asp.net web表單我想,這樣說沒有(簡單)的方式,使與客戶端的JavaScript服務器端asp.net DataSource接口,而不用自己寫asp.net控制基於JavaScript插件。

話雖這麼說,我發現這是使用webMethods的最簡單方法。如果您更喜歡使用Jquery,請參閱link。如果你想讓asp.net做繁重的工作,請參閱this link。首先,你需要添加一個scriptManager到你的頁面,它將「寫入」javascript函數,以便對服務器端webmethods進行異步調用,這將會在您的aspx文件:

<head runat="server"> 
    <title></title> 
    <script type="text/javascript"> 
     function HandleIT() { 
      var name = document.getElementById('<%=txtname.ClientID %>').value; 
      var address = document.getElementById('<%=txtaddress.ClientID %>').value; 
      PageMethods.ProcessIT(name, address, onSucess, onError); 
      function onSucess(result) { 
       alert(result); 
      } 
      function onError(result) { 
       alert('Something wrong.'); 
      } 
     } 
    </script> 
</head> 
<div> 
     <p>Say bye-bey to Postbacks.</p> 

     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager> 
     <asp:TextBox ID="txtname" runat="server"></asp:TextBox> 
     <br /> 
     <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox> 
     <br /> 
     <asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="HandleIT(); return false;" /> 
    </div> 

這裏是將WebMethod,(這將是你的.aspx.vb文件):

Imports System.Web.Services 
<WebMethod> _ 
Public Shared Function ProcessIT(name As String, address As String) As String 
    Dim result As String = (Convert.ToString((Convert.ToString("Welcome Mr. ") & name) + ". Your address is '") & address) + "'." 
    Return result 
End Function 

你可以做一個WebMethod對於每個SQL查詢但即使如此,我不要以爲你可以引用那些SqlDataSource對象。您將需要使用不同的方式使用vb.net來獲取數據,然後從webmethod中返回該數據。

的Webmethods使用JSON發送和接收對象,這意味着它們序列化和反序列化對象和JSON你。我發現這是最兼容返回的JavaScript對象類型是列表)(詞典(字符串,對象)),那麼你的JSON應該是這樣的:

[{"ID": 6, "Name":"sec0ndHand","StaffId" : "employeeOfTheMonth1"},{"ID": 3, "Name":"Tomy","StaffId" : "notEmployeeOfTheMonth1"}] 

讓你的WebMethod會是這個樣子的結束:

<WebMethod> _ 
Public Shared Function ProcessIT(name As String, address As String) As list(of dictionary(of string, object))) 
    Dim result As list(of dictionary(of string, object))) = New list(of dictionary(of string, object))) 
    'get/update/delete/insert your data 

    'convert your return object into list(of dictionary(of string, object))) 
    Return result 
End Function