开发者

Using a line of code from codebehind in actual HTML page

I have a database connection which takes an input from the querystring to access the appropriate data. However i have now upgraded things by encoding this data. As a result i now need to run the QueryString value through a function to unencode it.

At presen开发者_Python百科t i have this code for the DataSource:

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

The QueryString value is 'camp'.

In my code behind i would process this value via the following code;

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

So, how can incorporate the above line of code into the datasource? ie i need to effectively replace 'camp' with 'Convert.ToInt64(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString["camp"])))'

I hope this makes sense?

Thanks


Change it to a plain <asp:Parameter rather than a <asp:QueryStringParameter. Then handle the OnSelecting event for the datasource. You should be able to set the parameter value for the SqlCommand in your code-behind there.

In the aspx markup:

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

In the code-behind:

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

Also, looking at that, shouldn't you UrlDecode before the TamperProof decode?


If you want to do all of it without a codebehind, you could do something like:

<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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜