内联gridview在行的顶部添加新行

时间:2017-02-20 05:56:01

标签: c# asp.net

内联gridview在行顶部添加新行。

这是图片:

enter image description here

我想要在顶行(网格的第一行)添加行而不是底行。

以下是代码:

<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:HiddenField ID="hdnID" runat="server" Value='<%#Eval("ID") %>' />
            <%--<asp:Label ID="lblCustomerID" runat="server"
                Text='<%# Eval("ID")%>'></asp:Label>--%>
        </ItemTemplate>
        <FooterTemplate>
            <%--<asp:TextBox ID="txtCustomerID" Width="40px"
                        MaxLength="5" runat="server"></asp:TextBox>--%>
        </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Company Name">
        <ItemTemplate>
            <asp:Label ID="lblcmpnm" Text='<%#Eval("Companynm")%>' runat="server"> </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>

            <asp:TextBox ID="txtcmpnm" runat="server" CssClass="form-control"
                Text='<%#Eval("Companynm")%>'></asp:TextBox>
            <%--  <span id="erroreml1" runat="server" style="color:red"></span>--%>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtcmpnm" runat="server" CssClass="form-control" placeholder="Company Name"></asp:TextBox>
            <span id="errorcmpnm" runat="server" style="color: red"></span>

        </FooterTemplate>
    </asp:TemplateField>


    <asp:TemplateField HeaderText="Email">
        <ItemTemplate>
            <asp:Label ID="lblTname" Text='<%#Eval("Email")%>' runat="server"> </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>

            <asp:TextBox ID="txteml" runat="server" CssClass="form-control"
                Text='<%#Eval("Email")%>'></asp:TextBox>
            <span id="erroreml1" runat="server" style="color: red"></span>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txteml" runat="server" CssClass="form-control" placeholder="Enter Email"></asp:TextBox>
            <span id="erroreml" runat="server" style="color: red"></span>

        </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Phone">
        <ItemTemplate>
            <asp:Label ID="lblproject" Text='<%#Eval("Phone")%>' runat="server"> </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtph" TextMode="MultiLine" runat="server" CssClass="form-control"
                Text='<%#Eval("Phone")%>'></asp:TextBox>
            <span id="errorph1" runat="server" style="color: red"></span>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtph" TextMode="MultiLine" runat="server" CssClass="form-control" placeholder="Enter Phone Number"></asp:TextBox>
            <span id="errorph" runat="server" style="color: red"></span>

        </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Category">
        <ItemTemplate>
            <asp:Label ID="lblEmpName" Text='<%#Eval("Category")%>' runat="server"> </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtcat" runat="server" CssClass="form-control"
                Text='<%#Eval("Category")%>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtcat" CssClass="form-control" runat="server" placeholder="Enter Category"></asp:TextBox>
        </FooterTemplate>
    </asp:TemplateField>



    <asp:TemplateField HeaderText="City">
        <ItemTemplate>
            <asp:Label ID="lblSdate" Text='<%#Eval("City")%>' runat="server"> </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtcity" runat="server" CssClass="form-control"
                Text='<%#Eval("City")%>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtcity" runat="server" CssClass="form-control" placeholder="Enter City"></asp:TextBox>

        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Comments">
        <ItemTemplate>
            <asp:Label ID="lblcmnts" Text='<%#Eval("Comments")%>' runat="server"> </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtcmnts" TextMode="MultiLine" runat="server" CssClass="form-control"
                Text='<%#Eval("Comments")%>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtcmnts" TextMode="MultiLine" runat="server" CssClass="form-control" placeholder="Enter Comments"></asp:TextBox>

        </FooterTemplate>
    </asp:TemplateField>


    <asp:TemplateField HeaderText="Action">
        <ItemTemplate>
            <asp:LinkButton runat="server" Text="Edit" ID="imgEdit" CommandName="Edit" CommandArgument='<%#Eval("ID") %>'><img src="images/edit.png" alt="Edit" title="Edit Title" /> </asp:LinkButton>
            <asp:LinkButton ID="imgDelete" Text="Delete" runat="server" OnClientClick="return confirm('Are you sure to delete Task?');" CommandName="Delete" CommandArgument='<%#Eval("ID") %>'><img src="images/cross.png" alt="Edit" title="Delete Task" /></asp:LinkButton>                                                                      
            <asp:LinkButton ID="lnkopenpopup" runat="server" Text="Appointment" Style="float: left; margin-right: 5px;" CommandName="App" CommandArgument='<%#Eval("ID") %>' CssClass="label label-sm label-success"></asp:LinkButton>                                                                    </ItemTemplate>                                                                    <EditItemTemplate>                                                                        <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="../Content/images/user_edit.png" ToolTip="Update" OnClientClick="return GetGridFooterRowvalues1()" />
            <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="images/cross.png" ToolTip="Cancel" /></EditItemTemplate><FooterTemplate>                                                                        <asp:Button ID="btnsave" runat="server" Text="Add New" class="btn icon-btn btn-success btn-sm" OnClientClick="return GetGridFooterRowvalues()"
                OnClick="btnsave_Click" />
        </FooterTemplate>
    </asp:TemplateField><asp:CommandField ShowEditButton="True" />--%></Columns>

在上面的gridview中,添加行来自gridview的buttom,如图所示。我想在网格的顶部添加itemtemplate中的行。有什么建议? 我尝试了很多代码,但它们没有用。

以下是服务器端的总编码:

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dtg.Columns.Add("ID");
            dtg.Columns.Add("Companynm");
            dtg.Columns.Add("Email");
            dtg.Columns.Add("Phone");
            dtg.Columns.Add("Category");
            dtg.Columns.Add("City");
            dtg.Columns.Add("Comments");


            dtg.Rows.Add("0", " ", " ", " ", " ", " ", " ");
            dtg.AcceptChanges();

            if ((Session["adminuser"] == null) && (Session["adminusername"] == null) && (Session["EmployeeuserID"] == null) && (Session["Employeeusername"] == null))
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                bind_task_grid();
                //addedit.Visible = false;
                //dvsearch.Visible = true;
            }



        }
    }
    MailMarketingss obj = new MailMarketingss();
    protected void bind_task_grid()
    {
        DataTable dttask = new DataTable();


        dttask = obj.FetchMailMarketing(Convert.ToInt32(Session["EmployeeuserID"]));
        if (dttask.Rows.Count > 0)
        {
            gvTaskDetails.Visible = true;
            gvTaskDetails.DataSource = dttask;
            gvTaskDetails.DataBind();
            divMsg.Style[HtmlTextWriterStyle.Display] = "none";

        }
        else
        {
            //divMsg.Style[HtmlTextWriterStyle.Display] = "block";
            //divMsg.InnerHtml = "No Records found!";
            //   gvTaskDetails.Visible = false;
            gvTaskDetails.DataSource = dtg;
            gvTaskDetails.DataBind();

        }


    }
    protected void gvTaskDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        bind_task_grid();
        gvTaskDetails.PageIndex = e.NewPageIndex;
        gvTaskDetails.DataBind();
    }

    protected void gvTaskDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Edit")
        {
            //addedit.Visible = true;
            DataTable dtEdit = new DataTable();
            int id = Convert.ToInt32(e.CommandArgument);
            dtEdit = obj.FetchMailMarketingByID(Convert.ToInt32(e.CommandArgument));

            if (dtEdit.Rows.Count > 0)
            {

                //txteml.Text = Convert.ToString(dtEdit.Rows[0]["Email"].ToString());
                //txtph.Text = Convert.ToString(dtEdit.Rows[0]["Phone"].ToString());
                //txtcat.Text = Convert.ToString(dtEdit.Rows[0]["Category"].ToString());
                //txtcity.Text = Convert.ToString(dtEdit.Rows[0]["City"].ToString());
            }
            ViewState["id"] = id;

        }
        if (e.CommandName == "Delete")
        {

            int retval = obj.DeleteMailMarketingById(Convert.ToInt32(e.CommandArgument));
            if (retval > 0)
            {
                bind_task_grid();
                //addedit.Visible = false;
                Response.Redirect(Request.RawUrl);
            }
        }


        if (e.CommandName == "App")
        {
            rptCustomers.DataSource = null;
            rptCustomers.DataBind();
            ViewState["idd"] = null;
            mdlpopup.Show();

            DataTable retval = obj.FetchAppointmentByMalmrktId(Convert.ToInt32(e.CommandArgument));
            if (retval.Rows.Count > 0)
            {
                //Repeater rptCustomers = (Repeater)Page.FindControl("rptCustomers");
                rptCustomers.DataSource = retval;
                rptCustomers.DataBind();
                BindAppointment(Convert.ToInt32(e.CommandArgument));
                //                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "none",
                //"<script>$('#mymodal').modal('show');</script>", false);
                mdlpopup.Show();

            }
            ViewState["idd"] = e.CommandArgument;
        }
    }

     protected void gvTaskDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            HiddenField lbltital = (HiddenField)e.Row.FindControl("hdnID");

            if (lbltital != null)
            {
                if (lbltital.Value == "0")
                {
                    e.Row.Visible = false;
                }
            }
        }
    } 

     protected void gvTaskDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            HiddenField lbltital = (HiddenField)e.Row.FindControl("hdnID");

            if (lbltital != null)
            {
                if (lbltital.Value == "0")
                {
                    e.Row.Visible = false;
                }
            }
        }
    }
protected void gvTaskDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {



        string CustomerID = ((HiddenField)gvTaskDetails.Rows[e.RowIndex]
                   .FindControl("hdnID")).Value;

        string Companynm = ((TextBox)gvTaskDetails.Rows[e.RowIndex]
                           .FindControl("txtcmpnm")).Text;

        string Email = ((TextBox)gvTaskDetails.Rows[e.RowIndex]
                            .FindControl("txteml")).Text;
        string Phone = ((TextBox)gvTaskDetails.Rows[e.RowIndex]
                            .FindControl("txtph")).Text;
        string Category = ((TextBox)gvTaskDetails.Rows[e.RowIndex]
                          .FindControl("txtcat")).Text;
        string City = ((TextBox)gvTaskDetails.Rows[e.RowIndex]
                        .FindControl("txtcity")).Text;
        string Comment = ((TextBox)gvTaskDetails.Rows[e.RowIndex]
                       .FindControl("txtcmnts")).Text;
        if (Email != "" || Phone != "")
        {
            int retval1 = obj.UpdateUser(Convert.ToInt32(CustomerID), Companynm, Email, Phone, Category, City, Comment);
        }

        gvTaskDetails.EditIndex = -1;
        bind_task_grid();
        gvTaskDetails.DataBind();
    }
 protected void gvTaskDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvTaskDetails.EditIndex = -1;
        bind_task_grid();
    }
  protected void gvTaskDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {

        gvTaskDetails.EditIndex = e.NewEditIndex;
        bind_task_grid();


    }

 protected void btnsave_Click(object sender, EventArgs e)
    {
        int flag = 1;
        //if (Convert.ToInt32(ViewState["id"]) > 0)
        //{
        //flag = 2;

        //var duplicate = ManageModuleBL.DuplicateUpdateReason(txtrolename.Text, Convert.ToInt32(ViewState["id"]));
        //if (duplicate.Count > 0)
        //{
        //    errormsg.Text = "No Duplicate Entry Allowed..";
        //}
        //else
        //{





        //int retval1 = obj.UpdateUser(Convert.ToInt32(ViewState["id"]), txteml.Text, txtph.Text, txtcat.Text, txtcity.Text);
        //if (retval1 > 0)
        //{
        //    bind_task_grid();
        //    ViewState["id"] = null;
        //    //txteml.Text = String.Empty;
        //    //txtph.Text = String.Empty;
        //    //txtcat.Text = String.Empty;
        //    //txtcity.Text = String.Empty;
        //    addedit.Visible = false;
        //}
        // }
        //}
        //else
        {


            //var duplicate = ManageModuleBL.DuplicateReason(txtrolename.Text);
            //if (duplicate.Count > 0)
            //{
            //    errormsg.Text = "No Duplicate Entry Allowed..";
            //}
            //else
            //{

            string txtcmpnm = ((TextBox)gvTaskDetails.FooterRow.FindControl("txtcmpnm")).Text;
            string txteml = ((TextBox)gvTaskDetails.FooterRow.FindControl("txteml")).Text;
            string txtph = ((TextBox)gvTaskDetails.FooterRow.FindControl("txtph")).Text;
            string txtcat = ((TextBox)gvTaskDetails.FooterRow.FindControl("txtcat")).Text;
            string txtcity = ((TextBox)gvTaskDetails.FooterRow.FindControl("txtcity")).Text;
            string txtComments = ((TextBox)gvTaskDetails.FooterRow.FindControl("txtcmnts")).Text;
            if (txteml != "" || txtph != "")
            {
                int retval = obj.InsertUser(txtcmpnm, txteml, txtph, txtcat, txtcity, txtComments, Convert.ToInt32(Session["EmployeeuserID"]));
                if (retval > 0)
                {
                    bind_task_grid();
                }
            }


            //}
        }
    }

0 个答案:

没有答案