Showing posts with label checkboxes. Show all posts
Showing posts with label checkboxes. Show all posts

Monday, March 26, 2012

MutuallyExclusiveCheckBoxExtender doesnt work if elements are added dynamically

I create my table on the fly and added checkboxes as well as MECBextenders.

The client script is not working on the page. It works as expected if the MECBs are added during design time.

Here is some code to demonstrate population:

1string extenderKey ="AllTheSameKey";23foreach (MvpPoint pointin _selected.FolderPoints)4 {5678switch (point.PointType)9 {10case tPointType.DO:11case tPointType.CP:12 {13continue;14 }15default:16 {17 trow =new TableRow();1819 CheckBox chkSelected =new CheckBox();20string sPointId = point.Handle.ToString();21 chkSelected.ID ="chk" + sPointId;22 chkSelected.Visible =true;23if (point.PointType == tPointType.AI)24 chkSelected.Enabled =true;25else26 chkSelected.Enabled =true;2728 AjaxControlToolkit.MutuallyExclusiveCheckBoxExtender chkExtender =new AjaxControlToolkit.MutuallyExclusiveCheckBoxExtender();29 chkExtender.ID ="chkExt" + sPointId;30 chkExtender.TargetControlID = chkSelected.ID;31 chkExtender.Key = extenderKey;32

The problem ONLY exists if the MutuallyExclusiveCheckBoxExtender is cointeined inside a TabPanel. The IDs get changed by the cointeiner control and the jscript fails to find them.

Saturday, March 24, 2012

Need a way to reemember user selected checkboxes in a gridview in an update panel during a

I'm using ASP.NET 2.0 & Ajax. I have a gridview in an update panel. One of the templated columns in the gridview is a checkbox. In addition, two textboxes provide asynchronous triggers on their "TextChanged" event. I need to remember and re-apply the user selected checkboxes after an asynch postback. I've tried using ViewState as follows:

********************* aspx page code ******************************

<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline"
OnUnload="GetCheckedStores" OnLoad="SetCheckedStores">
<ContentTemplate>
<asp:Panel ID="StoresPnl" runat="server" Height="50px" Width="608px">
<asp:GridView ID="grvStores" runat="server" AutoGenerateColumns="false" DataMember="DefaultView"
DataSourceID="dsStores" DataKeyNames="Store" EnableTheming="true" Width="400px">
<Columns>
<asp:TemplateField>
<ItemStyle Width="50px" />
<ItemTemplate>
<asp:CheckBox ID="chbStore" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Store" HeaderText="Store #">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="BatchCount" HeaderText="Batch Count">
<ItemStyle Width="80px" />
</asp:BoundField>
</Columns>
</asp:GridView>

</asp:Panel>
<!-- ID="StoresPnl" -->
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txbFromDate" EventName="TextChanged" />
<asp:AsyncPostBackTrigger ControlID="txbToDate" EventName="TextChanged" />
</Triggers>

</asp:UpdatePanel>


******************************* code behind ******************************************

string reportDescription;
string reportCode;
ArrayList checkedStores;

ArrayList CreateCheckedStoresArray()
{
ArrayList result = new ArrayList();
foreach (GridViewRow row in grvStores.Rows)
{
result.Add(false);
}
return result;
}

protected void SetCheckedStores(object sender, EventArgs e)
{
int Ndx = 0;
checkedStores = (ArrayList)ViewState["checkedStoresArray"];

if (checkedStores != null)
{
foreach (GridViewRow row in grvStores.Rows)
{
CheckBox ckbx = row.FindControl("chbStore") as CheckBox;
ckbx.Checked = (bool)checkedStores[Ndx];
Ndx++;
}
}
}

protected void GetCheckedStores(object sender, EventArgs e)
{
int Ndx = 0;
checkedStores = (ArrayList)ViewState["checkedStoresArray"];

if (checkedStores != null)
{
foreach (GridViewRow row in grvStores.Rows)
{
CheckBox ckbx = row.FindControl("chbStore") as CheckBox;
checkedStores[Ndx] = ckbx.Checked;
Ndx++;
}
}
}

protected void Page_PreInit(Object sender, EventArgs e)
{
Page.Theme = Master.Theme;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
checkedStores = CreateCheckedStoresArray();
ViewState.Add("checkedStoresArray", checkedStores);
ReportsPanelVisible();
}
}
*************************************************************************

I was hoping that the "OnLoad" and "OnUnload" attributes of the update panel would execute my code-behind to store/retrieve which checkboxes were checked over an async postback, but this did not work.

I'm not married to this approach, but I do need a clue how to get this done.

Thanks for your help.

To avoid extra effort can you please provide the db related script.


Hi,

If you mean the detailed data source code, data access is done via a Data Access Layer and custom data access objects. I can only post that the DataSourceID="dsStores" is

<mtx:WSDataSource runat="server" ID="dsStores" DataGroup="Report" SubDataGroup="Related">
<SelectParameters>
<asp:ControlParameter ControlID="txbFromDate" Name="DateFrom" PropertyName="Text" />
<asp:ControlParameter ControlID="txbToDate" Name="DateTo" PropertyName="Text" />
</SelectParameters>
</mtx:WSDataSource>

As you can see, this is a custom/propriatary data access. The data returned from this source is quite correct, and the checkbox values are not retrieved from the data base.


Hi,

Based on my understanding, you creat CheckedStoresArray when the gridview is first binded, and then store it in viewstate,when the page is postback,you GetCheckedStores and store them in the Array in viewstate, then update/rebind/change your gridview,then SetCheckedStores according to the value in the array in viewstate.

I think your logic is messed up, just do GetCheckedStores before the gridview rebinding and do SetCheckedStores after the gridview rebinding.

Best Regards