如何使用c# asp.net获取LinkButton's点击事件中gridview的DataKeyNames值
本文关键字:事件 gridview DataKeyNames asp 何使用 net 获取 LinkButton | 更新日期: 2023-09-27 18:07:01
我需要使用c#在linkbutton的点击事件中获取Gridview
的DataKeyNames值。我在下面解释我的代码。
index.aspx:
<asp:GridView ID="comnt_Gridview" runat="server" AutoGenerateColumns="false" Width="100%" CssClass="table table-striped table-bordered margin-top-zero" OnRowDataBound="comnt_Gridview_RowDataBound" DataKeyNames="Health_Comment_ID" >
<Columns>
<asp:TemplateField HeaderText="Sl. No" ItemStyle-CssClass="col-md-1 col-sm-1">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<ItemStyle CssClass="col-md-1 col-sm-1"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Health ID" ItemStyle-CssClass="col-md-1 col-sm-1" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="healthid" runat="server" Text='<%#Eval("Health_ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" CssClass="col-md-1 col-sm-1"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-CssClass="col-md-1 col-sm-1" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Name" runat="server" Text='<%#Eval("Health_Comment_Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" CssClass="col-md-1 col-sm-1"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" ItemStyle-CssClass="col-md-2 col-sm-2" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Email" runat="server" Text='<%#Eval("Health_comment_Email") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" CssClass="col-md-2 col-sm-2"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Web Url" ItemStyle-CssClass="col-md-2 col-sm-2" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Url" runat="server" Text='<%#Eval("Health_Comment_Website") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" CssClass="col-md-2 col-sm-2"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments" ItemStyle-CssClass="col-md-3 col-sm-3" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblRemark" runat="server" Text='<%# Eval("Health_Comment_Message").ToString().Length > 100? (Eval("Health_Comment_Message") as string).Substring(0,100) + " ..." : Eval("Health_Comment_Message") %>'ToolTip='<%# Eval("Health_Comment_Message") %> '> </asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" CssClass="col-md-3 col-sm-3"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" ItemStyle-CssClass="col-md-1 col-sm-1" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Url" runat="server" Text='<%#Eval("Health_Comment_Status") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" CssClass="col-md-1 col-sm-1"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-CssClass="col-md-2 col-sm-2" >
<ItemTemplate>
<div class="pull-left rpaddingdivsmall1"><a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="View" onClick="doctordetailsshowdiv()"><i class="fa fa-eye"></i></a> / </div>
<div class="profile_right_side_eidt pull-left">
<div class="btn-group btn-toggle">
<asp:LinkButton ID="accept" CssClass="btn btn-xs btn-inactive" CommandName="Accept" runat="server" style="padding:1px 2px;" Text="Accept" OnClick="accept_click"></asp:LinkButton>
<asp:LinkButton ID="reject" runat="server" Text="Reject" CommandName="Reject"
CssClass="btn btn-xs btn-success active" style="padding:1px 2px;"
onclick="reject_Click" ></asp:LinkButton>
</div>
</div>
<div class="clearfix"></div>
</ItemTemplate>
<ItemStyle CssClass="col-md-2 col-sm-2"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
这里我需要当用户将点击任何链接按钮(i.e. accept/Reject
),相应的按钮背景色将改变,并在点击事件,我需要DataKeyNames
值。
您可以传递CommandArgument
,在单击按钮时,命令参数将所需值传递给后端。可通过e.CommandArgument
访问GridViewName_ItemCommand()
事件
你也可以这样给予:
CommandArgument='<%#Eval("CustomerID")%>'
要更改点击时的颜色,可以使用如下命令:
Protected Void GridViewName_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
if(e.CommandName=="")
{
LinkButton lnkButton=(LinkButton)e.CommandSource
lnkButton.ForeColor = System.Drawing.Color.Red;
//Do any additional operation if needed on link button click
}
}