How to have ASP.Net TemplateField with Label and Checkbox with manually added row

RickInWestPalmBeach

I have the following asp:GridView

                    <asp:GridView ID="contacts" runat="server" AutoGenerateColumns="false" Width="100%">
                        <Columns>
                            <asp:TemplateField Visible="false">
                                <ItemTemplate>
                                    <asp:HiddenField ID="ContactID" runat="server" Value='<%# Bind("employerdetailcontactid") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText ="Action"  ItemStyle-CssClass="center" >
                                <ItemTemplate>
                                    <asp:Label ID="ActionLabel" runat="server" Text='<%# Bind("action") %>' />
                                    <asp:CheckBox ID="Action" runat="server"  />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText ="First Name">
                                <ItemTemplate>
                                    <asp:TextBox ID="FirstName" runat="server" Text='<%# Bind("firstname") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText ="Last Name">
                                <ItemTemplate>
                                    <asp:TextBox ID="LastName" runat="server" Text='<%# Bind("lastname") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText ="E-Mail">
                                <ItemTemplate>
                                    <asp:TextBox ID="Email" runat="server" Text='<%# Bind("email") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText ="Phone Number">
                                <ItemTemplate>
                                    <asp:TextBox ID="PhoneNumber" runat="server" Text='<%# Bind("phonenumber") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText ="Send E-mail"  ItemStyle-CssClass="center" >
                                <ItemTemplate>
                                    <asp:CheckBox ID="SendEmail" runat="server" Checked='<%# Convert.ToBoolean( Eval("sendemail")) %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

The GridView has however many rows from the database, plus one extra row for adding a new row. Very rarely will the end-user ever add more than one contact at a time, so I am not going to bother having an "Add Row" button. I want the rows from the database to have the label say "Delete" and the manually added row have the label say "New". Here is my code to populate the GridView:

    SqlCommand cmd = new SqlCommand("SELECT *,'Delete' as action FROM contact WHERE employerid=@employerid", conn);
    cmd.Parameters.AddWithValue("@employerid", id);

    SqlDataReader dr = cmd.ExecuteReader();

    DataTable dt = new DataTable();

    //These are added to be able to manually add a row below
    dt.Columns.Add("ContactID");
    dt.Columns.Add("Action");
    dt.Columns.Add("FirstName");
    dt.Columns.Add("LastName");
    dt.Columns.Add("Email");
    dt.Columns.Add("PhoneNumber");
    dt.Columns.Add("SendEmail");

    dt.Load(dr);


    DataRow drow = dt.NewRow();
    drow["ContactID"] = 0;
    drow["Action"] = false;
    drow["FirstName"] = "";
    drow["LastName"] = "";
    drow["Email"] = "";
    drow["PhoneNumber"] = "";
    drow["SendEmail"] = "False";
    dt.Rows.Add(drow);

    contacts.DataSource = dt;
    contacts.DataBind();

How would I set the text of ActionLabel to "New" for the manually added row ?

Svetoslav Petkov

You have two options:

  1. Attach for the RowDataBound event and programmatically set the text
<asp:TemplateField HeaderText ="Action"  ItemStyle-CssClass="center" >
  <ItemTemplate>
    <asp:Label ID="ActionLabel" runat="server" Text='<%# Bind("action") %>' />
    <asp:CheckBox ID="Action" runat="server"  />
  </ItemTemplate>
</asp:TemplateField>

//use the onItemDatabound

  void contacts_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
        
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // get contact id to identify whether this is fake row
      int contactId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ContactID"));
      string labelText = contactId > 0 ? "Delete" : "New";
      ((Label)e.Item.FindControl("ActionLabel")).Text = labelText;
    }
  }
  1. Option 2: Use FooterRow template and enable footer row in the GridView component
<asp:GridView ShowFooter = "true"
...
<asp:TemplateField HeaderText ="Action"  ItemStyle-CssClass="center" >
  <ItemTemplate>
    <asp:Label ID="ActionLabel" runat="server" Text="Delete" />
    <asp:CheckBox ID="Action" runat="server"  />
  </ItemTemplate>
  <FooterTemplate>
    <asp:Label ID="ActionLabel" runat="server" Text="New" />
    <asp:CheckBox ID="Action" runat="server"  />
  </FooterTemplate>
</asp:TemplateField>

I am a little confused whether I modified the right GridView column, but I think you'll get the idea

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to update a label out of gridview by TemplateField checkbox in that gridview without postback

How to change the style of the label of a checkbox if it's checked in asp.net?

How to add (hidden) label for asp:CheckBox?

How to use multiple conditions in asp.net Eval function to enable and disable a button in a templatefield?

ASP.NET MVC: Align checkbox and label on same line

How to request an Asp.Net page manually?

How to align a label to the top of a row in a table asp.net C#

How to replace or remove text of label manually in VB.Net?

Space between checkbox and its label not Added

Do certificates have to be manually added to client machines?

how to clear ImageUpload and Checkbox in ASP NET?

label of selected row in gridview asp.net with jquery

Call TemplateField DropDownList in asp.net - code behind with FindControl()

Delete manually added table row by Javascript

ASP.NET - Problem with authentication: Manually added claim is missing in the next request

HOW TO bind value to label asp.net

How to bind MVC .net core checkbox with label as button

How to align a label with checkbox

How to manually split data by label

Dynamically added controls in gridview (checkbox) disappear after postback in asp.net

How to skip to next asp.TemplateField if no data is available to bind to dataBinder?

How to generate an anti forgery token manually in ASP.NET Core

How to manually decrypt an ASP.NET Core Authentication cookie?

How to check password manually in Asp.Net identity 2?

How to Access Programmatically Added checkbox

How can I create states for checkbox Booleans used in a todo list that can have items added/deleted by the user?

How to address a dynamically added label

How to use added table in tablecell asp.net

How to have a "on checked" animation restart with every "check" ? [label + input="checkbox" + CSS only]