user2274204
user2274204

Reputation: 345

Enable/Disable ImageButton in datalist

i need your help to this case i can't find any solution in the web

I have datalist that build like this :

 <ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
    <asp:Button ID="Button1" runat="server" Text="Button" />
<%# DataBinder.Eval(Container.DataItem, "Enabled") %>
    <asp:Button ID="Button2" runat="server" Text="Button" />
<asp:ImageButton ID="ImageButton1" runat="server" />
</ItemTemplate>

I want to enable the button just if the Enabled value that i get from the DB is equal to 1 , i have try to make this code below but didn't success.

     protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
     {
       int EnableDisable =     Convert.ToInt32(((DataRowView)e.Item.DataItem).Row.ItemArray[1]);
       if (EnableDisable != 1) 
       {
          Button BT = e.Item.FindControl("ImageButton1") as Button;
          BT.Enabled = true;
       }}

any idea, can you help me ?

Thank you so much.

Upvotes: 1

Views: 353

Answers (2)

Martin Parenteau
Martin Parenteau

Reputation: 73731

I don't see in your code where you disable the button when necessary. Have you tried this:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
 {
     int EnableDisable = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row.ItemArray[1]);
     ImageButton BT = e.Item.FindControl("ImageButton1") as ImageButton;
     BT.Enabled = (EnableDisable == 1);
 }

Upvotes: 1

Med.Amine.Touil
Med.Amine.Touil

Reputation: 1235

change this line

Button BT = e.Item.FindControl("ImageButton1") as Button;

by

ImageButton BT = e.Item.FindControl("ImageButton1") as ImageButton;

Upvotes: 0

Related Questions