我有一个非常简单的Telerik RadGrid我没有使用自动生成列。我正在使用gridbound列并创建自己的列。我将编辑模式设置为Inplace。这是RAdGid的ASPX
<div id="Grid">
<telerik:RadGrid RenderMode="Lightweight" runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowPaging="true"
OnNeedDataSource="UserGrid_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
OnItemCreated="RadGrid1_ItemCreated" OnDeleteCommand="RadGrid1_DeleteCommand"
OnInsertCommand="RadGrid1_InsertCommand">
<MasterTableView DataKeyNames="UserID" CommandItemDisplay="Top" EditMode="InPlace">
<Columns>
<telerik:GridEditCommandColumn />
<telerik:GridBoundColumn DataField="UserID" HeaderText="User ID" ReadOnly="true"
ForceExtractValue="Always" ConvertEmptyStringToNull="true" />
<telerik:GridCheckBoxColumn DataField="Active" HeaderText="Active" SortExpression="Active" UniqueName="chkActive"></telerik:GridCheckBoxColumn>
<telerik:GridTemplateColumn HeaderText="Role">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Role")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlRoles">
<asp:ListItem Text ="Admin" Value ="Administrator"></asp:ListItem>
<asp:ListItem Text ="User" Value ="User"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" UniqueName="FirstName" />
<telerik:GridButtonColumn ConfirmText="Delete this User?" ConfirmDialogType="RadWindow"
ConfirmTitle="Delete" ButtonType="FontIconButton" CommandName="Delete" />
</Columns>
<EditFormSettings InsertCaption="Add new item" CaptionFormatString="Edit User: {0}" CaptionDataField="FirstName">
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
<PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>
</div>
我删除了几个列以缩短代码示例。下面是一个缩短的e C#代码
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
string strUserID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["UserID"].ToString();
int intUserId = Convert.ToUInt16(strUserID);
using (ExpungeEntities db = new ExpungeEntities())
{
var Results = db.USERS_T_DATA.SingleOrDefault(i => i.UserID == intUserId);
if (Results == null)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to locate that user for updating"));
e.Canceled = true;
return;
}
Results.Role = (userControl.FindControl("ddlRoles") as DropDownList).SelectedValue;
Results.FirstName = (userControl.FindControl("FirstName") as TextBox).Text;
}
}
当我运行代码时,我收到以下错误消息:{“对象引用未设置为对象的实例。”}
看起来我没有得到控件的引用。谁能告诉我这里发生了什么?我使用此方法使用USerControl从RadGrid获取值来编辑或添加记录,这是我第一次尝试使用Inplace编辑。
我能够访问控件,但不得不删除Telerik GridBoundColumn,而是使用GridTemplateColumn,ItemTemplat和EidtItemTemplate。在EditdItemTemplate中,我使用了一个asp文本框。
我仍有问题,当我点击编辑按钮时会出现弹出窗体,但不会填充任何数据。下面是我的RadGrid行的示例。
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
我使用这个C#代码访问控件:
DropDownList ddl = (DropDownList)editableItem.FindControl("ddlRole") as
DropDownList;
d.Role = ddl.SelectedValue;
CheckBox CheckBox1 = editableItem.FindControl("chkActive") as
CheckBox;
d.Active = CheckBox1.Checked;
d.FirstName = (editableItem.FindControl("txtFirstName") as
TextBox).Text.Trim();
答案 0 :(得分:0)
根据我使用InPlace编辑的经验,您不需要任何UserControl来访问您的编辑控件。您可以直接从GridEditableItem访问它们。尝试访问您的控件,如下所示:
var ddlRoles = editedItem.FindControl("ddlRoles") as DropDownList;
if(ddlRoles != null)
{
// use your drop down list
}
我倾向于使用内联临时变量进行安全投射。以下是我将如何重写代码(如果您使用C#7)。
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem editedItem)
{
var strUserID = Convert.ToString(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["UserID"]);
var intUserId = Convert.ToUInt16(strUserID);
using (ExpungeEntities db = new ExpungeEntities())
{
var Results = db.USERS_T_DATA.SingleOrDefault(i => i.UserID == intUserId);
if (Results == null)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to locate that user for updating"));
e.Canceled = true;
return;
}
if (editedItem.FindControl("ddlRoles") is DropDownList ddlRoles)
Results.Role = ddlRoles.SelectedValue;
if (editedItem.FindControl("FirstName") is TextBox txtFirstName)
Results.FirstName = txtFirstName.Text;
}
}
}
答案 1 :(得分:0)
我能够解决我的问题请看看原帖。我添加了解释以及用于解决问题的asp和C#代码。