为什么我的网格视图后消失,我把搜索功能,选择,插入,删除,插入GridView控件 [英] Why did my Grid View disappear after I put search function, select,insert,delete,insert gridview
问题描述
我学习的GridView .NET和C#
I am learning Gridview .net and C#
我可以编辑,删除和插入也
I could edit,delete and also insert.
我添加了搜索功能,搜索功能是工作,但在打开的页面,我没有看到页面上的GridView控件,但搜索和按钮的唯一文本框。我有些类型后,搜索功能是完全正常工作。
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.
我想看看在GridView还页面打开的时候,加上搜索框
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" />
我只是添加此code
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>
为什么我的GridView消失了?
Why did my GridView disappear?
感谢你这么多。
推荐答案
这是因为你不及格的默认值。只需添加默认值
属性,在ControlParameter: -
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.
这篇关于为什么我的网格视图后消失,我把搜索功能,选择,插入,删除,插入GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!