HTML code:
<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
DropDownList
RadComboBox SelectedIndexChanged
事件,此RadComboBox位于RadGrid之外。
要求2:想要使用警报消息禁用RadGrid的“添加新”按钮,如果在单击“添加新”按钮之前未选择RadComboBox(在RadGrid之外)项目。< / p>
答案 0 :(得分:0)
下面的代码解决了这两个要求:
//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);
}
}
}