如何在单击链接按钮时隐藏文本框控件,其中这两个控件都是网格视图的子控件
本文关键字:控件 两个 视图 网格 链接 按钮 单击 隐藏 文本 | 更新日期: 2023-09-27 18:19:26
我有一个下的网格视图
<asp:GridView ID="dgTask" runat="server" Width="100%"
AutoGenerateColumns="False" onrowdatabound="dgTask_RowDataBound">
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID" ItemStyle-Width="1%" />
<asp:BoundField DataField="TaskName" HeaderText="Task Name" ItemStyle-HorizontalAlign="left"
ItemStyle-Width="10%" />
<asp:BoundField DataField="PriorityName" HeaderText="Priority" ItemStyle-HorizontalAlign="center"
ItemStyle-Width="10%" />
<asp:BoundField DataField="StatusName" HeaderText="Status" ItemStyle-HorizontalAlign="center"
ItemStyle-Width="10%" />
<asp:TemplateField HeaderText="Edit Task" ItemStyle-Width="10%">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="Server" Text="Edit" CommandArgument ='<%# Eval("TaskID") %>' />
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
现在,我要做的是,在linkButton的点击事件中,我需要隐藏该行的文本框控件。
如何做到这一点?
到目前为止,我已经做了
protected void dgTask_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnEdit = (LinkButton)e.Row.Cells[4].FindControl("lnkBtnEdit");
btnEdit.Attributes.Add("onclick", "return Test();");
}
}
感谢
您正走在正确的道路上,只需在调用javascript函数时进行一些小的更改,然后添加如下所示的javascript函数。
protected void dgTask_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnEdit = (LinkButton)e.Row.Cells[4].FindControl("lnkBtnEdit");
TextBox txtId = (TextBox)e.Row.Cells[4].FindControl("txtId");
btnEdit.Attributes.Add("onclick", "return Test("'" + txtId.ClientId + "'");");
}
}
像这样添加Javascript,
function Test(var txtId)
{
var inputtxt = document.getElementById(txtId);
if(inputtxt != null)
{
inputtxt.Attributes.Add("style","display:none;");
}
}
试试这个:
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton b = (LinkButton)sender;
GridViewRow r = (GridViewRow)b.NamingContainer;
((TextBox)(GridView1.Rows[r.RowIndex].Cells[0].FindControl("TextBox1"))).Visible = false;
}