This is a migrated thread and some comments may be shown as answers.

checkbox in commanditem template

4 Answers 179 Views
Grid
This is a migrated thread and some comments may be shown as answers.
MBEN
Top achievements
Rank 2
Veteran
MBEN asked on 02 Jun 2014, 11:39 PM
I have a checkbox in commanitem template.
I display the checkbox based on some values in the database and change the text of the checkbox accordingly.

On the first page load, my checkbox does not retain the values i set in the prerender event. After i do some postback (via the button in the itemtemplate) it displays the checkbox correctly. I set the grid in edit mode in my pre render event.

below is my code:
        protected void rgFunds_PreRender(object source, EventArgs e)
        {
            Button btnPostTransaction = (Button)rgFunds.MasterTableView.GetItems(GridItemType.CommandItem)[0].FindControl("btnPostTransaction");
            CheckBox chkAutoRebalance = (CheckBox)rgFunds.MasterTableView.GetItems(GridItemType.CommandItem)[0].FindControl("chkAutoRebalance");
 
            //Puts the grid in editmode
            if (!IsPostBack)
            {
                foreach (GridItem item in rgFunds.MasterTableView.Items)
                {
                    if (item is GridEditableItem)
                    {
                        GridEditableItem editableItem = item as GridDataItem;
                        editableItem.Edit = true;
                    }
                }
                rgFunds.Rebind();
            }
 
            //Set the AutoRebalance flag
            foreach (GridDataItem item in rgFunds.Items)
            {
                HtmlInputHidden _fundID = (HtmlInputHidden)item.FindControl("_fundID");
                int OnOff = Int32.Parse(_fundID.Value);
                chkAutoRebalance.Checked = OnOff == 1 ? true : false;
            }
 
            // Get the AutoRebalancing flag from plan settings
            if (ShowPostTransactionButton && IsCurrentEmployee && _allocationType == FundAllocation.AllocationType.Rebalance)
            {
                DataTable dtAutoRebalanceFrequency = FundRebalance.GetAutoRebalanceFrequencyByPlan(base.PlanID, DateTime.Now);
 
                if (dtAutoRebalanceFrequency.Rows.Count > 0)
                {
                    AutoRebalanceFrequency = dtAutoRebalanceFrequency.Rows[0][FundRebalance.Fields.RebalanceFrequency].ToString();
 
                    if (dtAutoRebalanceFrequency.Rows[0][FundRebalance.Fields.RebalanceFrequencyID].ToString() == ((int)FundRebalance.AutoRebalanceFrequency.NotAllowed).ToString())
                    {
                        chkAutoRebalance.Visible = false;
                        chkAutoRebalance.Checked = false;
                    }
                    else
                    {
                        chkAutoRebalance.Visible = true;
                        chkAutoRebalance.Text = "Auto Rebalance (" + AutoRebalanceFrequency + ")";
                    }
                }
                else
                {
                    chkAutoRebalance.Visible = false;
                    chkAutoRebalance.Checked = false;
                }
            }           
        }
 
Grid declaration:
 
<telerik:RadGrid ID="rgFunds" runat="server" AllowSorting="False" EnableViewState="false"
                        GridLines="Both" AllowMultiRowEdit="true" OnPreRender="rgFunds_PreRender" OnItemDataBound="rgFunds_ItemDataBound"
                        OnItemCommand="rgFunds_ItemCommand" OnNeedDataSource="rgFunds_NeedDataSource">
                        <MasterTableView TableLayout="Fixed" HierarchyDefaultExpanded="true" CommandItemDisplay="Bottom"
                            EditMode="InPlace" EnableNoRecordsTemplate="true">
                            <CommandItemTemplate>
                                <table width="100%">
                                    <tr>
                                        <td colspan="4" align="left">
                                            <asp:CheckBox ID="chkAutoRebalance" runat="server" Visible="true" ForeColor="White"
                                                OnCheckedChanged="chkAutoRebalance_CheckedChanged" />
                                        </td>
                                        <td align="right">
                                            <asp:Button ID="btnPostTransaction" runat="server" Text="Post Transaction" CssClass="button"
                                                CommandName="PostTransaction" />
                                        </td>
                                    </tr>
                                </table>
                            </CommandItemTemplate>
                            <Columns>
                                <telerik:GridTemplateColumn ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"
                                    FooterStyle-HorizontalAlign="Left" UniqueName="InvestmentOptions" ReadOnly="true"
                                    HeaderText="Investment Options">
                                    <ItemTemplate>
                                        <asp:Label ID="_fundName" runat="server" Text='<%# Eval("fundName")%>'></asp:Label>
                                        <asp:CheckBox ID="_excludeFund" runat="server" Checked='<%# Eval("allowRebalanceExclude")%>'
                                            Text="Exclude?" Visible='<%# Eval("allowRebalanceExclude")%>' />
                                        <input type="hidden" id="_fundID" runat="server" />
                                        <input type="hidden" id="_autoRebalanceFlag" runat="server" />
                                        <input type="hidden" id="_allowRebalance" runat="server" />
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <strong>Total must equal 100%</strong></FooterTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-HorizontalAlign="Right" HeaderText="Current %"
                                    HeaderStyle-Width="25%" ReadOnly="true" DataField="currentPercent" UniqueName="currentPercent">
                                    <ItemTemplate>
                                        <asp:Label ID="_currentPercent" runat="server" Text='<%# Eval("currentPercent", "{0:N2}%")%>'>
                                        </asp:Label></ItemTemplate>                                 
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-HorizontalAlign="Right" UniqueName="futurePercent"
                                    HeaderStyle-Width="25%" HeaderText="New %">
                                    <ItemTemplate>
                                        <asp:Label ID="_futurePercent" runat="server" Text='<%# Eval("futurePercent", "{0:N2}%")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtNewPercent" runat="server" CssClass="percent" groupName="allocations"
                                            Width="40px" onchange="SumGroup(this, false)" Text='<%# Eval("futurePercent")%>' />
                                        %
                                        <asp:RangeValidator ID="newPercentageRange" runat="server" ControlToValidate="txtNewPercent"
                                            Type="Integer" CssClass="errormessagesmall" Display="None" />
                                        <asp:CustomValidator ID="newPercentageIncrement" runat="server" ControlToValidate="txtNewPercent"
                                            ClientValidationFunction="Mod_ClientValidate" CssClass="errormessagesmall" Display="None" />
                                        <cc1:ValidatorCalloutExtender ID="newPercentageRangeCallout" runat="server" TargetControlID="newPercentageRange"
                                            Width="235px" />
                                        <cc1:ValidatorCalloutExtender ID="newPercentageIncrementCallout" runat="server" TargetControlID="newPercentageIncrement"
                                            Width="235px" />
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="fundTotal" runat="server" CssClass="percent" onfocus="blur()" Enabled="false"
                                            Font-Bold="true" ForeColor="Black" Width="40px" />
                                        %
                                        <asp:CompareValidator ID="totalEqual100" runat="server" ControlToValidate="fundTotal"
                                            ValueToCompare="100" Operator="Equal" CssClass="errormessagesmall" Display="None"
                                            ErrorMessage="New allocations must total 100%." />
                                        <asp:RequiredFieldValidator ID="fundTotalRequired" runat="server" ControlToValidate="fundTotal"
                                            Display="None" Visible="false" ErrorMessage="You <em>must</em> enter an allocation." />
                                        <cc1:ValidatorCalloutExtender ID="fundTotalRequiredCallout" runat="server" TargetControlID="fundTotalRequired"
                                            Width="235px" />
                                        <cc1:ValidatorCalloutExtender ID="totalEqual100Callout" runat="server" TargetControlID="totalEqual100"
                                            Width="235px" />
                                    </FooterTemplate>
                                </telerik:GridTemplateColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Jun 2014, 12:12 PM
Hi MBEN,

Please do the following modification in your code snippet which works fine at my end.

C#:
chkAutoRebalance.Checked =( OnOff == 0 )? true : false;

Thanks,
Shinu.
0
MBEN
Top achievements
Rank 2
Veteran
answered on 03 Jun 2014, 03:44 PM
Shinu,

The issue I am having is with the visibility of the checkbox in this part of the code.


I am setting the visibility of the checkbox and changing the text but that is not reflected on the first page load. It only shows up after a postback.

// Get the AutoRebalancing flag from plan settings
            if (ShowPostTransactionButton && IsCurrentEmployee && _allocationType == FundAllocation.AllocationType.Rebalance)
            {
                DataTable dtAutoRebalanceFrequency = FundRebalance.GetAutoRebalanceFrequencyByPlan(base.PlanID, DateTime.Now);

                if (dtAutoRebalanceFrequency.Rows.Count > 0)
                {
                    AutoRebalanceFrequency = dtAutoRebalanceFrequency.Rows[0][FundRebalance.Fields.RebalanceFrequency].ToString();

                    if (dtAutoRebalanceFrequency.Rows[0][FundRebalance.Fields.RebalanceFrequencyID].ToString() == ((int)FundRebalance.AutoRebalanceFrequency.NotAllowed).ToString())
                    {
                        chkAutoRebalance.Visible = false;
                        chkAutoRebalance.Checked = false;
                    }
                    else
                    {
                        chkAutoRebalance.Visible = true;
                        chkAutoRebalance.Text = "Auto Rebalance (" + AutoRebalanceFrequency + ")";
                    }
                }
                else
                {
                    chkAutoRebalance.Visible = false;
                    chkAutoRebalance.Checked = false;
                }
            }
0
MBEN
Top achievements
Rank 2
Veteran
answered on 03 Jun 2014, 10:08 PM
I call a rebind() in the pre render event to put the grid in edit mode.
I noticed that if I comment the rebind() then the grid reflects the changes to visibility and text.

What can i do ?
0
Shinu
Top achievements
Rank 2
answered on 04 Jun 2014, 05:34 AM
Hi,

You can use the ItemDataBound event of the RadGrid to bind values to controls in Grid.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridCommandItem)
  {
    GridCommandItem cmdItem = (GridCommandItem)e.Item;
    CheckBox chkAutoRebalance = (CheckBox)cmdItem.FindControl("chkAutoRebalance");
    //Rest of your code to set the checkbox value
  }
}

Thanks,
Shinu
Tags
Grid
Asked by
MBEN
Top achievements
Rank 2
Veteran
Answers by
Shinu
Top achievements
Rank 2
MBEN
Top achievements
Rank 2
Veteran
Share this question
or