HomeActive Query Builder - HowTos and TroubleshootingActive Query Builder ASP.NET EditionHow to edit the query programmatically and update the visual query representation?

3.3. How to edit the query programmatically and update the visual query representation?

The following code samples let change the query programmatically on the server side and update the query builder control to reflect that changes.

Add the following code to your ASPX page:

<!-- Place button on the page -->
<asp:Button ID="btnAddCriteria" runat="server" OnClick="btnAddCriteria_Click" Text="Add criteria" />

<!-- Add button to the UpdatePanel trigger -->
<asp:UpdatePanel ID="UpdatePanel1" runat="server" EnableViewState="false">
	<Triggers>
		<asp:AsyncPostBackTrigger ControlID="btnAddCriteria" />
	</Triggers>
</asp:UpdatePanel>

<!-- Bind to the UpdatePanel update event and call 
     the QB.Web.Application.update() method if panel is updated -->
<script type="text/javascript">
	Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageLoaded);

	function PageLoaded(sender, args) {
		var panelId = "UpdatePanel1";
		if (args == null) return;
		if (args.get_panelsUpdated() == null) return;
		
		for (var i = 0; i < args.get_panelsUpdated().length; i++)
			if (args.get_panelsUpdated()[i].id == panelId) {
				QB.Web.Core.update();
			}
	}
</script>

Place the code to update the query in the button click event handler:

protected void btnAddCriteria_Click(object sender, EventArgs e)
{
	QueryBuilder queryBuilder = (QueryBuilderControl1).QueryBuilder;

	QueryColumnListItem row = queryBuilder.ActiveSubQuery.ActiveUnionSubquery.QueryColumnList.AddExpression("Schema.Table.Field");
	row.ConditionStrings[0] = " = 1";
}

You can find samples of updating the query programmatically in the demo projects included in the installation package and in the following chapter of the Knowledge Book: Programmatic modification of SQL queries.

If you want to update the Criteria Builder control programmatically, modify the QueryTransformer object on the server side and use the QB.Web.Application.CriteriaBuilder.update() method to update the control. Sample code to work with the QueryTransformer can be found in the Query Results demo and in this article: Changing query with the QueryTransformer.

This page was: Helpful | Not Helpful