Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 中继器控制装置内的触发按钮_C#_Asp.net - Fatal编程技术网

C# 中继器控制装置内的触发按钮

C# 中继器控制装置内的触发按钮,c#,asp.net,C#,Asp.net,我有一个按钮内的中继器控制,我想禁用它。我试过这样的东西 if (Session["USER_ID"] == null) { //disable download button and } else { //enable download button } 这是我要启用和禁用的按钮btnTEST <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand

我有一个按钮内的中继器控制,我想禁用它。我试过这样的东西

if (Session["USER_ID"] == null)
{
   //disable download button and     
}
else
{
   //enable download button
}
这是我要启用和禁用的按钮
btnTEST

<asp:Repeater ID="Repeater1" runat="server" 
     OnItemCommand="Repeater1_ItemCommand">
   <ItemTemplate>            
         <asp:Image ID="image" ImageUrl='<%# Eval("image_src")%>' runat="server"  />                           
         <asp:Button ID="btnTEST" runat="server" Text="Click Me!" CommandName="testme" Enabled="False" />                          
   </ItemTemplate>
</asp:Repeater>

由于某些原因,它不起作用。

由于该按钮位于中继器控件内,它将动态生成,生成次数与数据源数据绑定中的项目相同。因此,您必须启用/禁用屏幕上的按钮,如下所示:


注:无需禁用,因为默认情况下该按钮处于禁用状态。

因为该按钮位于中继器中。您需要禁用itemdatabound中的按钮。我建议您在代码隐藏中添加如下内容:

protected override void OnInit(EventArgs e)
{
       base.OnInit(e);
       Repeater1.ItemDataBound += (s, ev) =>
       {
           if (ev.Item.ItemType != ListItemType.AlternatingItem && ev.Item.ItemType != ListItemType.Item)
                    return;
             var btnTest= ((System.Web.UI.WebControls.Button) ev.Item.FindControl("btnTEST"));
             btnTest.Enabled = Session["USER_ID"] != null;
        };
 }

它不起作用我注意到它甚至没有进入if语句
if(e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==ListItemType.AlternatingItem){Session[“USER\u ID”].ToString();
但是标签甚至没有显示userid您是否将此方法附加到repeaters ItemDataBound事件中?
这就是我目前拥有的
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Button btn = (Button)e.Item.FindControl("btnTEST");

        if (Session["USER_ID"] != null)
        {
            btn.Enabled = true;
        }
    }

}
protected override void OnInit(EventArgs e)
{
       base.OnInit(e);
       Repeater1.ItemDataBound += (s, ev) =>
       {
           if (ev.Item.ItemType != ListItemType.AlternatingItem && ev.Item.ItemType != ListItemType.Item)
                    return;
             var btnTest= ((System.Web.UI.WebControls.Button) ev.Item.FindControl("btnTEST"));
             btnTest.Enabled = Session["USER_ID"] != null;
        };
 }