2009-11-09 39 views
0

我有一個數據庫連接,它從查詢字符串的輸入訪問適當的數據。不過,我現在通過編碼這些數據來升級東西。因此,我現在需要通過函數運行QueryString值來對其進行解碼。從代碼隱藏在實際的HTML頁面中使用一行代碼

目前我對數據源的代碼:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:spareathoughtConnectionString %>" 
        SelectCommand="campaign_Statistics" SelectCommandType="StoredProcedure"> 
        <SelectParameters> 
         <asp:QueryStringParameter DefaultValue="0" Name="tmp_Campaign" 
          QueryStringField="camp" Type="Int64" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 

查詢字符串值是「營地」。

在我的代碼後面我會通過下面的代碼處理這個值;

Convert.ToInt64(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString["camp"]))) 

那麼,如何才能將上面的代碼行納入數據源?即我需要'Convert.ToInt64(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString [「camp」]))''''有效地取代'陣營')'

我希望這是有道理的?

感謝

回答

2

將其更改爲普通<asp:Parameter而非<asp:QueryStringParameter。然後處理數據源的OnSelecting事件。您應該可以在代碼中設置SqlCommand的參數值。

在ASPX標記:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
       ConnectionString="<%$ ConnectionStrings:spareathoughtConnectionString %>" 
       SelectCommand="campaign_Statistics" SelectCommandType="StoredProcedure" 
       OnSelecting="SqlDataSource1_Selecting"> 
    <SelectParameters> 
     <asp:Parameter DefaultValue="0" Name="tmp_Campaign" Type="Int64" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

在後臺代碼:

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
{ 
    e.Command.Parameters["@tmp_Campaign"].Value = Convert.ToInt64(TamperProofQueryString.decode(HttpUtility.UrlDecode(Request.QueryString["camp"]))); 
} 

此外,望着那,你不應該的防僞解碼之前UrlDecode

+0

我不跟着你的解釋很抱歉。 – Munklefish

+0

謝謝你。 Witrh問候UrlEncode /解碼我目前做如下,它工作得很好,雖然它會出現你在技術上是正確的。請評論: 要編碼: HttpUtility.UrlEncode(TamperProofQueryString.encode(「wibble」));要解碼: HttpUtility.UrlDecode – Munklefish

+0

我現在改爲: TamperProofQueryString.decode(HttpUtility.UrlDecode( )似乎仍然功能相同,但現在應該在技術上正確的順序 – Munklefish

1

如果你想要做的這一切沒有代碼隱藏,你可以這樣做:

<asp:SqlDataSource 
    ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:spareathoughtConnectionString %>" 
    SelectCommand="campaign_Statistics" SelectCommandType="StoredProcedure"> 
</asp:SqlDataSource> 
<% 
    SqlDataSource1.SelectParameters.Add(
     "tmp_Campaign", 
     Convert.ToString(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString["camp"])))); 
%> 
+0

這似乎並沒有工作。 – Munklefish