问题
I am learning Gridview .net and C#
I could edit,delete and also insert.
I added search function, search function is working but when the page open, I do not see the gridview on the page, but only textbox of search and button. After I types some, the search function is completely working.
I would like to see the gridview also when the page opens, plus search box
<asp:TextBox ID="txtSearch" runat="server" CssClass="txt"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="buttongr" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FleetManagementConnectionString %>"
DeleteCommand="DELETE FROM [Genres] WHERE [GenreID] = @GenreID"
InsertCommand="INSERT INTO [Genres] ([Genre]) VALUES (@Genre)"
SelectCommand="SELECT * FROM [Genres] WHERE ([Genre] LIKE '%' + @Genre + '%')"
UpdateCommand="UPDATE [Genres] SET [Genre] = @Genre WHERE [GenreID] = @GenreID">
<DeleteParameters>
<asp:Parameter Name="GenreID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Genre" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Genre" Type="String" />
<asp:Parameter Name="GenreID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="Genre" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="GenreID" DataSourceID="SqlDataSource1" ShowFooter="True" Visible ="true" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="GenreID" InsertVisible="False" SortExpression="GenreID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("GenreID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("GenreID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ValidationGroup="Insert" OnClick="LbInsert_Click" runat="server">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Genre" SortExpression="Genre">
<EditItemTemplate>
<asp:TextBox ID="txtGenre" runat="server" Text='<%# Bind("Genre") %>'></asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="Insert" ID="rfvGenre" runat="server" ErrorMessage="Required"
ControlToValidate="Textbox1" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Genre") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:ValidationSummary ValidationGroup="Insert" ID="ValidationSummary1" ForeColor="red" runat="server" />
I just add this code
SelectCommand="SELECT * FROM [Genres] WHERE ([Genre] LIKE '%' + @Genre + '%')"
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="Genre" PropertyName="Text" Type="String" />
</SelectParameters>
Why did my GridView disappear?
Thank you so much
回答1:
That's because you are not passing the default value. Simply add DefaultValue attribute in your ControlParameter:-
<asp:ControlParameter ControlID="txtSearch" DefaultValue="%%"
Name="Genre" PropertyName="Text" Type="String" />
Here, I am passing an %% since you have Like operator. So by default when the page first loads and the value of textbox will be empty, control parameter will pass this default value which will fetch you all the records.
来源:https://stackoverflow.com/questions/33443533/why-did-my-grid-view-disappear-after-i-put-search-function-select-insert-delete