【发布时间】:2016-09-12 14:05:40
【问题描述】:
我下面的代码只是为了显示带有“添加新课程”按钮的 radgrid 标题。最初在页面加载时,我没有要绑定到 radgrid 的数据。在“添加新课程”按钮上,单击我向 RadGrid 添加一个新行,其中包含 2 个文本框和 2 个下拉列表(从 db 绑定)。每行都应该有“删除行”按钮。我有这些工作。现在我的问题是当用户尝试向网格添加另一个新行时,我需要恢复用户输入的数据。还有怎么做我删除了该行?请根据您的建议帮助我,或突出显示我现有代码中的错误。提前致谢。
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" MasterTableView-CommandItemSettings-ShowAddNewRecordButton="false" MasterTableView-CommandItemSettings-ShowRefreshButton="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnItemDataBound="RadGrid1_ItemDataBound" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView Width="100%" HeaderStyle-Font-Bold="true" CommandItemStyle-Font-Bold="true" DataKeyNames="IsAdd,BusID" CommandItemDisplay="Top" CommandItemStyle-HorizontalAlign="Right">
<CommandItemTemplate>
<asp:Button ID="IsAdd" Font-Size="Small" Font-Bold="true" CommandName="InitInsert" Text ="Add New Bus" runat="server" />
</CommandItemTemplate>
<Columns>
<telerik:GridTemplateColumn UniqueName="BusID" HeaderText="Bus #" DataField="BusID">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Name" HeaderText="Bus Series" DataField="Name">
<ItemTemplate>
<asp:DropDownList ID="SeriesDropDown" DataTextField="SeriesName" runat="server" AutoPostBack="false"></asp:DropDownList>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Name" HeaderText="Bus Group" DataField="Name">
<ItemTemplate>
<asp:DropDownList ID="GroupDropDown" DataTextField="GroupName" runat="server" AutoPostBack="false"></asp:DropDownList>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Name" HeaderText="VIN" DataField="Name">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Remove Row" CommandName="Delete" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="savebtn" runat="server" Font-Bold="true" Text="Save Rows"/>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (!IsPostBack)
{
dt.Columns.Add("BusID");
dt.Columns.Add("Name");
dt.Columns.Add("IsAdd");
dt.Rows.Add(1, "Name1", false);
dt.Rows.Add(2, "Name2", false);
dt.Rows.Add(3, "Name3", false);
Session["dt"] = dt;
}
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = (DataTable)Session["dt"];
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
Button btn = new Button();
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
Button Button1 = item.FindControl("Button1") as Button;
DropDownList SeriesDropDown = item.FindControl("SeriesDropDown") as DropDownList;
DropDownList GroupDropDown = item.FindControl("GroupDropDown") as DropDownList;
TextBox TextBox4 = item.FindControl("TextBox4") as TextBox;
DataSet busGroup_Dataset = admin.GetBusGroup();
GroupDropDown.DataSource = busGroup_Dataset;
GroupDropDown.DataBind();
DataSet busSeries_Dataset = admin.GetBusSeries();
SeriesDropDown.DataSource = busSeries_Dataset;
SeriesDropDown.DataBind();
bool isAdd = Convert.ToBoolean(item.GetDataKeyValue("IsAdd"));
if (isAdd)
{
TextBox1.Visible = SeriesDropDown.Visible = GroupDropDown.Visible = TextBox4.Visible = true;
btn.Visible = true;
RadGrid1.DataSource = Session["dt"];
}
else
{
TextBox1.Visible = SeriesDropDown.Visible = GroupDropDown.Visible = TextBox4.Visible = false;
Button1.Visible = false;
}
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.InitInsertCommandName)
{
DataTable dt = (DataTable)Session["dt"];
dt.Rows.Add(0, string.Empty, true);
RadGrid1.MasterTableView.IsItemInserted = false;
e.Canceled = true;
RadGrid1.Rebind();
}
if (e.CommandName == "Delete")
{
string ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["0"].ToString();
DataTable table = (DataTable)Session["dt"];
if (table.Rows.Find(ID) != null)
{
table.Rows.Find(ID).Delete();
table.AcceptChanges();
Session["dt"] = table;
}
RadGrid1.Rebind();
}
}
【问题讨论】:
标签: asp.net telerik telerik-grid