简体   繁体   English

加入状态为真或假时启用或禁用按钮

[英]Button enabling or disabling when the status for joining is true or false

I want the button "Button_Join" to be enabled when the row "join status" in my table is true and disabled when it's false. 我希望当表中的“连接状态”行为true时启用按钮“ Button_Join”,为false时禁用按钮。

Code: 码:

<asp:Repeater ID="RepeaterTeam" runat="server">
 <ItemTemplate>
  <tr>
   <td>
     <asp:Button ID="Button_Join" runat="server" Text="Join" Enabled='<%#Enabled() %>'/>
   </td>
  </tr>
 </ItemTemplate>
</asp:Repeater>

Code behind: 后面的代码:

protected bool Enabled()
{

    if (Session["join status"] == "False")
    {
        return false;
    }
    else
    {
        return true;
    }
}

I dont know how to do it, but that was my guess, but it doesn't seem to work. 我不知道该怎么做,但这是我的猜测,但这似乎行不通。

Is this possible instead of all this code behind? 有可能代替所有这些代码吗?

<asp:Button ID="Button_Join" runat="server" Text="Join" Enabled='<%#Eval ("join status") %>'/>

Set enabled of control in code behind. 在后面的代码中设置启用控制。

 protected void Page_Load(object sender, EventArgs e)
 {
     this.Button_Join.Enabled = Session["join status"] != "False";
 }

UPDATE 更新

Add handler of OnItemDataBound for your repeater 为您的中继器添加OnItemDataBound处理程序

<asp:Repeater runat="server" ID="myRepeater" OnItemDataBound="myRepeater_ItemDataBound">

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Button button = (Button)e.Item.FindControl("Button_Join");
    button.Enabled = Session["join status"] != "False";
}

And better put in session bool instead string. 最好将会话布尔值改为字符串。 You will be able use it this way 您将可以通过这种方式使用它

button.Enabled = (Session["join status"] as bool?) ?? false;

Why not do it in the button's load event? 为什么不在按钮的load事件中这样做呢?

protected void Button_Join_Load(object sender, EventArgs e)
{
   // your logic...
   (sender as Button).Enabled = false;
}

EDIT: 编辑:

If as per your comment the button is in a repeater, use the repeater's ItemDataBound event: 如果根据您的评论,该按钮位于转发器中,请使用转发器的ItemDataBound事件:

protected void Your_Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RepeaterItem ri = e.Item;
    var btn = (ri.FindControl("Button_Join") as Button);
    if (btn != null) btn.Enabled = 
        bool.Parse((Session["join status"] ?? false).ToString()) == true ? true : false;
}

I guess you might have to use Enabled='<%=Enabled() %>' instead of Enabled='<%#Enabled() %>' 我想您可能必须使用Enabled='<%=Enabled() %>'而不是Enabled='<%#Enabled() %>'

<%# %> used for data binding. <%# %>用于数据绑定。 I see that you are not binding any data. 我看到您没有绑定任何数据。 <%= %> is equivalent of Response.Write() <%= %>等效于Response.Write()

More info here ... 更多信息在这里 ...

尝试使用您的启用功能

Enabled='<%#Eval("Enabled")%>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM