John D
John D

Reputation: 687

Asp.net sql or object-datacontrol select parameter having both DefaultValue and ControlParameter

I would like expert advice about what actually occurs when a sql or object select parameter is given BOTH a DefaultValue AND ControlParameter reference in the aspx code. In example below, the ControlParameter is a hidden-control that gets set in the form-load (say to a value of 50). QUESTION1: Which value (control-value 50 or default-value 99) has precedence when the select operation is executed?
Q2: Is it poor coding to supply a DefaultValue for a ControlParameter?

Sample from the aspx code:

<SelectParameters>
   <asp:ControlParameter ControlID="hidUID_DIVISION" Name="UID_DIVISION" 
        PropertyName="Value" Type="Int32" DefaultValue="99" />

Thanks.

Upvotes: 0

Views: 77

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 21825

According to MSDN:-

The DefaultValue property is used in scenarios where the parameter is bound to a value, but the value is null or cannot be resolved when the Parameter object is evaluated.

So it answers your first question, since your control parameter is already resolved and has the value 50 it will be used and default value will be ignored.

Is it poor coding to supply a DefaultValue for a ControlParameter?

No not at all, in fact you should provide default value to avoid any confusion a user may face. Just for example say you have created a simple search functionality with gridview & Sqldatasource control. You have one dropdown whiich will be used to filter the data. Now when nothing is selected in the dropdown and when page loads for the first time no records will be shown because the filter condition based on dropdown will fail. To avoid this you should provide a default value so that user can see all the records when nothing is selected in dropdown. Check this answer to understand what I mean.

Upvotes: 1

Related Questions