views:

174

answers:

2

Let's say we have a value coming in through the URL of a page. Example: www.test.com/page.aspx?&Key=Val. Val is retrievable through Request.Querystring("Key").

Is there an accepted best practice for assigning a QueryString Value to a parameter of a SqlDataSource defined on an ASPX page?

Options that I know of:

  1. Do not include parameters in the ASPX file. Add them directly through the codebehind -- and assign Param.DefaultValue upon adding.
  2. Set Param.DefaultValue in the page load (Codeblock 2)

Both of these use DefaultValue, a property that clearly was not intended to be used in this manner. Do the ASP.NET overlords have a recommended method in mind for achieving this common task?

Codeblock 1

    <SelectParameters>
        <asp:Parameter Name="StoreID" Type="String" DefaultValue="-1" />
    </SelectParameters>

Codeblock 2:

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SD1.SelectParameters("StoreID").DefaultValue = Request.QueryString("StoreID")
 End Sub
+1  A: 

I did sometimes ago it with object datasource and i didn't add default value to Page_Load or ASPx.

public class QueryStringKey<T>
{
    public static implicit operator QueryStringKey<T>(string key)
    {
        return new QueryStringKey<T> { Key = key };
    }

    public string Key { get; set; }

    public T Value
    {
        get
        {
            if (HasValue == false)
            {
                throw new ArgumentNullException(Key);
            }

            TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
            try
            {
                return (T)converter.ConvertFromString(_valueString);
            }
            catch
            {
                return (T)Activator.CreateInstance<T>();
            }
        }
    }

    public bool HasValue
    {
        get
        {
            return !String.IsNullOrEmpty(_valueString);
        }
    }

    private string _valueString
    {
        get
        {
            return HttpContext.Current.Request.QueryString[Key];
        }
    }
}

public class QueryStringKeys
{
    public static QueryStringKey<int> StoreId = "StoreId";
}

    protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (QueryStringKeys.StoredId.HasValue)
            e.InputParameters["StoreId"] = QueryStringKeys.StoreId.Value;
    }
cem
I would have guessed that SqlDataDource Selectparameters would allow this, but they do not. Unable to convert string to Parameter. For now, I just adjust the DefaultValue property, but it feels wrong.
hamlin11
I checked it out now. parameter values able to change when selecting event fire but i think its directly accessing SqlCommand; e.Command.Parameters["@StoreId"].Value = QueryStringKeys.StoreId.Value;
cem
Unfortunately I do not have a Value property on the parameter, only a defaultValue. Fortunately Jamon informed me about the QueryStringParameter that seems to have been designed specifically for this purpose.
hamlin11
+2  A: 

This is best handled using the DataSource events directly, not the PageLoad events.

To set the QueryString in the Select, add this to your datasource:

    <SelectParameters>
        <asp:QueryStringParameter Name="StoreID" QueryStringField="StoreID" Type="Int64" />
    </SelectParameters>

This tells the DataSource Select to look for "?StoreID=x" in the QueryString and set StoreID to 'x' from the URL as you mentioned.

To set a default value for StoreID, hook into the DataSource Selecting Event like this:

Protected Sub SD1DataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SD1DataSource.Selecting
    If e.Command.Parameters("@StoreID").Value Is Nothing Then
        e.Command.Parameters("@StoreID").Value = 155
    End If
End Sub

Edit: modified to use your variable names.

BenB
Wow, had no idea that was possible. Thanks!
hamlin11
no problem, we had this issue a lot. Let me know if you need help hooking it all in.
BenB