我每行都有一个带有radcombobox的radgrid。我想在选择组合框后获取行的ID(有人在下拉列表中选择一个值)。我正在使用radgrid的onitemcreated属性来获取我在后面调用的方法。但是,我无法读取属于所选组合框所属行的ID的值。任何人都可以提供任何建议我已经尝试了所有Telerik的样本,但是我没有取得好成绩。
正如您在下面的代码中看到的,我在gridtemplate列中有一个radbutton,这可以按预期工作。当我在调试时,我得到了intID的值,这不是我从组合框中选择一个值后所期望的结果。
以下是我的所有代码:
HTML标记:
<telerik:RadGrid ID="rdg1"
runat="server"
ItemStyle-Wrap="false"
TabIndex="1000"
GridLines="Horizontal"
BorderColor="#738BA4"
BorderWidth="1px"
OnNeedDataSource="ds1"
OnItemDataBound="oidb1"
AutoGenerateColumns="False"
OnItemCommand = "oicommand1"
EnableLinqExpressions="true"
OnItemCreated="oicreated1">
<MasterTableView DataKeyNames="ID"
HorizontalAlign="Center"
HeaderStyle-BorderWidth="0"
ItemStyle-BorderWidth="2"
ItemStyle-BorderColor="#738BA4"
FooterStyle-BorderWidth="0"
BorderWidth="0">
<CommandItemSettings ExportToPdfText="Export to Pdf" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID"
ItemStyle-Font-Size="8"
UniqueName="ID"
Visible="false">
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="False" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn ItemStyle-Font-Size="8" HeaderText="Level" UniqueName="Level">
<ItemTemplate>
<telerik:RadComboBox ID="rdcb1" runat="server" AutoPostBack="true"></telerik:RadComboBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="False" />
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn ItemStyle-Font-Size="8"
DataField="Name"
UniqueName="Name"
Visible="true"
HeaderText="Name">
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="true" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl="/Images/Delete-Small.PNG" />
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<ItemStyle BorderColor="#738BA4" BorderWidth="2px" />
<AlternatingItemStyle />
<HeaderStyle BorderWidth="0px" />
<FooterStyle BorderWidth="0px" />
</MasterTableView>
<ItemStyle Wrap="False" />
<FilterMenu EnableImageSprites="False">
</FilterMenu>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
</HeaderContextMenu>
</telerik:RadGrid>
</asp:Panel>
代码背后:
protected void ds1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e) {
var Role = (from r in db.Role
select new { r.ID, r.Name });
rdg1.DataSource = Role;
Level = Role.Count();
_dataTableLevel.Clear();
_dataTableLevel.Columns.Add("Level");
for (int i = 1; i <= Level; i++) {
DataRow drLevel = _dataTableLevel.NewRow();
drLevel["Level"] = i;
_dataTableLevel.Rows.Add(drOrderLevel);
}
protected void oidb1(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
GridDataItem Item1 = (GridDataItem)e.Item;
(Item1.FindControl("rdcb1") as RadComboBox).DataValueField = "Level";
(Item1.FindControl("rdcb1") as RadComboBox).DataTextField = "Level";
(Item1.FindControl("rdcb1") as RadComboBox).DataSource = _dataTableLevel.DefaultView;
(Item1.FindControl("rdcb1") as RadComboBox).DataBind();
}
}
protected void oicommand1(object sender, GridCommandEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem Item2 = (GridDataItem)e.Item;
TableCell ID = Item2["ID"] as TableCell;
int intID = Convert.ToInt32(ID.Text);
var deleteRole = (from r in db.Role
where r.ID == intID).First();
db.Role.DeleteObject(deleteRole);
db.SaveChanges();
rdg1.Rebind();
}
}
protected void oicreated1(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem Item3 = (GridDataItem)e.Item;
TableCell ID = Item3["ID"] as TableCell;
string ID = ID.Text;
}
}
答案 0 :(得分:3)
Telerik的人提供了以下解决方案。 Telerik的解决方案有效。
感谢。
感谢您与我们联系。
如果你想在更改特定组合框的选定索引时获取行的ID,我的建议是订阅服务端OnSelectedIndexChanged
事件并使用以下事件处理函数的实现:
protected void OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combobox = sender as RadComboBox;
GridDataItem dataItem = combobox.Parent.Parent as GridDataItem;
var text = dataItem["ID"].Text;
}
我希望这会帮助你。
亲切的问候, Dimitar Terziev Telerik团队
答案 1 :(得分:0)
您的解决方案有效,但是如果有人没有在列中ID,则可以使用RadGrid的DataKeyName作为另一种方法
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combobox = sender as RadComboBox;
GridDataItem dataItem = combobox.Parent.Parent as GridDataItem;
string id = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["Id"].ToString();
}