RadGrid 的 GridTemplateColumn 中的按钮在按钮单击事件上返回不正确的 ID
Button in GridTemplateColumn of RadGrid returning incorrect ID on button click event
我有一个 radgrid,在 GridTemplateColumn 中有一个按钮,当我单击一个按钮时,它应该 return 该行隐藏 ID,但它保持 return 不正确的 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/>");
}
我建议您使用 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"));
}
}
我有一个 radgrid,在 GridTemplateColumn 中有一个按钮,当我单击一个按钮时,它应该 return 该行隐藏 ID,但它保持 return 不正确的 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/>");
}
我建议您使用 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"));
}
}