简体   繁体   中英

How to get values before bind into Gridview using c# asp.net

I am retrieving some data from database and going to bind in Grid view using c# asp.net.First i would like to explain my code and then i will explain my requirement.

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)
        {

        }
}
}

There is Health_Comment_Status filed value i am fetching from database.Here my requirement is before bind into gridview if Health_Comment_Status value will be R the the reject link button color from gridview will be red other wise the accept link button color from that same grid view will be green.similarly inside each click event of link button i want to check the link button back color so that i can update the status in DB.please help me.

Things are a bit more clear to me now. I have split my answer into two parts. In the first I will explain how to change the background color and in the second part I will explain how to access the status value in the OnClick event from the link (or button).

Changing the background color:

To change the background color of the 'Reject' link in the GridView control you could hook into the RowDataBound event. Here is a code sample:

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

Note that you need to change the index to represent the correct cell. If you want to get the correct cell based on the column name you could do something like described in this post: How to get the cell value by column name not by index in GridView in asp.net

You could also change the background color of the whole row by replacing this line:

e.Row.Cell[2].BackColor = System.Drawing.Color.Red;

with this line:

e.Row.BackColor = System.Drawing.Color.Red;

Access row information in OnClick event:

Add the health comment status value to the commandargument property of the Accept link. First update the link as follows:

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

Second change the OnClick eventhandler to something like this:

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

Also here note to update the code to use the correct cell index. Or you could use the same solution mentioned earlier to get the cell by column name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM