Бихме искали да опресним ASP.Net GridView, ако ASP.Net бутон и GridView са в различни блокове asp:Content
В работеща уеб форма на ASP.Net имахме DataSource, GridView, DetailsView, различни други контроли, asp:TextBox и asp:Button за търсене на данни въз основа на това, което потребителят въвежда в текстовото поле. Всички те бяха в един блок asp:Content и също имаше asp:UpdatePanel.
Решихме да променим оформлението на формуляра и да разделим GridView и DetailsView и да ги поставим в друг блок asp:Content. Когато формулярът беше стартиран, всичко се появи на правилните места на екрана и също така показа данни от базата данни, както се очакваше.
Открихме, че ако потребителят въведе критерии за търсене и щракне върху бутона за търсене, кодът във файла зад кода наистина се изпълни, но GridView не се обнови.
Предполагам, че трябва да се добави допълнително кодиране във файла с код, за да се направи това.
Ето маркирането за бутона за търсене от един от блоковете asp:Content:
<asp:Content
ID="ContentBody"
ContentPlaceHolderID="BodyPlaceholder"
runat="server">
<% '-- Ajax enable this area so flicker us cut down to a minumum. -- %>
<% '---------------------------------------------------------------- %>
<asp:UpdatePanel
ID="UpdatePanelSummary"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<h1>Classes / Subjects Maintenance</h1>
Class Search:
<asp:TextBox
ID="TextBoxSearch"
runat="server"
Width="207px"
Text="ALL">
</asp:TextBox>
<asp:Button
ID="ButtonSearch"
runat="server"
Text="Search"
OnClick="ButtonSearch_Click" />
<asp:Button
ID="ButtonSearchAll"
runat="server"
Text="Show ALL Classes"
OnClick="ButtonSearchAll_Click"/>
<br />
<asp:Button
ID="ButtonAddNewClass"
runat="server"
Text="Add a New Class to this List" />
<br />
<strong><span class="auto-style1">
<br />
To send an email of this list, enter the email address of whom you wish to send it to then click the envelope.</span></strong>
<br />
<br />
Recipient:
<asp:TextBox ID="TextBoxEmailRecipient" runat="server" Width="203px"></asp:TextBox>
<strong><span class="auto-style1"> </span></strong>
<asp:ImageButton
ID="ImageButtonEmailThisList"
runat="server"
BorderStyle="None"
ImageUrl="~/Images/email1.png"
OnClick="ImageButtonEmailThisList_Click"
ToolTip="Email this List as a report." Height="50px" Width="50px"
/>
<br />
<asp:Label ID="LabelEmailMessage" runat="server" style="font-weight: 700; color: black"></asp:Label>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Това е маркирането на блока asp:Content, който има GridView:
<asp:Content
ID="DetailsBody"
ContentPlaceHolderID="DetailsPlaceholder"
runat="server">
<asp:UpdatePanel
ID="UpdatePanelDetails"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<% '-- GridView (Grid) for summary. -- %>
<% '-- The user chooses a Class from here and details are shown in a DetailsView. -- %>
<% '--------------------------------------------------------------------------------- %>
<asp:GridView
ID="GridViewSummary"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID"
Width="401px"
AllowPaging="True"
PageSize="3">
<Columns>
<asp:BoundField DataField="ClassName" HeaderText="Class / Subject"
SortExpression="ClassName" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Grade" HeaderText="Grade"
SortExpression="Grade" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:CommandField ButtonType="Button" SelectText="Select Class Details"
ShowSelectButton="True"/>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" Mode="NextPreviousFirstLast" NextPageText="Next" PreviousPageText="Previous"/>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Много от контролите са премахнати, така че този код ще бъде по-лесен за следване.
Това е кодирането от кода зад файла, който зарежда данни в GridView, след като потребителят щракне върху бутона за търсене:
Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs)
' Show the schedules the user wants.
'-----------------------------------
GridViewSummary.DataSource = theTableAdapter.GetDataByAllClasses(TextBoxSearch.Text)
GridViewSummary.DataBind()
End Sub