RadGrid的gridtemplatecol列中的按钮在按钮单击事件上返回错误的ID
本文关键字:按钮 返回 错误 ID 事件 gridtemplatecol RadGrid 单击 | 更新日期: 2023-09-27 18:16:23
我有一个radgrid,在gridtemplatecoluml中的一个按钮,当我点击一个按钮,它应该返回行隐藏的ID,但它一直返回不正确的ID。这是我的网格
<telerik:RadGrid ID="rgMyAddresses" runat="server" BorderStyle="Solid" CssClass="table-responsive"
AllowPaging="True" AllowSorting="True" OnNeedDataSource="rgMyAddresses_NeedDataSource"
AllowMultiRowSelection="True" AllowFilteringByColumn="true" GroupingSettings-CaseSensitive="false">
<ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
<Selecting AllowRowSelect="True" />
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
<Resizing AllowColumnResize="True" AllowResizeToFit="True" ClipCellContentOnResize="False"
EnableRealTimeResize="True" />
<ClientEvents OnRowSelected="rowSelect" />
</ClientSettings>
<MasterTableView DataKeyNames="MemberContactsID" ClientDataKeyNames="MemberContactsID"
CommandItemDisplay="None" AutoGenerateColumns="False">
<Columns>
<telerik:GridBoundColumn DataField="MemberContactsID" FilterControlAltText="Filter MemberContactsID column"
HeaderText="MemberContactsID" UniqueName="MemberContactsID" Display="false" />
<telerik:GridTemplateColumn AllowFiltering="false" HeaderStyle-Width="60px">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Button ID="btnContactToEdit" runat="server" Text="Edit" CssClass="btn btn-primary" OnClick="btnContactToEdit_Click" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="FirstLastName" FilterControlAltText="Filter Contact Name column" ShowFilterIcon="false"
HeaderText="Name" UniqueName="FirstLastName" Visible="True" AutoPostBackOnFilter="true" HeaderStyle-Width="50%" />
<telerik:GridBoundColumn DataField="Address" FilterControlAltText="Filter Member Address column" ShowFilterIcon="false"
HeaderText="Address" UniqueName="Address" Visible="True" AutoPostBackOnFilter="true">
</telerik:GridBoundColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column" />
</EditFormSettings>
</MasterTableView>
<FilterMenu EnableImageSprites="False" />
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default" />
</telerik:RadGrid>
这里是button click event
string id;
protected void btnContactToEdit_Click(object sender, EventArgs e)
{
addressView.Visible = false;
contactEditArea.Visible = true;
foreach(GridDataItem item in rgMyAddresses.MasterTableView.Items)
{
Button btn = item.FindControl("btnContactToEdit") as Button;
id = item.GetDataKeyValue("MemberContactsID").ToString();
}
Response.Write("Here is the ID: " + id.ToString() + "<br/>");
}
我建议您使用ItemCommand来获取DataKeyValue。
当前你的问题将是获得最后一个DataKeyValue由于你的循环。
是
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView DataKeyNames="MemberContactID">
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Button ID="btn" runat="server" Text="Edit"
CommandName="EditButton" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Variable
DataTable dt = new DataTable();
dt.Columns.Add("MemberContactID");
for (int i = 1; i <= 10; i++)
dt.Rows.Add(i + "");
ViewState["Data"] = dt;
RadGrid1.DataSource = dt;
RadGrid1.DataBind();
dt.Dispose();
}
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = ViewState["Data"] as DataTable;
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
// Check
if (e.CommandName == "EditButton")
{
GridDataItem item = e.Item as GridDataItem;
// Response the DataKeyValues
Response.Write(item.GetDataKeyValue("MemberContactID"));
}
}