【问题标题】:Using a line of code from codebehind in actual HTML page在实际 HTML 页面中使用代码隐藏中的一行代码
【发布时间】:2009-11-09 15:40:36
【问题描述】:

我有一个数据库连接,它从查询字符串中获取输入以访问适当的数据。但是我现在已经通过编码这些数据来升级东西。因此,我现在需要通过一个函数运行 QueryString 值来取消编码。

目前我有这个数据源的代码:

<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>

QueryString 值为 'camp'。

在我后面的代码中,我将通过以下代码处理这个值;

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

那么,如何将上述代码行合并到数据源中呢?即我需要用 'Convert.ToInt64(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString["camp"])))' 有效地替换 'camp'

我希望这是有道理的?

谢谢

【问题讨论】:

    标签: c# asp.net asp.net-2.0 asp.net-3.5


    【解决方案1】:

    将其更改为普通的 &lt;asp:Parameter 而不是 &lt;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 吗?

    【讨论】:

    • 对不起,我没有听从你的解释。
    • 谢谢。 Witrh 认为 UrlEncode / Decode 我目前执行如下操作,它工作得很好,尽管看起来你在技术上是正确的。请评论:要编码:HttpUtility.UrlEncode(TamperProofQueryString.encode("wibble"));解码:HttpUtility.UrlDecode(TamperProofQueryString.decode("wibble"));
    • 我现在将其更改为: TamperProofQueryString.decode(HttpUtility.UrlDecode( 功能似乎仍然相同,尽管现在技术上应该是正确的顺序。
    【解决方案2】:

    如果您想在没有代码隐藏的情况下完成所有操作,您可以执行以下操作:

    <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"]))));
    %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-03
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多