locked
C# Custom GridView RRS feed

  • Question

  • User1344036602 posted

    Hi,

    I'm not really proficient <g class="gr_ gr_100 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="100" data-gr-id="100">in</g><g class="gr_ gr_100 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="100" data-gr-id="100"> </g>asp.net<g class="gr_ gr_100 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="100" data-gr-id="100">.</g> I Have an asp based web application and I want to create a <g class="gr_ gr_105 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="105" data-gr-id="105">custom</g><g class="gr_ gr_105 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="105" data-gr-id="105"> </g>GridView<g class="gr_ gr_105 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="105" data-gr-id="105"> </g><g class="gr_ gr_105 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="105" data-gr-id="105">in</g> order to use it whenever I have a search box and reduce redundancy in my code.

    I want to have <g class="gr_ gr_95 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="95" data-gr-id="95">this</g><g class="gr_ gr_95 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="95" data-gr-id="95"> </g>GridView<g class="gr_ gr_95 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="95" data-gr-id="95"> </g><g class="gr_ gr_95 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="95" data-gr-id="95">below</g> <g class="gr_ gr_96 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="96" data-gr-id="96">my</g><g class="gr_ gr_96 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="96" data-gr-id="96"> </g>textbox<g class="gr_ gr_96 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="96" data-gr-id="96"> </g><g class="gr_ gr_96 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="96" data-gr-id="96">and</g> on text changing <g class="gr_ gr_97 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="97" data-gr-id="97">the</g><g class="gr_ gr_97 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="97" data-gr-id="97"> </g>GridView<g class="gr_ gr_97 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="97" data-gr-id="97"> </g><g class="gr_ gr_97 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="97" data-gr-id="97">shows</g> mostly searched results and a "More" button for <g class="gr_ gr_94 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="94" data-gr-id="94">advance</g> search which will open a new page. Can anybody help me how can I start?

    Thank You

    Hari

    Saturday, February 17, 2018 1:15 PM

Answers

  • User283571144 posted

    Hi kosmik5,

    According to your description, I suggest you could try to use girdview's FilterExpression feature to achieve your requirement.

    After searching the result, you could create a more button in the bottom of the gridview.

    If user click the button, then you could generate the sql query according to search textbox's value and pass this sql query to another page to show the search result gridview.

    Details about how to parameter in girdview, you could refer to this article.

    More details, you could refer to below codes:

        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
                  <asp:Button ID="Button2" runat="server" Text="Reset"  OnClick="Button2_Click"/>
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" ShowFootet="false" >
                    <Columns>
                        <asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">
                            <EditItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Age" SortExpression="Age">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
                            </ItemTemplate>
    
                                <FooterTemplate>
                               <asp:Button ID="Button3" runat="server" Text="More" OnClick="Button3_Click" />
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name" SortExpression="Name">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
    
                        </asp:TemplateField>
       
                        </Columns>
                 
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Persons]" FilterExpression=" name like '%{0}%' ">
                           <FilterParameters>
                <asp:ControlParameter Name="txtSearcht" ControlID="txtSearch" 
                    Type="String"  PropertyName="Text" />
     
            </FilterParameters>
                </asp:SqlDataSource>
                <br />
                <br />
                <br />
                <br />
            </div>
        </form>

    Code-behind:

        public partial class Gridview : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
             }
    
     
            protected void Button2_Click(object sender, EventArgs e)
            {
                SqlDataSource1.FilterExpression = null;
            }
    
            protected void btnSearch_Click(object sender, EventArgs e)
            {
                GridView1.ShowFooter = true;
            }
    
            protected void Button3_Click(object sender, EventArgs e)
            {
                //Then generate a sql query with textbox value and send the sql query to another page to show the girdview
                Response.Redirect("url with query string");
                //refesh the page with default search result
                SqlDataSource1.FilterExpression = null;
            }
        }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 9:02 AM