views:

127

answers:

3

When adding WHERE-clause parameters in a DataSource, you can specify an existing form control (textbox, dropdown, etc) by selecting "Source: Control" and then picking the "ControlID" from the next dropdown.

Is there some way to configure a user control so it will appear in the Controls list?

OR

How can I use a property of the user control as a query parameter?

A: 

Your control will need to implement the IPostBackDataHandler interface so that it can "report" the selected index or value to the server when your page posts back. The interface is implemented by both TextBox and DropDownList. I am not sure whether the Visual Studio diaolg uses it to determine whether to display it in the Controls list, but it is worth a try. I am still looking into how this works but I thought I would post this to get you looking into that.

Daniel Dyson
A: 

You could create custom parameters like so http://fredrik.nsquared2.com/viewpost.aspx?PostID=355

but it might be easier to do the following:

We add a selecting event to the data source which will retrieve a public property from the user control and set it as a parameter. The public property of the user control retrieves the value of some web control like a textbox and exposes it as a property.

Given the following SqlDataSource

<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
  SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"

  <SelectParameters>
    <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
  </SelectParameters>

 </asp:sqlDataSource>

We add a selecting event like so

protected void EmployeeDetailsSqlDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["@EmpID"].Value = MyUserControl.EmpID;
}

The UserControl has a property like so:

public int EmployeeID {
  get {
      return Convert.ToInt32(TextBox1.Text);
  }
}

Where TextBox1 is where the user enters the employeeID. You could also use ViewState to store the EmployeeId property in case you want to persist across postbacks (depending on your scenario).

Raj Kaimal
+1  A: 

I'm not sure if you can do that with System.Web.UI.UserControl, but I was able to do it with System.Web.UI.WebControls.WebControl.

You have to mark your WebControl class as [ControlValueProperty("PropertyName")].

PropertyName is the name of the property who's value will be used in the where clause. For eg- in case of TextBox it's "Text", in case of DropDownList it's "SelectedValue"

I tried doing this with UserControl but couldn't make it work, will let you know if I find a solution.

Vivek Athalye
I converted my usercontrol to a web control, added the controlvalueproperty as described, but the webcontrol id's are still not in the list. Thoughts?
Steven
I'm not sure why you are not getting the control in the list. I created a separate project just to make sure that I've not missed out anything, and I was able get the control in the where clause.I've uploaded my code @ http://www.mediafire.com/file/gm5wjjitjiw/Server Control in DataSource Where Clause_v2.zipYou can take a look at it. I've created a TestServerControl that exposes SearchString property that is used in DataSource. PS: In the web page, I've added buttons just for the sake of causing post back. There is no code behind as such.HTH
Vivek Athalye
FYI...The ControlValuePropertyAttribute attribute is applied to a control to specify its default property that a ControlParameter object binds to at run time. (ref: http://msdn.microsoft.com/en-us/library/system.web.ui.controlvaluepropertyattribute.aspx , http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.controlparameter.aspx)ControlValueProperty - Used by data source parameters to get the "intrinsic" value of the control. (ref: http://weblogs.asp.net/leftslipper/archive/2007/02/15/attributes-to-consider-applying-when-writing-a-custom-control.aspx)
Vivek Athalye
I used ControlValuePropertyAttribute instead of ControlValueProperty and it worked perfectly. Thank you!
Steven