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

Disable Add New button of RadGrid when no data is selected from RadCombox (which is outside of RadGrid)

3 Answers 472 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Priyanka
Top achievements
Rank 1
Priyanka asked on 09 Jul 2015, 08:15 AM

There is a RadComboBox (outside of RadGrid) and a RadGrid in my Web page.

When first time page loads, and user forgets to Select Item from RadComboBox (which is outside of RadGrid) and clicks on "Add New" button of RadGrid then this button should disable at that time and an alert should come ('Select item from RadComboBox first')

Now, when user select the item from RadComboBox, and then click on "Add New" button of RadGrid then it should perform "add" functionality //---this part is done

Below code is working fine only to disable the "Add New" button and show alert.

GridCommandItem cmditem = (GridCommandItem)RGGSTAcCode.MasterTableView.GetItems(GridItemType.CommandItem)[0];
            System.Web.UI.WebControls.Button ctrl = (System.Web.UI.WebControls.Button)cmditem.FindControl("AddNewRecordButton");
            ctrl.Enabled = false;
            ctrl.Attributes.Add("onClick", "test()");
 
            System.Web.UI.WebControls.LinkButton btn = (System.Web.UI.WebControls.LinkButton)cmditem.FindControl("InitInsertButton");
            btn.Enabled = false;
            btn.Attributes.Add("onClick", "test()");

But how to create this whole requirement using above code? Where to put this line of code, in which event, so that it should work as per need. 

Below is the whole HTML code I am using currently:

<telerik:RadComboBox ID="ddlCompany" runat="server" Height="200" Width="240"
          DropDownWidth="310" EmptyMessage="- Select Product -" HighlightTemplatedItems="true" CausesValidation="false"
          Filter="StartsWith" AppendDataBoundItems="true" AllowCustomText="true" AutoPostBack="true"
          DataTextField="Title" DataValueField="Code" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
        </telerik:RadComboBox>
 
        <br />
        <br />
 
                <telerik:RadGrid ID="RGGSTAcCode" runat="server"
                   ShowFooter="True" GroupingEnabled="False" ShowStatusBar="true" EmptyDataText="No record available."
                   AllowAutomaticInserts="False" AllowAutomaticUpdates="False" AllowAutomaticDeletes="true"
                   OnNeedDataSource="RGGSTAcCode_NeedDataSource" OnItemDataBound="RGGSTAcCode_ItemDataBound"
                   OnInsertCommand="RGGSTAcCode_InsertCommand" OnDeleteCommand="RGGSTAcCode_DeleteCommand"
                   OnUpdateCommand="RGGSTAcCode_UpdateCommand">
 
                  <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
                  <mastertableview ShowHeadersWhenNoRecords="true" autogeneratecolumns="false" datakeynames="AccountCodeID" InsertItemDisplay="Top"
                    insertitempageindexaction="ShowItemOnCurrentPage" ShowFooter="True" CommandItemDisplay="Top">                                  
                         <Columns>
                             <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
                                                           
                             <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
                                <ItemTemplate>
                                  <asp:Label ID="lblAcCode" Text='<%# Eval("AccountCode") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                  <asp:DropDownList ID="ddlAcCode" DataTextField="AccountDescription" DataValueField="AccountCodeID" runat="server"/>
                                </EditItemTemplate>
                             </telerik:GridTemplateColumn>
 
                             <telerik:GridBoundColumn DataField="AccountDescription" HeaderText="Description" UniqueName="AccountDescription" SortExpression="AccountDescription" InsertVisiblityMode="AlwaysHidden" ReadOnly="true" ></telerik:GridBoundColumn>
                             <telerik:GridBoundColumn aggregate="SUM" DataField="Amount" HeaderText="Amount" FooterText="Total : " UniqueName="Amount" SortExpression="Amount"></telerik:GridBoundColumn>
                             <telerik:GridBoundColumn DataField="Remark" HeaderText="IFCA Remark" UniqueName="Remark" SortExpression="Remark">
                                  
                             </telerik:GridBoundColumn>   
                              
                             <telerik:GridButtonColumn ConfirmTextFormatString="Are you sure you want to Delete {0} Account Code?" ConfirmTextFields="AccountCodeID"
                             ConfirmDialogType="RadWindow" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"></telerik:GridButtonColumn>                                                                           
                      </Columns>
                      <EditFormSettings>
                       <EditColumn ButtonType="ImageButton" />
                      </EditFormSettings>
                      <CommandItemSettings AddNewRecordText="Add new record" RefreshText="Refresh"></CommandItemSettings>
                  </mastertableview>
                </telerik:RadGrid>

Please guide.

Please note that I am very new in Telerik controls so if I ask something very basic please forgive and try to guide me in a simple way.

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 14 Jul 2015, 07:04 AM
Hi Priyanka,

You can achieve this requirement using the following approach:
<ClientSettings>
    <ClientEvents OnCommand="gridCommand" />
</ClientSettings>
JavaScript:
function gridCommand(sender, args) {
    if (args.get_commandName() == "InitInsert") {
        var combo = $find('<%= ddlCompany.ClientID %>');
        if (combo.get_text() == combo.get_emptyMessage()) {
            args.set_cancel(true);
        }
    }
}

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Priyanka
Top achievements
Rank 1
answered on 14 Jul 2015, 09:15 AM

@Eyup Thank you for the reply 

Please let me know where to place the below code ?

Please note that I am very new in Telerik so If my questions are senseless please try to guide me in right way.

My Requirement is: I have a RadComboBox outside the RadGrid and asp DropDownList inside RadGrid.

When user select any item from RadComboBox, based on the Selected Item, asp DropDownList is bind.

Example: If RadComBox item = Company, then all the company names will be bind inside asp DropDownList.

Else if user do not select or missed to select item from RadComboBox, and try to click on "Add New" button of RadGrid, then "Add New" button should be disable at that time and an alert should appear "Please select item from RadComboBox.

For my above requirement I tried below code i..e, at RadComBox SelectedIndexChanged event and RadGrid's ItemDataBound event. But I am having below issues:

1) Inside RadGrid's ItemDataBound event, When while Edit, I try to fetch the SelectedValue of DropDownList for particular row/Id, I always get "- Select -" not the SelectedValue of DropDown from the database for that particular Id.

2) Inside RadComBox SelectedIndexChange​d, I am unable to bind the DropDownList based on RadComboBox items.
Please review my code and let me know what is wrong in it. If mot code is not as per requirement then please provide the sample code based on requirement.

<telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
                                <ItemTemplate>
                                  <asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                   <asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>'></asp:Label>
                                  <asp:DropDownList ID="ddlAcCode" DataTextField="AccountDescription" DataValueField="AccountCodeID" runat="server"/>
                                </EditItemTemplate>
                             </telerik:GridTemplateColumn>

public DataTable GetAccCode(string CompanyCode)
    {
        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("[Invoice].[usp_tbl_AccountCode_DL_Test]", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@CompanyCode", CompanyCode);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        try
        {
            con.Open();
            da.Fill(dt);
            con.Close();
        }
        catch (Exception ex)
        {
        }
        return dt;
    }
 
    protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (ddlCompany.SelectedItem == null)
        {
            Response.Write("Please select Company first");
 
            GridCommandItem cmditem = (GridCommandItem)RGGSTAcCode.MasterTableView.GetItems(GridItemType.CommandItem)[0];
            System.Web.UI.WebControls.Button ctrl = (System.Web.UI.WebControls.Button)cmditem.FindControl("AddNewRecordButton");
            ctrl.Enabled = false;
 
            System.Web.UI.WebControls.LinkButton btn = (System.Web.UI.WebControls.LinkButton)cmditem.FindControl("InitInsertButton");
            btn.Enabled = false;
        }
        else
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //bind dropdwon while "Add"
                string CompanyCode = ddlCompany.SelectedValue.ToString();
                GridEditableItem item = (GridEditableItem)e.Item;
                DropDownList ddl = (DropDownList)item.FindControl("ddlAcCode");
                ddl.DataSource = GetAccCode(CompanyCode);
                ddl.DataTextField = "AccountDescription";
                ddl.DataValueField = "AccountCodeID";
                ddl.DataBind();
                ddl.Items.Insert(0, "- Select -");
 
                //Select particular dropdown value while "Edit"
                string accCodeID = item.GetDataKeyValue("AccountCodeID").ToString();
                Label lblAcCode2 = item.FindControl("lblAcCode2") as Label;
 
                if (!string.IsNullOrEmpty(lblAcCode2.Text))
                {
                    SqlConnection con = new SqlConnection(strcon);
                    con.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand("SELECT [AccountCode]+' - '+[AccountDescription] as [AccountDescription] FROM [Sunway_AP].[Invoice].[tbl_AccountCodeTest] where AccountCodeID='" + accCodeID + "' ", con);
 
                    DataTable dt = new DataTable();
                    try
                    {
                        adapter.Fill(dt);
                    }
                    finally
                    {
                        con.Close();
                    }
                    ddl.SelectedValue = dt.ToString();
                    string SelectedVal = ddl.SelectedValue;
                }
            }
        }
    }
 
protected void ddlCompany_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (ddlCompany.SelectedItem != null)
        {
           //bind asp DropDownList based on Selected RadComboBox item
        }
        else
        {
            Response.Write("Please select Company first");
 
            GridCommandItem cmditem = (GridCommandItem)RGGSTAcCode.MasterTableView.GetItems(GridItemType.CommandItem)[0];
            System.Web.UI.WebControls.Button ctrl = (System.Web.UI.WebControls.Button)cmditem.FindControl("AddNewRecordButton");
            ctrl.Enabled = false;
                    ctrl.Attributes.Add("onClick", "test()");
 
            System.Web.UI.WebControls.LinkButton btn = (System.Web.UI.WebControls.LinkButton)cmditem.FindControl("InitInsertButton");
            btn.Enabled = false;
                    ctrl.Attributes.Add("onClick", "test()");
        }
    }

0
Eyup
Telerik team
answered on 16 Jul 2015, 12:23 PM
Hello Priyanka,

You place the suggested code snippet in the aspx page:
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
    </Scripts>
</telerik:RadScriptManager>
<script type="text/javascript">
    //Put your JavaScript code here.
    function gridCommand(sender, args) {
        if (args.get_commandName() == "InitInsert") {
            var combo = $find('<%= ddlCompany.ClientID %>');
            if (combo.get_text() == combo.get_emptyMessage()) {
                args.set_cancel(true);
            }
        }
    }
</script>
 ...
<telerik:RadGrid ...>
    <ClientSettings>
        <ClientEvents OnCommand="gridCommand" />
    </ClientSettings>

If you have further difficulties, I can prepare a sample web site for your convenience.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Priyanka
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Priyanka
Top achievements
Rank 1
Share this question
or