简体   繁体   中英

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.

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

<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?

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:

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() %>'

<%# %> used for data binding. I see that you are not binding any data. <%= %> is equivalent of Response.Write()

More info here ...

尝试使用您的启用功能

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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