我使用RadGrid控件,并且我创建了一组命令按钮,作为插入,更新,保存,删除。当我点击插入按钮时,RadGrid可以进入插入模式,但是当我输入数据后点击保存按钮时,e.item总是为空?如何在editmode / insermode中获取项目的Radgrid值?
这是部分代码: ASP:
<telerik:RadGrid ID="RadGridLandBlock" runat="server" Culture="zh-CN" GroupPanelPosition="Top" AutoGenerateColumns="false"
Width="1000px" Height="500px" Skin="Metro"
OnNeedDataSource="RadGridLandBlock_NeedDataSource"
OnItemCommand ="RadGridLandBlock_ItemCommand"
OnItemCreated ="RadGridLandBlock_ItemCreated"
OnInsertCommand="RadGridLandBlock_InsertCommand"
>
<ClientSettings>
<Selecting AllowRowSelect="True" />
<Scrolling AllowScroll="true" />
<Resizing AllowColumnResize="true" ResizeGridOnColumnResize="true" AllowResizeToFit="true" />
</ClientSettings>
<MasterTableView ClientDataKeyNames="PK_Land" DataKeyNames="PK_Land" EditMode="InPlace" CommandItemDisplay="Top" >
<NoRecordsTemplate>
No Record
</NoRecordsTemplate>
<CommandItemSettings ShowRefreshButton="false" />
<CommandItemTemplate >
<div id="RadGridCommand" style="padding: 10px 10px;" >
<telerik:RadButton ID="InsertBtn" CommandName="InsertBtn" CommandArgument ='<%#Eval ("PK_Land") %>' runat="server" Skin="Metro" Text="插入" Enabled='<%#! RadGridLandBlock.MasterTableView.IsItemInserted %>' ></telerik:RadButton>
<telerik:RadButton ID="DeleteBtn" CommandName="DeleteBtn" runat="server" Skin="Metro" Text ="Delete" Enabled='<%#! RadGridLandBlock.MasterTableView.IsItemInserted %>' OnClientClicked="confirmDelete" ></telerik:RadButton>
<telerik:RadButton ID="EditBtn" CommandName="EditBtn" runat="server" Skin="Metro" Text="Edit" Enabled='<%# RadGridLandBlock.EditIndexes.Count == 0 %>'></telerik:RadButton>
<telerik:RadButton ID="SaveBtn" CommandName="SaveBtn" runat="server" Skin="Metro" Text="Save" Enabled='<%# RadGridLandBlock.MasterTableView.IsItemInserted %>' ></telerik:RadButton>
<telerik:RadButton ID="CancelBtn" CommandName="CancelBtn" runat="server" Skin="Metro" Text="Cancel" Enabled='<%# RadGridLandBlock.EditIndexes.Count > 0 ||RadGridLandBlock.MasterTableView.IsItemInserted %>' ></telerik:RadButton>
</div>
</CommandItemTemplate>
<Columns>
<telerik:GridBoundColumn DataField="PK_Land" DataType="System.Guid" HeaderText="PK_Land" ReadOnly="True" SortExpression="PK_Land" UniqueName="PK_Land">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PK_BookID" DataType="System.Int32" HeaderText="PK_BookID" ReadOnly="True" SortExpression="PK_BookID" UniqueName="PK_BookID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LandBlockName" DataType="System.Char" HeaderText="LandName" ReadOnly="false" SortExpression="LandBlockName" UniqueName="LandBlockName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="CapacityRate" DataType="System.Char" HeaderText="Capacity" ReadOnly="false" SortExpression="CapacityRate" UniqueName="CapacityRate">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PK_period" DataType="System.Int32" HeaderText="PK_period" ReadOnly="false" SortExpression="PK_period" UniqueName="PK_period">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PK_useridLastEditor" DataType="System.Int32" HeaderText="PK_useridLastEditor" ReadOnly="false" SortExpression="PK_useridLastEditor" UniqueName="PK_useridLastEditor">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
C#
protected void RadGridLandBlock_ItemCommand(object sender, GridCommandEventArgs e)
{
//insert btn clicked
if (e.CommandName == "InsertBtn")
{
LabelTest.Text = "insert btn from itemcommand is fired";
RadGridLandBlock.MasterTableView.IsItemInserted = true;
RadGridLandBlock.Rebind();
}
//Save btn clicked
if (e.CommandName=="SaveBtn")
{
LabelTest.Text = "SaveBtn from itemcommand is fired";
myCnn = new SqlConnection(myCnnstring);
myCnn.Open();
mysql = "insert into ld_landblock (PK_bookID,landblockname,capacityrate,pk_period,pk_userIDlasteditor) " +
" values(@pk_bookid, @landblockname,@capacityrate,@pk_period,@pk_userIDlasteditor)";
mycmd = new SqlCommand(mysql, myCnn);
GridEditFormItem editItem = e.Item as GridEditFormItem;
mycmd.Parameters.AddWithValue("@pk_bookid", Convert.ToInt32((editItem.FindControl("PK_BookID") as TextBox).Text));//HERE IS THE ERROR OCCURED)
mycmd.Parameters.AddWithValue.......
}
}
protected void RadGridLandBlock_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
// RadGridLandBlock.MasterTableView.IsItemInserted = false;
PK_bookID = Convert.ToInt32(ViewState["PK_BookID"]);
PK_period = Convert.ToInt32(ViewState["PK_Period"]);
(sender as RadGrid).DataSource = GetGridDatatable();
}
protected void RadGridLandBlock_ItemCreated(object sender, GridItemEventArgs e)
{
}
提前致谢。
答案 0 :(得分:0)
这可能对你有所帮助...... 根据代码中的注释进行描述......
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
for (int i = 0; i < 10; i++ )
dt.Rows.Add("1 " + i);
ViewState["Da"] = dt;
rg.DataSource = dt;
rg.DataBind();
}
}
protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
{
// Check
if (e.CommandName == "SaveTrigger")
{
// Variable
string value = string.Empty;
TextBox txt = null;
// Check
if (e.Item is GridDataInsertItem)
{
// Set Init Insert Items
GridDataInsertItem item = e.Item as GridDataInsertItem;
// Get Textbox from Insert New Item
txt = item.FindControl("txt") as TextBox;
}
else
{
// This will get the first line of the Edit Item
GridEditableItem editItem = rg.EditItems[0] as GridEditableItem;
// Or
// This will get be using Index Number through the button which you press save
// GridDataItem editItem = rg.Items[e.Item.ItemIndex] as GridDataItem;
txt = editItem.FindControl("txt") as TextBox;
}
Response.Write(txt.Text.Trim());
}
}
protected void rg_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
rg.DataSource = ViewState["Da"] as DataTable;
}