ASP.NET - Best practice for setting SqlDataSource parameter value from QueryString @ Page Load?

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

Is there a good practice for assigning a QueryString value to the SqlDataSource parameter defined on an ASPX page?

Options I know of:

  • Do not include parameters in the .aspx file. Add them directly through codebehind - and assign Param.DefaultValue as you add.
  • Set Param.DefaultValue parameter in page load (Codeblock 2)

Both of them use DefaultValue, a property that was clearly not intended to be used that way. Does ASP.NET tagging have a recommended method for accomplishing 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
+3
source share
2 answers

This is best handled using DataSource events directly, rather than PageLoad events.

To set the QueryString to Select, add this to your data source:

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

This indicates that the DataSource Select searches for “StoreID = x” in the QueryString and sets the value of “StoreID” to “x” from the URL as you mentioned.

To set the default value for StoreID, connect to the data source selection event as follows:

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

: , .

+4

, Page_Load 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;
    }
+1

Source: https://habr.com/ru/post/1733638/


All Articles