如何使用c#asp.net在LinkBut​​ton的click事件中获取gridview的DataKeyNames值

时间:2015-08-08 04:36:43

标签: c# asp.net gridview

我需要使用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值。请帮助我。

1 个答案:

答案 0 :(得分:0)

您可以传递CommandArgument,单击按钮,命令参数将所需值传递给后端。可以通过GridViewName_ItemCommand()

e.CommandArgument事件中访问

你也可以这样给:

 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
    }
}