我有一個使用模板字段的DetailsView。我希望發生的情況是,當字段值爲「FTP」時,它將顯示與FTP相關的字段,否則顯示與SFTP相關的字段。如何動態地隱藏DetailsView中的模板字段
我已閱讀並且回答here。但是,它似乎並沒有工作。我試圖改變變量之前和之後我databind我的DetailsView。有人能指出我做錯了什麼嗎?
另外,我需要在這個頁面上單獨使用大約20個字段來完成此操作,所以我試圖避免使用FindControl()的複雜循環結構。
這是我的一些代碼。
<ItemTemplate>
<asp:Label ID="REPRESENTATION_TYPELBL" runat="server" Visible='<%# showFTP %>'
Text='<%# Bind("REPRESENTATION_TYPE") %>'></asp:Label>
</ItemTemplate>
代碼背後:
protected bool showFTP = false; // Tried initializing as class variable
protected bool showSFTP; // and also not initializing
protected void Page_Load(object sender, EventArgs e)
{
ChooseFTPDisplay("SFTP"); // Tried setting it in Page_Load and not in Page_Load
}
protected void FTPGetBind()
{
if (FTPLookupDDL.SelectedIndex != 0)
{
DataTable tbl = BL.GetFTPRow("FTPLookUpID"); // data retrieval
//ChooseFTPDisplay((string)tbl.Rows[0]["FTP_MODE"]); // tried here
FTPGetView.DataSource = tbl;
FTPGetView.DataBind();
ChooseFTPDisplay((string)tbl.Rows[0]["FTP_MODE"]); // tried here
}
}
private void ChooseFTPDisplay(string mode)
{
if (mode == "FTP")
{
showFTP = true;
showSFTP = false;
}
else // Could be null or "SFTP"
{
showFTP = false;
showSFTP = true;
}
}
編輯
現在我已經試過這還有:
<ItemTemplate>
<asp:Label ID="REPRESENTATION_TYPE" runat="server" Visible='<%# (string)Eval("FTP_MODE") == "FTP" ? true: false%>'
Text='<%# Bind("REPRESENTATION_TYPE") %>'></asp:Label>
</ItemTemplate>
UPDATE
我沒有提供足夠的信息。我想隱藏整個Field。包括標題,項目/編輯/插入模板和任何子控件。我上面的內容隱藏了單個控件而不是整個字段。 不幸的是,我不能做到這一點:
<TemplateField Visible='<%# boolValue %>' >...Stuff...</TemplateField>
,因爲模板列不綁定。
基本上,我想等價的:
DetailsView1.Fields[15].Visible = false;// 15 is the index of the field I want hidden
以最有效的和可擴展的方式我可以做到這一點。這種形式在未來幾乎肯定會發生變化,我不希望有人必須回頭找出需要動態顯示或隱藏的所有20多個字段的索引。
什麼意思是「它似乎並沒有工作」?你會得到什麼結果? – ConnorsFan
它只顯示所有的字段,不管布爾值如何。 –