RadGrid 中的 RadComboBox 并获取唯一行
我有一个 radgrid,每行都有一个 radcombobox。我想在选择组合框后获取行的 ID(有人在下拉列表中选择一个值)。我正在使用 radgrid 的 onitemcreated 属性来在后面的代码中调用我的方法。但是,我无法读取属于所选组合框所属行的 ID 值。任何人都可以提供任何建议。我已经尝试了 Telerik 的所有示例,但没有得到好的结果。
正如您在下面的代码中看到的,我在 gridtemplatecolumn 中有一个 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;
}
}
I have a radgrid with a radcombobox in each row. I want to get the row's ID after the combo box has been chosen (someone selecting a value in the drop down). I'm using the onitemcreated property of the radgrid to get my method called in code behind. However, I'm not able to read the value of the ID that belongs to the row in which the chosen combo box belongs to. Can anyone provide any suggestions. I have attempted all of Telerik's samples but I'm not getting good results.
As you see in my code below I have a radbutton in the gridtemplatecolumn and this works as expected. When I'm in debug I get   for the value to intID which is not a result I would expect after I select a value from the combo box.
Here is all of my code:
HTML markup:
<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>
Code behind:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Telerik 的人员提供了以下解决方案。 Telerik 的解决方案有效。
谢谢。
感谢您与我们联系。
如果您想在更改特定组合框的选定索引时获取行的 ID,我的建议是订阅服务端
OnSelectedIndexChanged
事件并使用以下事件处理函数的实现:我希望这会对你有所帮助。
亲切的问候,
迪米塔尔·泰尔齐耶夫
Telerik 团队
The guys at Telerik provided the solution below. The solution by Telerik works.
Thanks.
Thank you for contacting us.
If you want go get the row's ID when you change the selected index of particular combobox, my suggestion is to subscribe on the service-side
OnSelectedIndexChanged
event and use the following implementation of the event handling function:I hope this would help you out.
Kind regards,
Dimitar Terziev
the Telerik team
您的解决方案有效,但如果有人不在列中 ID,则还有另一种使用 RadGrid 的 DataKeyName 的方法
your solution works but there is another approach with DataKeyName of RadGrid, if somebody does not ID in columns