我是 ASP.NET webforms 和 C# 的新手。我想創建一個簡單的 Web 應用程式,該應用程式具有從 XML 檔案中搜索特定學生姓名的功能。Web 應用程式有一個文本框,我可以在其中輸入學生姓名或 ID,然后單擊提交按鈕,它應該從 XML 中檢索學生的資料。我如何使這個搜索功能從 XML 格式作業?用戶收到的資料應該是獨立的,而不是 gridview 格式,因此我可以將它們設計為看起來像畢業證書。
這是我制作的示例:

我使用通配符搜索名稱 - 我們可以假設只匹配說開始,但你可以說搜索“B”,你會得到這個:

當然,如果你輸入一個數字,那我就按ID搜索,然后說這個

我想我們應該在 GV 中添加一個“無資料行,像這樣說:
<asp:GridView ID="GridView1" runat="server" CssClass="table" Width="40%">
<EmptyDataTemplate>
<h2>No data found</h2>
</EmptyDataTemplate>
</asp:GridView>

但是,如前所述,您不需要網格格式的結果。
因此,我們可以將 GV 更改為中繼器或資料串列。
讓我們使用資料串列。
所以,我們的標記現在變成了這樣:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" >
</asp:DetailsView>
代碼保持不變,為此執行:
DetailsView1.DataSource = MyTable;
DetailsView1.DataBind();
And we now have the results in a FORM type of layout:

So be it a grid, or even just filling out controls on a page, the approach is quite much the same. There certainly no limits, or reason to use a grid for the results.
You also don't even have to use a auto bind control (say list details list).
You can simple drop in controls and lay them out anyway you want.
Say, like this:
<div style="border-style:solid;color:black;width:300px;float:left">
<div style="padding:5px;text-align:right">
<p>Student ID: <asp:TextBox ID="ID" runat="server" /></p>
<p>Student Name: <asp:TextBox ID="Student_Name" runat="server" /></p>
<p>Honours: <asp:TextBox ID="Honours" runat="server" /></p>
<p>Book Price: <asp:TextBox ID="Book_Price" runat="server" /></p>
<p>Program: <asp:TextBox ID="Programme" runat="server" /></p>
</div>
</div>
So above are just simple controls dropped into the page.
And now our code to fill out above would be:
(same as before, but now in place of a grid bind, or data list bind, we just use code to fill out the controls like this:
if (MyTable.DefaultView.Count > 0)
{
// we have a match - display it
DataRow OneRow = MyTable.DefaultView.ToTable().Rows[0];
ID.Text = OneRow["ID"].ToString();
Student_Name.Text = OneRow["STudent_Name"].ToString();
Honours.Text = OneRow["Honours"].ToString();
Book_Price.Text = OneRow["Book_Price"].ToString();
Programme.Text = OneRow["Programme"].ToString();
}
And the result is now this:

所以將結果發送到網格,或者只是一些代碼實際上是完全相同的程序。事實上,如您所見,我們將 Grid 視圖換成了 Details 視圖。換句話說,只需更改 2 行代碼,我們就有了一個詳細資訊視圖來代替 gridview。
但是,如前所述,您顯然希望以您想要的任何方式布置最終結果 - 不需要網格,甚至不需要內置詳細資訊視圖。
正如您在上一個示例中看到的那樣 - 將過濾器的結果抓取/獲取到一個漂亮的簡單 DataRow 物件中是一件簡單的事情,然后我們只需撰寫我們放置在頁面上的控制元件的設定。我擁有的示例布局當然可以是您能想到的任何型別的布局。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367133.html
上一篇:如何解決端點之間的沖突?
