我有一个radgrid,在GridTemplateColumn中有一个按钮,当我点击一个按钮时,它应该返回该行隐藏的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>
这里是按钮点击事件
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/>");
}
答案 0 :(得分:0)
我建议您使用ItemCommand获取DataKeyValue。
目前,由于您的循环,您的问题将是获取最后一个DataKeyValue。
的.aspx
<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"));
}
}