非常快速的问题 - 我一定不能正确看待它,但是有什么理由为什么ASP按钮“btnEdit”在gridview中仍然可见?在RowDatabound上(用于测试)我只是试图隐藏它,计划稍后将if语句放在它周围。
代码隐藏(RowDataBound)
protected void gvEditUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnEdit = (Button)e.Row.FindControl("btnEdit");
btnEdit.Visible = false;
if (e.Row.RowState != null)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
CheckBox cbAdmin = (CheckBox)e.Row.FindControl("cbAdmin");
DataRowView dr = e.Row.DataItem as DataRowView;
if (Convert.ToBoolean(dr["admin"] = true))
{
cbAdmin.Checked = true;
}
else
{
cbAdmin.Checked = false;
}
}
}
}
}
ASPX页面(GridView)
<asp:GridView ID="gvEditUser" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowCancelingEdit="gvEditUser_RowCancelingEdit" OnRowEditing="gvEditUser_RowEditing" OnRowCommand="gvEditUser_RowCommand" OnRowDeleting="gvEditUser_RowDeleting" OnPageIndexChanging="gvEditUser_PageIndexChanging" CellPadding="4" GridLines="None" CssClass="size12_text" PageSize="10">
<Columns>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="lbUsername" runat="server" Text='<%# Eval("username") %>' ></asp:Label>
<asp:Label ID="lbUserID" runat="server" Text='<%# Eval("userid") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Full Name">
<ItemTemplate>
<asp:Label ID="lbFullName" runat="server" Text='<%# Eval("fullname") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<a href="mailto:<%# Eval("email") %>">Email</a>
<asp:Label ID="lbEmail" runat="server" Visible="false" Text='<%# Eval("email") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lbAddress" runat="server" Text='<%# Eval("housenumber")!=DBNull.Value ? Eval("housetype").ToString() == "0" ? Eval("HouseNumber").ToString() + " " + Eval("Address01").ToString() + " " + Eval("Address02").ToString() + " " + Eval("StreetType").ToString() + "<br />" + Eval("Suburb").ToString() + ", " + Eval("State").ToString() + " " + Eval("Postcode").ToString() + "<br />" + Eval("Country").ToString() : Eval("Address01").ToString() + " " + Eval("HouseNumber").ToString() + " " + Eval("Address02").ToString() + " " + Eval("StreetType").ToString() + "<br />" + Eval("Suburb").ToString() + ", " + Eval("State").ToString() + " " + Eval("Postcode").ToString() + "<br />" + Eval("Country").ToString() : "<div class=\"size11_text\">No Address in system.</div>"%>'></asp:Label>
<asp:Label ID="lbAddressRaw" runat="server" Text='<%# Eval("housenumber")!=DBNull.Value ? Eval("housetype").ToString() == "0" ? Eval("HouseNumber").ToString() + " " + Eval("Address01").ToString() + " " + (Eval("Address02")!=DBNull.Value ? Eval("Address02").ToString() + " " : "") + Eval("StreetType").ToString() : Eval("Address01").ToString() + " " + Eval("HouseNumber").ToString() + " " + Eval("Address02").ToString() + " " + Eval("StreetType").ToString() : ""%>' Visible="false"></asp:Label>
<asp:Label ID="lbHouseType" runat="server" Text='<%# Eval("housetype") %>' Visible="false"></asp:Label>
<asp:Label ID="lbHouseNumber" runat="server" Text='<%# Eval("housenumber") %>' Visible="false"></asp:Label>
<asp:Label ID="lbAddress01" runat="server" Text='<%# Eval("address01") %>' Visible="false"></asp:Label>
<asp:Label ID="lbAddress02" runat="server" Text='<%# Eval("address02") %>' Visible="false"></asp:Label>
<asp:Label ID="lbStreetType" runat="server" Text='<%# Eval("streettype") %>' Visible="false"></asp:Label>
<asp:Label ID="lbSuburb" runat="server" Text='<%# Eval("suburb") %>' Visible="false"></asp:Label>
<asp:Label ID="lbState" runat="server" Text='<%# Eval("state") %>' Visible="false"></asp:Label>
<asp:Label ID="lbPostcode" runat="server" Text='<%# Eval("postcode") %>' Visible="false"></asp:Label>
<asp:Label ID="lbCountry" runat="server" Text='<%# Eval("country") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Home Phone">
<ItemTemplate>
<asp:Label ID="lbTelephone" runat="server" Text='<%# Eval("telephone") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Work Phone">
<ItemTemplate>
<asp:Label ID="lbWorkPhone" runat="server" Text='<%# Eval("workphone") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:Label ID="lbMobile" runat="server" Text='<%# Eval("mobile") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shirt Size">
<ItemTemplate>
<asp:Label ID="lbShirtSize" runat="server" Text='<%# Eval("shirtsize") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pant Size">
<ItemTemplate>
<asp:Label ID="lbPantSize" runat="server" Text='<%# Eval("pantsize") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Admin">
<ItemTemplate>
<asp:CheckBox ID="cbAdmin" runat="server" Checked='<%# Convert.ToBoolean(Eval("admin")) %>' Enabled="false" />
<asp:Label ID="lbAdmin" runat="server" Text='<%# Eval("admin") %>' Visible="false" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Login">
<ItemTemplate>
<asp:Label ID="lbLastLogin" runat="server" Text='<%# Eval("lastlogin") %>' CssClass="size11_text_blurb"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="100px">
<ItemTemplate>
<asp:Button ID="btnPasswordReset" runat="server" CssClass="txtbuttonsmall" CommandName="ResetPassword" Text="Reset Password" OnClientClick='<%# Eval("username","return confirm(\"Reset password for {0}?\");") %>'></asp:Button><br />
<asp:Button ID="btnEdit" runat="server" CssClass="txtbuttonsmall" CommandName="Edit" Text="Edit User"></asp:Button><br />
<asp:Button ID="btnDelete" runat="server" CssClass="txtbuttonsmall" CommandName="Delete" Text="Delete User" OnClientClick='<%# Eval("username","return confirm(\"Delete user {0}?\");") %>'></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel Edit" CssClass="txtbutton"></asp:Button>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#d8e6f0" />
<EditRowStyle BackColor="#a3cbe8" />
<HeaderStyle BackColor="#032035" ForeColor="White" CssClass="size12_text" />
<PagerStyle BackColor="#032035" ForeColor="White" HorizontalAlign="Center" CssClass="size12_text" />
<RowStyle BackColor="#f9fbfc" />
</asp:GridView>
干杯,
遄
答案 0 :(得分:2)
我忘了在gridview上订阅OnRowDataBound。不是一个聪明的家伙......
答案 1 :(得分:0)
绑定上的隐藏按钮(可以使用偶数条件)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="grd_finantial_year_RowCommand" DataKeyNames="srno" >
<Columns>
<asp:TemplateField HeaderText="Change Status">
<ItemTemplate>
<asp:LinkButton ID="btnststact" Visible='<%#BtnVisibility(Eval("active").ToString(),"StatusACT")%>' Text="ACTIVATE" runat="server" CommandName="StatusACT" CommandArgument='<%#Eval("srno")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
只需在具有公共访问级别的C#中添加此功能
public Boolean BtnVisibility(string StatusNo, string ButtonName) { // 0 means not active, 1 means activated
if (ButtonName == "StatusACT") {
if (StatusNo == "0") {//not active
return true;
}
else if (StatusNo == "1") //active
{
return false;
}
}
return true;
}