ASP.NET:显示/隐藏单选按钮列表项编程 [英] ASP.NET: Show/hide radio button list items programmatically

查看:152
本文介绍了ASP.NET:显示/隐藏单选按钮列表项编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何做到以下几点会直觉地做,如果它的工作是什么(想象一下 useGreek() useNato()是将每个加载或回传一次协商状态):

I need to know how to do what the following would intuitively do if it worked (imagine useGreek() and useNato() to be states that would be consulted once per load or postback):

<asp:radioButtonList id="rbl" runat="server" autoPostBack="true">
    <asp:listItem value="alpha" text="Alpha" />
    <% if(useGreek()) { %>
        <asp:listItem value="beta" text="Beta" />
        <asp:listItem value="gamma" text="Gamma" />
    <% } else if(useNato()) { %>
        <asp:listItem value="bravo" text="Bravo" />
        <asp:listItem value="charlie" text="Charlie" />
    <% } %>
    <asp:listItem value="delta" text="Delta" />
</asp:radioButtonList>

(它会已经很明显,我通常不要求写为IIS)。

(It will already be apparent that I'm not usually asked to write for IIS.)

总之,ASP.NET不喜欢列表项code交错,所以这是一个不走。我想,有一些C#为基础的办法以某种方式处理这个问题,但我一直没有运气现在正试图几天。

Anyway, ASP.NET doesn't like code interleaved with list items, so this is a no-go. I imagine that there's some C#-based way to handle this somehow, but I've been trying for a few days now with no luck.

此外,仅仅是明确的,我在这里寻求一个服务器端解决方案。我精通使用jQuery,但我们试图让大多数这种特殊形式的处理过的客户。

Also, just to be clear, I'm seeking a server-side solution here. I'm well-versed with jQuery, but we're trying to keep most of the processing of this particular form off the client.

谢谢,方上。

推荐答案

没有C#,但我想你明白我的意思是:

No C# but i think you understand what i mean:

在codebehind的Page_Load中:

in codebehind's page_load:

If Not IsPostBack Then
    Me.rbl.Items.Add(New ListItem("Alpha", "alpha"))
    If (useGreek()) Then
        Me.rbl.Items.Add(New ListItem("Beta", "beta"))
        Me.rbl.Items.Add(New ListItem("Gamma", "gamma"))
    ElseIf (useNato()) Then
        Me.rbl.Items.Add(New ListItem("Bravo", "bravo"))
        Me.rbl.Items.Add(New ListItem("Charlie", "charlie"))
    End If
    Me.rbl.Items.Add(New ListItem("Delta", "delta"))
End If

如果你要检查每一个回传,因为国家可以改变快,你可以​​在 Me.rbl.Items.Clear()添加到顶部并取出回发检查。

If you have to check on every postback because the state could change fast, you could add an Me.rbl.Items.Clear() to the top and remove the PostBack-check.

编辑:
C#

if (!IsPostBack) {
    this.rbl.Items.Add(new ListItem("Alpha", "alpha"));
    if ((useGreek())) {
        this.rbl.Items.Add(new ListItem("Beta", "beta"));
        this.rbl.Items.Add(new ListItem("Gamma", "gamma"));
    } else if ((useNato())) {
        this.rbl.Items.Add(new ListItem("Bravo", "bravo"));
        this.rbl.Items.Add(new ListItem("Charlie", "charlie"));
    }
    this.rbl.Items.Add(new ListItem("Delta", "delta"));
}

由于我不知道,如果你已经知道了codebehind模型,看看下面的链接:
MSDN:codebehind和编译在ASP.NET 2.0中

Because i'm not sure if you already know the codebehind model, have a look at following link: MSDN: Codebehind and Compilation in ASP.NET 2.0

这篇关于ASP.NET:显示/隐藏单选按钮列表项编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆