如何在标题列旁边显示搜索框

时间:2016-11-09 12:27:05

标签: javascript c# jquery asp.net

在我的应用程序中,我需要在EmpName旁边的标题列旁边显示搜索框,但它会显示员工姓名下方的搜索框。如何将搜索框放在gridview标题旁边。屏幕截图显示附加的节目我得到了标题EmpName下面的搜索框但我应该在EmpName旁边。我可以这样做

<style type="text/css">
        .Gridview {
            font-family: Verdana;
            font-size: 10pt;
            font-weight: normal;
            color: black;
        }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="js/quicksearch.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.search_textbox').each(function (i) {
                $(this).quicksearch("[id*=GridView1] tr:not(:has(th))", {
                    'testQuery': function (query, txt, row) {
                        return $(row).children(":eq(" + i + ")").text().toLowerCase().indexOf(query[0].toLowerCase()) != -1;
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Panel ID="Panel2" runat="server" Visible="true">
                <asp:ScriptManager ID="ScriptManager1" runat="server" />
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:GridView ID="GridView1" ShowHeader="true" runat="server" AutoGenerateColumns="False" DataKeyNames="EmpId" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound" UseAccessibleHeader ="true">
                            <AlternatingRowStyle Width="80px" />
                            <Columns>

                                <asp:BoundField DataField="EmpName" HeaderText="EmpName" ReadOnly="true" ItemStyle-Width="100"  />
                                <asp:BoundField DataField="Designation" HeaderText="Designation" ReadOnly="true" ItemStyle-Width="100" />
                                <asp:BoundField DataField="salary" HeaderText="Salary" ReadOnly="true" ItemStyle-Width="100" />
                                <asp:BoundField DataField="notes" HeaderText="Notes" ItemStyle-Width="150" />

                                <%--                    <asp:CommandField ShowEditButton="true" CancelText="" DeleteText="" EditText='<%#Eval("notes").ToString()=="" ? "add" : "edit" %>' UpdateText='<%# Eval("notes")%>' />--%>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Button CommandName="Edit" runat="server" Text='<%# (string.IsNullOrEmpty(Eval("notes").ToString()))  ? "Add":"Edit"%>' ID="btnAdd" />
                                        <asp:Button CommandName="Update" Visible="false" runat="server" Text='<%# (string.IsNullOrEmpty(Eval("notes").ToString()))  ? "Save":"Update"%>' ID="btnUpdate" />
                                        <%-- <asp:Button CommandName="Cancel" Visible="false" runat="server" Text="Cancel" ID="btnCancel" />--%>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>

                        </asp:GridView>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </asp:Panel>

        </div>

 private SqlConnection conn = new SqlConnection("Data Source=EHSD208;Database=Training;User Id=sa;Password=Access#123");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvbind();
            }
        }
        protected void gvbind()
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select * from EmpDet", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = ds;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            gvbind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            gvbind();
            Button addButton = (Button)GridView1.Rows[e.NewEditIndex].FindControl("btnAdd");
            addButton.Visible = false;
            Button updateButton = (Button)GridView1.Rows[e.NewEditIndex].FindControl("btnUpdate");
            updateButton.Visible = true;

            //Button cancelButton = (Button)GridView1.Rows[e.NewEditIndex].FindControl("btnCancel");
            //cancelButton.Visible = true;
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            int EmpId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            TextBox textc1 = (TextBox)row.Cells[3].Controls[0];
            GridView1.EditIndex = -1;
            conn.Open();
            SqlCommand cmd = new SqlCommand("update EmpDet set notes='" + textc1.Text + "'where EmpId='" + EmpId + "'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            gvbind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //GridView1.EditIndex = -1;
            //gvbind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (!string.IsNullOrEmpty(e.Row.Cells[3].Text))
                {
                    e.Row.Cells[3].BackColor = System.Drawing.Color.BurlyWood;
                }
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            //for (int i = 0; i < GridView1.Columns.Count - 1; i++)
            //{
                TableHeaderCell cell = new TableHeaderCell();
                TextBox txtSearch = new TextBox();
                txtSearch.Attributes["Search Box"] = GridView1.Columns[1].HeaderText;
                txtSearch.CssClass = "search_textbox";
                cell.Controls.Add(txtSearch);
                row.Controls.Add(cell);
            //}
            GridView1.HeaderRow.Parent.Controls.AddAt(1, row);
        }
    }

以上是我的完整代码,与搜索框相关的代码是

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="js/quicksearch.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.search_textbox').each(function (i) {
                $(this).quicksearch("[id*=GridView1] tr:not(:has(th))", {
                    'testQuery': function (query, txt, row) {
                        return $(row).children(":eq(" + i + ")").text().toLowerCase().indexOf(query[0].toLowerCase()) != -1;
                    }
                });

 <asp:GridView ID="GridView1" ShowHeader="true" runat="server" AutoGenerateColumns="False" DataKeyNames="EmpId" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound" UseAccessibleHeader ="true">
                            <AlternatingRowStyle Width="80px" />
                            <Columns>

                                <asp:BoundField DataField="EmpName" HeaderText="EmpName" ReadOnly="true" ItemStyle-Width="100"  />
                                <asp:BoundField DataField="Designation" HeaderText="Designation" ReadOnly="true" ItemStyle-Width="100" />
                                <asp:BoundField DataField="salary" HeaderText="Salary" ReadOnly="true" ItemStyle-Width="100" />
                                <asp:BoundField DataField="notes" HeaderText="Notes" ItemStyle-Width="150" />

                                <%--                    <asp:CommandField ShowEditButton="true" CancelText="" DeleteText="" EditText='<%#Eval("notes").ToString()=="" ? "add" : "edit" %>' UpdateText='<%# Eval("notes")%>' />--%>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Button CommandName="Edit" runat="server" Text='<%# (string.IsNullOrEmpty(Eval("notes").ToString()))  ? "Add":"Edit"%>' ID="btnAdd" />
                                        <asp:Button CommandName="Update" Visible="false" runat="server" Text='<%# (string.IsNullOrEmpty(Eval("notes").ToString()))  ? "Save":"Update"%>' ID="btnUpdate" />
                                        <%-- <asp:Button CommandName="Cancel" Visible="false" runat="server" Text="Cancel" ID="btnCancel" />--%>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>

                        </asp:GridView>
            });
        });
    </script>
  protected void GridView1_DataBound(object sender, EventArgs e)
        {
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            //for (int i = 0; i < GridView1.Columns.Count - 1; i++)
            //{
                TableHeaderCell cell = new TableHeaderCell();
                TextBox txtSearch = new TextBox();
                txtSearch.Attributes["Search Box"] = GridView1.Columns[1].HeaderText;
                txtSearch.CssClass = "search_textbox";
                cell.Controls.Add(txtSearch);
                row.Controls.Add(cell);
            //}
            GridView1.HeaderRow.Parent.Controls.AddAt(1, row);
        }
    }

0 个答案:

没有答案
相关问题