[英]Button enabling or disabling when the status for joining is true or false
我希望當表中的“連接狀態”行為true時啟用按鈕“ Button_Join”,為false時禁用按鈕。
<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>
protected bool Enabled()
{
if (Session["join status"] == "False")
{
return false;
}
else
{
return true;
}
}
我不知道該怎么做,但這是我的猜測,但這似乎行不通。
有可能代替所有這些代碼嗎?
<asp:Button ID="Button_Join" runat="server" Text="Join" Enabled='<%#Eval ("join status") %>'/>
在后面的代碼中設置啟用控制。
protected void Page_Load(object sender, EventArgs e)
{
this.Button_Join.Enabled = Session["join status"] != "False";
}
更新
為您的中繼器添加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";
}
最好將會話布爾值改為字符串。 您將可以通過這種方式使用它
button.Enabled = (Session["join status"] as bool?) ?? false;
為什么不在按鈕的load事件中這樣做呢?
protected void Button_Join_Load(object sender, EventArgs e)
{
// your logic...
(sender as Button).Enabled = false;
}
編輯:
如果根據您的評論,該按鈕位於轉發器中,請使用轉發器的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;
}
我想您可能必須使用Enabled='<%=Enabled() %>'
而不是Enabled='<%#Enabled() %>'
<%# %>
用於數據綁定。 我看到您沒有綁定任何數據。 <%= %>
等效於Response.Write()
更多信息在這里 ...
嘗試使用您的啟用功能
Enabled='<%#Eval("Enabled")%>'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.