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

I need to show a textbox depending on a condition in a grid ?

4 Answers 503 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neeraj
Top achievements
Rank 1
Neeraj asked on 27 May 2009, 08:17 PM
I have a radgrid. Within the radgrid in Edit or AddMode I have a radcombobox and amount field which are bound to a datasource. Depending on certain values in the radcombobox that the user selects in add and edit mode  - I need to show one extra textbox  within the popup box so that users can enter % numbers. Once the user enters the % number,  the amount needs to be automatically calculated  -  can someone please provide an example for this ?

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 May 2009, 05:07 AM
Hello Neeraj,

You can try out the following sample code to achieve the required scenario. In the example below, I use a FormTemplate with a RadComboBox, a hidden TextBox(Percentage) and another TextBox(Amount). On selecting a particular value from the combobox, the hidden textbox is made visible and on text change of the percentage textbox the amount textbox is populated:
aspx:
<EditFormSettings EditFormType="Template" > 
      <FormTemplate> 
            <telerik:RadComboBox ID="RadComboBox1" AutoPostBack="true" DataSourceID="SqlDataSource1" DataTextField="Text" runat="server" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged"
            </telerik:RadComboBox> 
            <asp:TextBox ID="percentagetxt" AutoPostBack="true" runat="server" Visible="false" OnTextChanged="percentagetxt_TextChanged"></asp:TextBox> 
            <asp:TextBox ID="amttxt" runat="server"></asp:TextBox> 
       </FormTemplate>   
</EditFormSettings> 

c#:
 protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e) 
    { 
        RadComboBox combo = (RadComboBox)o; 
        GridEditFormItem item = (GridEditFormItem)combo.NamingContainer; 
        if (combo.SelectedItem.Text == "5"
        { 
            TextBox txt = (TextBox)item.FindControl("percentagetxt"); 
            txt.Visible = true;             
        } 
    }     
    protected void percentagetxt_TextChanged(object sender, EventArgs e) 
    { 
        TextBox percentagetxt = (TextBox)sender; 
        GridEditFormItem item = (GridEditFormItem)percentagetxt.NamingContainer; 
        TextBox txt = (TextBox)item.FindControl("amttxt"); 
        txt.Text = percentagetxt.Text;// perform calculation here 
    } 

Thanks
Princy.
0
Neeraj
Top achievements
Rank 1
answered on 28 May 2009, 02:52 PM

I do not have a editform template but I am using the below code and I am getting null exception ?

TextBox t = (TextBox)gridInvLines.FindControl("txtpercentage");

how would I access this textbox ? below is my aspx code: 

 

  <telerik:RadGrid ID="gridInvLines" AllowPaging="False" Skin="Office2007"
        runat="server" AutoGenerateColumns="False" DataSourceID="odsInvLines"
        GridLines="None" OnItemCommand="gridInvLines_ItemCommand" OnItemDataBound="gridInvLines_ItemDataBound" ShowFooter="True">
        <ClientSettings >
         <ClientEvents OnKeyPress="disableEnterKey" OnPopUpShowing="PopUpShowing" />
        </ClientSettings>
        <MasterTableView DataKeyNames="LINE_NUMBER" DataSourceID="odsInvLines" EditMode="PopUp" InsertItemDisplay="Top"  AllowAutomaticInserts="true" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true"  CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Line">
            <Columns>
         
            
              <telerik:GridTemplateColumn HeaderText="Activity Code" UniqueName="tcActivityCode">
              <HeaderStyle width="60px" />
              <ItemStyle  HorizontalAlign="center"   width="60px" />
              <ItemTemplate>
                      <asp:Label ID="lblActivityCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ACTIVITY_CODE_DESC") %>'>
                      </asp:Label>
              </ItemTemplate>
             
              <EditItemTemplate>
            
              <telerik:RadComboBox ID="rcbActivityCodes"  runat="server" Height="200px" Width="180px"
                AllowCustomText="False" ShowToggleImage="True" ShowMoreResultsBox="false"   
                    EnableLoadOnDemand="True" DropDownWidth="650px" DataTextField="DESCRIPTION"  DataSource='<%# LoadActivityCodes() %>'
                      MarkFirstMatch="True" SelectedValue='<%# Bind("REV_ACTIVITY_CODE_ID")%>' DataValueField="REV_ACTIVITY_CODE_ID" 
                      Text='<%# DataBinder.Eval(Container.DataItem, "ACTIVITY_CODE_DESC") %>'  AppendDataBoundItems="true" OnClientItemsRequesting="" AutoPostBack="true"  Skin="Office2007"
                       OnSelectedIndexChanged="rcbActivityCodes_SelectedIndexChanged">
                  
                   <HeaderTemplate>
                        <table style="width: 650px" cellspacing="0" cellpadding="0">
                            <tr>
                                <td style="width: 200px;">
                                    Description</td>
                                <td style="width: 200px;">
                                    Account</td>
                                <td style="width: 250px;">
                                    Category</td>
                            </tr>
                        </table>
                    </HeaderTemplate>
               
                    <ItemTemplate>
                        <table style="width: 650px" cellspacing="0" cellpadding="0">
                            <tr>
                                <td style="width: 200px;">
                                    <%# DataBinder.Eval(Container.DataItem,"DESCRIPTION")%>
                                </td>
                                <td style="width: 200px;">
                                    <%# DataBinder.Eval(Container.DataItem, "ACCOUNT")%>
                                    - <%# DataBinder.Eval(Container.DataItem, "ACCOUNT_DESCRIPTION")%>
                                </td>
                                <td style="width: 250px;">
                                    <%# DataBinder.Eval(Container.DataItem,"CATEGORY")%>
                                    -
                                    <%# DataBinder.Eval(Container.DataItem,"CATEGORY_DESCRIPTION")%>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:RadComboBox>
                </EditItemTemplate>
                </telerik:GridTemplateColumn>
               
                  <telerik:GridBoundColumn DataField="TOTAL_AMOUNT" HeaderText="Total Amount"  DataFormatString="{0:c}" ReadOnly="true" ForceExtractValue="None">
                   <HeaderStyle Width="50px" />
                  <ItemStyle HorizontalAlign="Right" Width="50px" />
                 </telerik:GridBoundColumn>
           
               
                <telerik:GridTemplateColumn Visible="false" UniqueName="percentage">
                <EditItemTemplate >
                <asp:TextBox ID="txtpercentage" runat="server" Visible="false"  />
                </EditItemTemplate>
                </telerik:GridTemplateColumn>
               
                
               
            </Columns>
      
       <EditFormSettings EditColumn-UpdateText="Save" EditColumn-InsertText="Save" CaptionFormatString="Invoice Line Information" ColumnNumber="2">
                   <FormTableItemStyle Wrap="False" Width="100%"></FormTableItemStyle>
                    <FormCaptionStyle CssClass="EditFormHeader" Width="100%"></FormCaptionStyle>
                    <FormMainTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="3" BackColor="White"
                        Width="100%" />
                    <FormTableStyle CellSpacing="0" CellPadding="2" CssClass="module" Height="110px"
                        BackColor="White" />
                    <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>

       <PopUpSettings  Modal="true" Width="600"  />
       </EditFormSettings>
        </MasterTableView>
        <ValidationSettings CommandsToValidate="PerformInsert,Update" />
      
    </telerik:RadGrid>
     

 

0
Neeraj
Top achievements
Rank 1
answered on 28 May 2009, 08:23 PM
actually I got it working, I wrapped the textboxes and the radcombox in placeholders and it did the trick. But now I have one problem, the post back onto the server and back is very slow. Its kind of irritating. Can I convert this code to clientside to javascript?   If so can you please post how to do it for the below code?


protected

 

void rcbActivityCodes_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)

 

{

 

RadComboBox combo = (RadComboBox)o;

 

 

PlaceHolder ph = combo.Parent as PlaceHolder;

 

 

 

GridEditableItem editItem = (GridEditableItem)ph.NamingContainer;

 

 

RadNumericTextBox t = (RadNumericTextBox)editItem.FindControl("rntPercentage");

 

 

Label l = (Label)editItem.FindControl("lblPercentage");

 

 

 

if (combo.SelectedItem.Attributes["ACCOM_SALE_FLAG"].ToString().ToUpper() == ConfigurationManager.AppSettings["c_TRUE"].ToString())

 

{

t.Visible =

true;

 

l.Visible =

true;

 

}

 

else

 

{

t.Visible =

false;

 

l.Visible =

false;

 

}

 

 

}

 

protected void rntPercentage_TextChanged(object sender, EventArgs e)

 

{

 

RadNumericTextBox percent = (RadNumericTextBox)sender;

 

 

PlaceHolder ph = percent.Parent as PlaceHolder;

 

 

GridEditableItem editItem = (GridEditableItem)ph.NamingContainer;

 

 

RadNumericTextBox amt = (RadNumericTextBox)editItem.FindControl("rntUnitPrice");

 

amt.Value = percent.Value;

// perform calculation here

 

 

}

0
Neeraj
Top achievements
Rank 1
answered on 29 May 2009, 03:52 PM
I could get so far in the below code.......I need these two statements to be converted :

I cannot get the parent and NamingContainer in javascript ? how would I do them ?

PlaceHolder ph = combo.Parent as PlaceHolder;
GridEditableItem editItem = (GridEditableItem)ph.NamingContainer; ?
       
 function ShowPercentage(sender, eventArgs)
                {
                var sender = sender;
                var item = eventArgs.get_item();
                var att  = item.get_attributes().getAttribute("ACCOM_SALE_FLAG");
              }
Tags
Grid
Asked by
Neeraj
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Neeraj
Top achievements
Rank 1
Share this question
or