ASP.NET User Control As FilterParameter
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?
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.
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.
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).
精彩评论