开发者

How to set enable=false to a item template inside a grid in asp.net

开发者 https://www.devze.com 2023-04-09 13:49 出处:网络
I want to set enable = false according to the role I have an if statement to check for the role page load() {

I want to set enable = false according to the role

I have an if statement to check for the role

page load() {
    if(role == "something")
    {
        // I want to set imgbtn.enabled = false;
    }
}

How to do that.

<Columns><asp:TemplateField HeaderText="Edit Controls" ItemStyle-Width="15%">
       <ItemTemplate>
       <asp:ImageButton ID="imgbtn" ImageUrl="Styles/Images/Edit.jpg" runat="server" OnClick="imgbtn_GroupEditClick" ToolTip="Edit Group" />
       <asp:Image开发者_JS百科Button ID="img_Send" ImageUrl="Styles/Images/Message.jpg" Enabled="True"
   runat="server" PostBackUrl='<%# Eval("GroupName", "SendMessage.aspx?GroupName={0}") %>'
                                ToolTip="Create Message"></asp:ImageButton>


</ItemTemplate>
     </asp:TemplateField>
      <asp:BoundField DataField="GroupID" ItemStyle-Width="0%" HeaderText="GroupID" />
      <asp:BoundField DataField="GroupName" ItemStyle-Width="20%" HeaderText="GroupName" />
     </columns>

I am doing databinding for the grid


You can handle the RowDataBound Grid's event

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
       //Or you can use e.Row.Cells[0].FindControl("ur control id");
       foreach (object  c in e.Row.Cells[0].Controls)
       {
           ImageButton btn = c as ImageButton;
           if (c != null && role == "something")
           {
               //Do your logic here
           }
       }
  }


Handle the RowDataBound event on the GridView like this:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var btn = e.Row.FindControl("imgbtn") as Button;
    btn.Enabled = role == "something";
}


Maybe you can use a databinding expression and do it in the ASPX markup:

<asp:ImageButton ID="imgbtn" Enabled='<%# role == "something" %>' ... />


If you have one or more controls on the page you wish to enable or disable based on a role, you could create a public property in your page/control like so:

public bool BelongsToRoleA { get; set; }

Set its value based on the role

If(role==”something”)
{
    BelongsToRoleA = true;
}else
{
    BelongsToRoleA = false;
}

And then reference it in your markup like so:

<asp:ImageButton ID="imgbtn" Enabled='<%# BelongsToRoleA %>' ...


You can also do like this:-

set on your .aspx.cs page

public string role { set; get; }

and in design part replace your code with this

<Columns>
            <asp:TemplateField HeaderText="Edit Controls" ItemStyle-Width="15%">
                <ItemTemplate>
                <%if (role == "Something")
                  { %>
                    <asp:ImageButton ID="imgbtn" ImageUrl="Styles/Images/Edit.jpg" runat="server" OnClick="imgbtn_GroupEditClick"
                        ToolTip="Edit Group" Enabled="false" />
                        <%}
                  else
                  { %>
                          <asp:ImageButton ID="ImageButton1" ImageUrl="Styles/Images/Edit.jpg" runat="server" OnClick="imgbtn_GroupEditClick"
                        ToolTip="Edit Group" Enabled="true" />
                        <%} %>
                    <asp:ImageButton ID="img_Send" ImageUrl="Styles/Images/Message.jpg" Enabled="True"
                        runat="server" PostBackUrl='<%# Eval("GroupName", "SendMessage.aspx?GroupName={0}") %>'
                        ToolTip="Create Message"></asp:ImageButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="GroupID" ItemStyle-Width="0%" HeaderText="GroupID" />
            <asp:BoundField DataField="GroupName" ItemStyle-Width="20%" HeaderText="GroupName" />
        </Columns>
0

精彩评论

暂无评论...
验证码 换一张
取 消