Wednesday, March 28, 2012

Programmatically creating UpdatePanel and creating Triggers

I'm connecting to a database to get a number then using a for loop to create multiple sets of controls. I'm generating a textbox (which happens to use an AutoCompleteExtender) then adding an EventHandler for that text box which updates a RadioButtonList by connecting to a database. I'm rendering the RadioButtonList in a new UpdatePanel control. Then I'm using the .RegisterAsyncPostBackControl() method for my scriptmanager to get async postback for the new update panel.

The only way I could get the EventHandler to fire is if I manually set .AutoPostBack = true for the TextBox. I'm assuming this is why the whole page posts back instead of just the UpdatePanel.

I'm completely new to these Ajax tools. I'd like to figure out a way to make only the contents of the update panel (the RadioButtonList) to update instead of the entire page. Here's some of my code:

protectedvoid Page_Load(object sender,EventArgs e)

{

// Code to connect to the database to get a value for numCoupons...

for (i = 0; i < numCoupons; i++)

{

Panel pnl =newPanel();pnl.ID ="pnl_" + i;

defaultCoupons_pnl.Controls.Add(pnl);

TextBox couponTitle =newTextBox();

couponTitle.ID ="couponTitle_" + i;

pnl.Controls.Add(couponTitle);

couponTitle.AutoPostBack =true;

couponTitle.TextChanged +=newEventHandler(couponTitle_TextChanged);

AjaxControlToolkit.AutoCompleteExtender CouponSearch =new AjaxControlToolkit.AutoCompleteExtender();CouponSearch.ID ="cpnAutoComplete_" + i;

CouponSearch.TargetControlID = couponTitle.ID;

CouponSearch.ServicePath ="AutoComplete.asmx";

CouponSearch.ServiceMethod ="GetCompletionList";

pnl.Controls.Add(CouponSearch);

UpdatePanel descriptionChoices =newUpdatePanel();descriptionChoices.ID ="couponChoices_" + i;

pnl.Controls.Add(descriptionChoices);

ScriptManager1.RegisterAsyncPostBackControl(couponTitle);

RadioButtonList descriptionList =newRadioButtonList();descriptionList.ID ="couponList_" + i;

descriptionChoices.ContentTemplateContainer.Controls.Add(descriptionList);

}a

}

protectedvoid couponTitle_TextChanged(object sender,EventArgs e)

{

// Code to connect to database and query results based on couponTitle entry...

string searchTerm = ((TextBox)sender).Text;findCoupon_objCmd.Parameters.Add(new SqlParameter("@dotnet.itags.org.searchTerm", searchTerm));

findCoupon_objConn.Open();

string couponNumber = ((TextBox)sender).ID.ToString().Replace("couponTitle_","");

SqlDataReader DR = findCoupon_objCmd.ExecuteReader();

RadioButtonList descriptionChoices = (RadioButtonList)defaultCoupons_pnl.FindControl("pnl_" + couponNumber).FindControl("couponChoices_" + couponNumber).FindControl("couponList_" + couponNumber);

descriptionChoices.DataSource = DR;

descriptionChoices.DataTextField ="description";

descriptionChoices.DataValueField ="allowedCouponId";

descriptionChoices.DataBind();

findCoupon_objConn.Close();

}

Hi,

You can seeCreating updatepanels at runtime? andIs it possible to add a ScriptManager and/or UpdatePanel to a MasterPage from a ContentPage dynaically? to find out how to Programmatically creating UpdatePanel.

Also seehttp://www.asp.net/ajax/documentation/live/overview/UpdatePanelOverview.aspx for the following section:

To add anUpdatePanel control to a page programmatically, you create a new instance of theUpdatePanel control. You then add controls to it by using theContentTemplateContainer property and theAdd(Control) method. Do not add controls directly to theContentTemplate property.

When anUpdatePanel control is added programmatically, only postbacks from controls in the same naming container as theUpdatePanel control can be used as triggers for the panel.

The following example shows how to programmatically add anUpdatePanel control to a page. The example adds aLabel and aButton control to the update panel by using theContentTemplateContainer property. Because theChildrenAsTriggers property istrue by default, theButton control acts as a trigger for the panel.

You cann't create Triggers Programmatically:

You can add anUpdatePanel control programmatically, but you cannot add triggers programmatically. To create trigger-like behavior, you can register a control on the page as an asynchronous postback control. You do this by calling theRegisterAsyncPostBackControl(Control) method of theScriptManager control. You can then create an event handler that runs in response to the asynchronous postback, and in the handler, call theUpdate() method of theUpdatePanel control.

Best Regards,

No comments:

Post a Comment