ASP.NET Custom Control as FilterParameter

When adding WHERE clause parameters to the DataSource, you can specify an existing form control (text box, dropdown menu, etc.) by selecting "Source: Control" and then selecting "ControlID" from the following dropdown list.

Is there a way to customize the custom control to appear in the list of controls?

OR

How can I use a custom control property as a request parameter?

+2


source to share


3 answers


I'm not sure if you can do it with System.Web.UI.UserControl

, but I was able to do it with System.Web.UI.WebControls.WebControl

.

You must mark your WebControl class as [ControlValueProperty("PropertyName")]

.



PropertyName is the name of the property whose value will be used in the where clause. For example, in the case of the TextBox it is "Text", in the case of the DropDownList it is "SelectedValue"

I tried to do it with UserControl but couldn't get it to work will tell you if I can find a solution.

+2


source


Your control will need to implement the IPostBackDataHandler interface so that it can "communicate" the selected index or value to the server when your page returns. The interface is implemented by both TextBox and DropDownList. I'm not sure if Visual Studio uses diaolg to determine whether to show it in the Controls list, but it's worth a try. I'm still researching how it works, but I thought I'd post this so you can figure it out.



0


source


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

but it might be easier to do the following:

We are adding a select event to the data source that will retrieve the public property from the custom control and set it as a parameter. A public property of a custom control retrieves the value of some Web control, such as a text box, 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 select event this way

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

      

The UserControl has the following property:

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

      

Where TextBox1 is where the user enters the employeeID. You can also use ViewState to store the EmployeeId property in case you want to keep the postback (depending on your scenario).

0


source







All Articles