将RadGrid`EditItemTemplate`DropDownList绑定到RadComboBox`Selecte

本文关键字:RadComboBox 绑定 Selecte DropDownList RadGrid EditItemTemplate | 更新日期: 2023-09-27 18:02:42


HTML代码:

<telerik:RadComboBox ID="ddlCompany" runat="server" AutoPostBack="true"
  DataTextField="Title" DataValueField="Code"     OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
</telerik:RadComboBox> 
<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")%>' Visible="false"></asp:Label>
     <asp:DropDownList ID="ddlAcCode" DataTextField="AccountDescription" DataValueField="AccountCodeID" runat="server"/> 
   </EditItemTemplate>
</telerik:GridTemplateColumn>

C#代码:

protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
    {
       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"
          Label lblAcCode2 = item.FindControl("lblAcCode2") as Label;
          if (lblAcCode2.Text != "- ")
          {
             ddl.SelectedItem.Text = lblAcCode2.Text;
             ddl.SelectedValue = lblAcCode2.Text;
          }
       }
    }
protected void ddlCompany_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    //have to bind the dropdown based on RedComboBox Item
}

需求1:我想在内部绑定RadGrid EditItemTemplate DropDownListRadComboBoxSelectedIndexChanged事件,此RadComboBox在RadGrid之外。

要求2:想要禁用带有警报消息的RadGrid的"添加新的"按钮,如果在单击"添加新"按钮之前未选择RadComboBox(位于RadGrid之外(项目。

将RadGrid`EditItemTemplate`DropDownList绑定到RadComboBox`Selecte

下面的代码解决了这两个要求:

//RedComboBox 'selectedIndexChanged' event
protected void CompanyChanged(object sender, EventArgs e)
{
    RadComboBox CompanyComboBox = fvIPRForm.FindControl("ddlCompany") as RadComboBox;
    if (CompanyComboBox.SelectedValue == null || CompanyComboBox.SelectedValue == "")
    {
        RadGrid grid = fvIPRForm.FindControl("RGGSTAcCode") as RadGrid;
        GridCommandItem cmditem = (GridCommandItem)grid.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;
        string content = "Please select company first";
        ScriptManager.RegisterStartupScript(this, typeof(string), "Successful", "alert('" + content + "');", true);
    }
    else
    {            
        //else code here
    }
}
//Disable Account Code functionality, If company is not selected
protected void RGGSTAcCode_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "InitInsert")
    {
        RadComboBox CompanyComboBox = fvIPRForm.FindControl("ddlCompany") as RadComboBox;
        if (CompanyComboBox.SelectedValue == null || CompanyComboBox.SelectedValue == "")
        {
            e.Canceled = true;
            RadGrid grid = fvIPRForm.FindControl("RGGSTAcCode") as RadGrid;
            GridCommandItem cmditem = (GridCommandItem)grid.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;
            string content = "Please select company first";
            ScriptManager.RegisterStartupScript(this, typeof(string), "Successful", "alert('" + content + "');", true);
        }
    }
}