本文介绍了在特定的GridView行中禁用/隐藏控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码来在asp.net和gridview内部创建一个gridview我有一个删除按钮。下面的代码工作正常,并显示在所有行中删除。
我想在第一行隐藏/禁用删除按钮。有人可以提供代码部分吗?
< asp:gridview ID =Gridview1runat =server
ShowFooter =trueAutoGenerateColumns =false>
<列>
< asp:BoundField DataField =RowNumberHeaderText =Row Number/>
< asp:TemplateField HeaderText =Cat>
< ItemTemplate>
< asp:TextBox ID =TextBoxCatrunat =serverEnabled =false>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =Delete>
< ItemTemplate>
< asp:LinkButton ID =DeleteItemsGridRowButtonrunat =server>删除< / asp:LinkButton>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:gridview>
解决方案
您可以使用事件。
然后使用 FindControl 方法找到 LinkButton 。
public class Animal
{
public int RowNumber {get;组; }
public string Name {get;组; }
$ b protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
Gridview1.DataSource =新列表<动物>
新的动物{RowNumber = 1,Name =One},
新的动物{RowNumber = 2,Name =Two},
new Animal {RowNumber = 3,Name =Three},
new Animal {RowNumber = 4,Name =Four},
};
Gridview1.DataBind();
}
}
private int _counter;
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
if (_counter == 0)
{
var linkButton = e.Row.FindControl(DeleteItemsGridRowButton)
作为LinkButton;
linkButton.Visible = false;
_counter ++;
}
}
}
I have below code to create a gridview in asp.net and inside gridview I have a delete button. Below code works fine and shows Delete in all rows.I want to hide/ Disable the Delete button in very first row. Can somebody suggest the code part?
<asp:gridview ID="Gridview1" runat="server"
ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Cat">
<ItemTemplate>
<asp:TextBox ID="TextBoxCat" runat="server" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" >
<ItemTemplate>
<asp:LinkButton ID="DeleteItemsGridRowButton" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
解决方案
You can use GridView.RowDataBound Event event.
Then find the LinkButton using FindControl method.
public class Animal
{
public int RowNumber { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridview1.DataSource = new List<Animal>
{
new Animal {RowNumber = 1, Name = "One"},
new Animal {RowNumber = 2, Name = "Two"},
new Animal {RowNumber = 3, Name = "Three"},
new Animal {RowNumber = 4, Name = "Four"},
};
Gridview1.DataBind();
}
}
private int _counter;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (_counter == 0)
{
var linkButton = e.Row.FindControl("DeleteItemsGridRowButton")
as LinkButton;
linkButton.Visible = false;
_counter++;
}
}
}
这篇关于在特定的GridView行中禁用/隐藏控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!