如何以编程方式确定在 EditItemTemplate 中使用哪个控件? (ASP.NET)

发布于 2024-09-12 07:57:58 字数 778 浏览 0 评论 0原文

在我的 ASP.NET 应用程序中,我有一个 GridView。对于此 GridView 中的特定字段,我添加了带有 DropDownList 的 EditItemTemplate。但是,如果该字段的值为“X”,那么我只想显示一个标签而不是 DropDownList。那么我如何以编程方式检查字段值,然后决定显示哪个控件?

这是我的 EditItemTemplate:

<EditItemTemplate>

<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
    DataSourceID="ODSTechLvl" DataTextField="Level_Name"
    DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'>
</asp:DropDownList>

</EditItemTemplate>

如果 Level_ID 的值为“X”,那么我想使用:

<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'></asp:Label>

而不是 DropDownList。

我尝试在 DropDownList 之前嵌入 if 语句来检查 Eval("Level_ID"),但这似乎不起作用。有什么想法吗?

In my ASP.NET application, I have a GridView. For a particular field in this GridView, I've added an EditItemTemplate with a DropDownList. However, if the value of the field is "X", then I want to just display a label instead of the DropDownList. So how can I programatically check the field value, then decide which control to display?

Here is my EditItemTemplate:

<EditItemTemplate>

<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
    DataSourceID="ODSTechLvl" DataTextField="Level_Name"
    DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'>
</asp:DropDownList>

</EditItemTemplate>

If the value of Level_ID is "X", then I want to use:

<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'></asp:Label>

instead of the DropDownList.

I tried embedding an if statement before the DropDownList to check Eval("Level_ID"), but that doesn't seem to work. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-09-19 07:57:58

试试这个:

<EditItemTemplate>

<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
    DataSourceID="ODSTechLvl" DataTextField="Level_Name"
    DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'
    Visible='<%# Eval("Level_ID") != "X" %>'>
</asp:DropDownList>

<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'
    Visible='<%# Eval("Level_ID") == "X" %>'></asp:Label>

</EditItemTemplate>

Try this:

<EditItemTemplate>

<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
    DataSourceID="ODSTechLvl" DataTextField="Level_Name"
    DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'
    Visible='<%# Eval("Level_ID") != "X" %>'>
</asp:DropDownList>

<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'
    Visible='<%# Eval("Level_ID") == "X" %>'></asp:Label>

</EditItemTemplate>
倥絔 2024-09-19 07:57:58

这是适用于 ASP.Net 的东西。

您可以创建 RowDataBound 事件并隐藏标签或 DropDownList

<asp:GridView id="thingsGrid" runat="server" OnRowDataBound="thingsGrid_RowDataBound"

... >
...

并且在后面的代码中:

protected void thingsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var boundData = e.Row.DataItem;
            ...
            if (boundDataMeetsCondition)
            {
                e.Row.Cells[4].FindControl("editThingDropDownList").Visible = false;
                e.Row.Cells[4].FindControl("editThingLabel").Visible = true;//*
            }
            else
            {
                ...    
            }
        }
}

*注意这不太理想,因为它对单元格索引进行硬编码,并且控件的 ID 是一个字符串,直到运行时才会检查。在 ASP.NET MVC 中有很多更优雅的方法来解决这个问题。

OnRowDataBound 是一把大锤,它可以让您完全访问网格、页面方法和整个应用程序。在非常简单的场景中,您也可以内联执行此操作,而无需涉及代码隐藏。

<asp:Label ID="Label1" runat="server" Visible='<%# Convert.ToBoolean(Eval("BooleanPropertyInData"))%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>                           

或者

<asp:Label ID="Label1" runat="server" Visible='<%# Eval("PropertyInData").ToString()=="specialValue"%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>  

在第一个内联方法中,您的数据源必须公开这样的属性,而在第二种方法中,您将specialValue业务逻辑硬编码到您的演示文稿中,这也很丑陋,并且会导致可维护性问题。

Here is something that will work for ASP.Net.

You could create an RowDataBound event and hide the label or the DropDownList

<asp:GridView id="thingsGrid" runat="server" OnRowDataBound="thingsGrid_RowDataBound"

... >
...

and in your code behind:

protected void thingsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var boundData = e.Row.DataItem;
            ...
            if (boundDataMeetsCondition)
            {
                e.Row.Cells[4].FindControl("editThingDropDownList").Visible = false;
                e.Row.Cells[4].FindControl("editThingLabel").Visible = true;//*
            }
            else
            {
                ...    
            }
        }
}

*note this is less than ideal because it hard codes the cell index, and the ID of the controls is a string that wont be checked until runtime. There are much more elegant ways to solve this problem in asp.net mvc.

the OnRowDataBound is a sledge hammer that will give you full access to your grid, page methods, and your entire application. In a very simple scenario you could also do it inline without involving the codebehind.

<asp:Label ID="Label1" runat="server" Visible='<%# Convert.ToBoolean(Eval("BooleanPropertyInData"))%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>                           

or

<asp:Label ID="Label1" runat="server" Visible='<%# Eval("PropertyInData").ToString()=="specialValue"%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>  

in the first inline approach, your data source has to expose such a property, and in the second you are hard coding your specialValue business logic into your presentation, which is also ugly and will lead to problems with maintainability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文