我正在从数据库中检索一些数据并使用c#asp.net在Grid视图中绑定。首先,我想解释一下我的代码,然后我将解释我的要求。
的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>
<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" CommandArgument='<%#Eval("Health_Comment_ID")%>'></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" CommandArgument='<%#Eval("Health_Comment_ID")%>' ></asp:LinkButton>
</div>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" CssClass="col-md-2 col-sm-2"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-CssClass="col-md-2 col-sm-2" >
<ItemTemplate>
<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>
</ItemTemplate>
<ItemStyle CssClass="col-md-2 col-sm-2"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
index.aspx.cs:
namespace ODIYA_Doctor_Admin.Module.Front_End_Management
{
public partial class HealthComment : System.Web.UI.Page
{
private healthCommentBL objhealthCommentBL = new healthCommentBL();
protected void Page_Load(object sender, EventArgs e)
{
lbluname.Text = Session["username"].ToString().Substring(0, Session["username"].ToString().IndexOf(' '));
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
comnt_Gridview.DataSource = objhealthCommentBL.getHealthCommentDetails();
comnt_Gridview.DataBind();
}
protected void comnt_Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
}
}
我从数据库中获取Health_Comment_Status
字段值。我的要求是在绑定到gridview之前,如果Health_Comment_Status
值为R
,则来自gridview的拒绝链接按钮颜色将是红色其他明智的接受链接按钮颜色从相同的网格视图将是绿色。类似于链接按钮的每个单击事件内我想检查链接按钮返回颜色,以便我可以更新DB.please帮助我的状态。
答案 0 :(得分:2)
现在事情对我来说更清楚了。我把答案分成了两部分。在第一部分中,我将解释如何更改背景颜色,在第二部分中,我将解释如何从链接(或按钮)访问OnClick事件中的状态值。
更改背景颜色
更改&#39;拒绝&#39;的背景颜色。您可以在GridView控件中链接到RowDataBound事件。这是一个代码示例:
protected void EmployeeAvailabilityGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(DataBinder.Eval(e.Row.DataItem, "Health_Comment_Status") == "R")
{
e.Row.Cell[2].BackColor = System.Drawing.Color.Red;
}
}
}
catch (Exception ex)
{
//ErrorLabel.Text = ex.Message;
}
}
请注意,您需要更改索引以表示正确的单元格。如果您想根据列名称获取正确的单元格,可以执行此帖中所述的操作:How to get the cell value by column name not by index in GridView in asp.net
您还可以通过替换此行来更改整行的背景颜色:
e.Row.Cell[2].BackColor = System.Drawing.Color.Red;
这一行:
e.Row.BackColor = System.Drawing.Color.Red;
访问OnClick事件中的行信息:
将运行状况注释状态值添加到Accept链接的commandargument属性中。首先更新链接如下:
<asp:LinkButton ID="accept" CssClass="btn btn-xs btn-inactive" CommandName="Accept" runat="server" style="padding:1px 2px;" Text="Accept" onclick="accept_click" CommandArgument='<%#Eval("Health_Comment_ID")%>|<%#Eval("Health_Comment_Status")%>'></asp:LinkButton>
第二次将OnClick事件处理程序更改为:
protected void linkButton_click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string[] commandArgs = btn.CommandArgument.Split('|')
if(commandArgs.Length <= 1 then
return;
string healthCommentStatus = commandArgs[1];
}
此处还注意更新代码以使用正确的单元格索引。或者您可以使用前面提到的相同解决方案来按列名称获取单元格。