Well there are two ways I know of these (select) parameter values can be set but I've come across a third this morning, so I thought I'd share (all).
1. You can setup a ControlParameter and link it to a control at design time. Like shown below where we have a link to the PlayerID field of the dropdownlist.:
<SelectParameters><asp:ControlParameter ControlID="DropDownList1" Name="PlayerID" PropertyName="SelectedValue"Type="String" /></SelectParameters>
2. In situations where the value that the DataSource is linked to does not relate to a control on the form but rather to some code e.g. the the current month (accessible programmatically via Now.Year)... You can do two things:
(a) Don't add the select parameter to the asp page but instead create it at runtime probably in the page load event.. below I use the Add method to add the parameter and set its value i.e. Now.Year.ToString.
If Not Page.IsPostBack Then ObjectDataSource1.SelectParameters.Add("myparam", TypeCode.String, Now.Year.ToString)Else 'Do nothing or you will be adding to the parameter already presentEnd If
One note about this however is that you'll have check for postbacks (as I've done above).. if there is a postback you'll have to skip re-adding the parameter. Yea... I know this method isn't good at all.. but in some situations where the parameter doesn't need to be frequently updated, it can be an option.
(b) If the parameter value isn't that important and security of it isn't a priority, you can go the lond-winded route.. i.e. Create a "holder control" e.g. a label or textbox on the asp page, then in the Form Load event, set the parameter value to the Label.Text property... THEN, use option (1) and create a ControlParameter and bind that parameter to the created Label so you get something like below:
<SelectParameters><asp:ControlParameter ControlID="Label1" Name="myparam" PropertyName="Text"Type="String" /></SelectParameters>
This should work .. but also remember to set the Label.Visible property to "false".
3. Now to the one I found this morning on the asp.net 2 site. This one is rather simple and I tried it a second ago and it works well. This scenario is where you need to set a parameter value in code like in (2) above. The approach? Simply... use the DataSource_Selecting event... like below:
Protected Sub ObjectDataSource1_Selecting _(sender As Object, e As ObjectDataSourceSelectingEventArgs) _Handles ObjectDataSource1.Selecting e.InputParameters("myparam") = Now.Year.ToStringEnd Sub
And.. that's it! When the DataSource goes to fill itself with data, it comes to this event and picks up the input parameter.
K ta!
tagged: professional // Comments [0]
Related posts:Select a random row in MS SQL...Regular expressionsVS2005, ASP.NET 2 & DLLs, DLLs..MS's ASP.NET and.. PHP2-way databinding cascading drop down lists within a FormViewApplicationName Property when customising providers
Disclaimer The posts on this blog are provided "AS IS" with no warranties. The opinions expressed herein are my own personal opinions and do not represent any other person's views in anyway.