在javascript中隐藏文本框

在javascript中隐藏文本框,javascript,asp.net,gridview,Javascript,Asp.net,Gridview,尝试使用以下脚本隐藏文本框: function EnableTextBox(clientId2, clientId1) { var label = eval("document.getElementById('" + clientId2 + "')"); var textBox = eval("document.getElementById('" + clientId1 + "')"); if (label.Visible == true) {

尝试使用以下脚本隐藏文本框:

function EnableTextBox(clientId2, clientId1) {

        var label = eval("document.getElementById('" + clientId2 + "')");
        var textBox = eval("document.getElementById('" + clientId1 + "')");

        if (label.Visible == true) {
            label.Visible = false;
            textBox.Visible = true;
        }
        else {
            label.Visible = true;
            textBox.Visible = false;
        }
    }
文本框与标签位于同一单元格中,事件在gridview\u ondatabound事件期间在代码隐藏中创建:

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblNotes = (Label)(e.Row.Cells[1].Controls[1]);
            TextBox tbNotes = (TextBox)(e.Row.Cells[1].Controls[3]);

            if (lblNotes != null)
            {

                lblNotes.Attributes.Add("methodstring", string.Format("EnableTextBox('{0}', '{1}')", lblNotes.ClientID, tbNotes.ClientID));
                lblNotes.Attributes.Add("onClick", "eval(this.methodstring)");
            }
        }
我还没有解决的问题是,脚本中的变量tbNotes仍然处于null状态。有什么建议吗


谢谢

使用
e.Row.FindControl(“ID\u OF\u CONTROL”)
而不是尝试查看子集合

if (e.Row.RowType == DataControlRowType.DataRow)
{
     Label lblNotes = e.Row.FindControl("lblNotes") AS Label; // Proper Id of the control
     TextBox tbNotes = e.Row.FindControl("tbNotes ") AS TextBox; // Proper Id of the control

     if (lblNotes != null && tbNotes !=null)
     {
         lblNotes.Attributes.Add("methodstring", string.Format("EnableTextBox('{0}', '{1}')", lblNotes.ClientID, tbNotes.ClientID));
         lblNotes.Attributes.Add("onClick", "eval(this.methodstring)");
     }
 }

使用
e.Row.FindControl(“ID\u OF\u CONTROL”)
而不是尝试遍历子集合

if (e.Row.RowType == DataControlRowType.DataRow)
{
     Label lblNotes = e.Row.FindControl("lblNotes") AS Label; // Proper Id of the control
     TextBox tbNotes = e.Row.FindControl("tbNotes ") AS TextBox; // Proper Id of the control

     if (lblNotes != null && tbNotes !=null)
     {
         lblNotes.Attributes.Add("methodstring", string.Format("EnableTextBox('{0}', '{1}')", lblNotes.ClientID, tbNotes.ClientID));
         lblNotes.Attributes.Add("onClick", "eval(this.methodstring)");
     }
 }

使用
e.Row.FindControl(“ID\u OF\u CONTROL”)
而不是尝试遍历子集合

if (e.Row.RowType == DataControlRowType.DataRow)
{
     Label lblNotes = e.Row.FindControl("lblNotes") AS Label; // Proper Id of the control
     TextBox tbNotes = e.Row.FindControl("tbNotes ") AS TextBox; // Proper Id of the control

     if (lblNotes != null && tbNotes !=null)
     {
         lblNotes.Attributes.Add("methodstring", string.Format("EnableTextBox('{0}', '{1}')", lblNotes.ClientID, tbNotes.ClientID));
         lblNotes.Attributes.Add("onClick", "eval(this.methodstring)");
     }
 }

使用
e.Row.FindControl(“ID\u OF\u CONTROL”)
而不是尝试遍历子集合

if (e.Row.RowType == DataControlRowType.DataRow)
{
     Label lblNotes = e.Row.FindControl("lblNotes") AS Label; // Proper Id of the control
     TextBox tbNotes = e.Row.FindControl("tbNotes ") AS TextBox; // Proper Id of the control

     if (lblNotes != null && tbNotes !=null)
     {
         lblNotes.Attributes.Add("methodstring", string.Format("EnableTextBox('{0}', '{1}')", lblNotes.ClientID, tbNotes.ClientID));
         lblNotes.Attributes.Add("onClick", "eval(this.methodstring)");
     }
 }

您就快到了,只需使用
style.display
即可显示/隐藏元素

在下面的代码中,如果单击标签,将显示一个文本框并隐藏标签

根据你的逻辑,一旦标签被隐藏,我不知道如何显示出来

ASPX

功能启用文本框(标签,文本框){
var lbl=document.getElementById(标签);
var txt=document.getElementById(文本框);
如果(lbl.style.display==“”| | lbl.style.display==“块”){
lbl.style.display=“无”;
txt.style.display=“块”;
}否则{
lbl.style.display=“块”;
txt.style.display=“无”;
}
}
代码隐藏
protected override void OnLoad(事件参数e)
{
如果(!IsPostBack)
{
GridView1.DataSource=新列表
{
新事物{Id=1,Notes=“One”},
新事物{Id=2,Notes=“Two”},
};
GridView1.DataBind();
}
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var notesLabel=e.Row.FindControl(“notesLabel”)作为标签;
var notesTextBox=e.Row.FindControl(“notesTextBox”)作为文本框;
notesLabel.Attributes.Add(“onclick”,
Format(“enableTextBox('{0}','{1}')”,
notesLabel.ClientID,notesTextBox.ClientID));
}
}
公开课
{
公共字符串注释{get;set;}
公共int Id{get;set;}
}

注意:jQuery可能会简单得多,但它不在范围之内。

您几乎做到了,只需使用
样式。显示
即可显示/隐藏元素

在下面的代码中,如果单击标签,将显示一个文本框并隐藏标签

根据你的逻辑,一旦标签被隐藏,我不知道如何显示出来

ASPX

功能启用文本框(标签,文本框){
var lbl=document.getElementById(标签);
var txt=document.getElementById(文本框);
如果(lbl.style.display==“”| | lbl.style.display==“块”){
lbl.style.display=“无”;
txt.style.display=“块”;
}否则{
lbl.style.display=“块”;
txt.style.display=“无”;
}
}
代码隐藏
protected override void OnLoad(事件参数e)
{
如果(!IsPostBack)
{
GridView1.DataSource=新列表
{
新事物{Id=1,Notes=“One”},
新事物{Id=2,Notes=“Two”},
};
GridView1.DataBind();
}
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var notesLabel=e.Row.FindControl(“notesLabel”)作为标签;
var notesTextBox=e.Row.FindControl(“notesTextBox”)作为文本框;
notesLabel.Attributes.Add(“onclick”,
Format(“enableTextBox('{0}','{1}')”,
notesLabel.ClientID,notesTextBox.ClientID));
}
}
公开课
{
公共字符串注释{get;set;}
公共int Id{get;set;}
}

注意:jQuery可能会简单得多,但它不在范围之内。

您几乎做到了,只需使用
样式。显示
即可显示/隐藏元素

在下面的代码中,如果单击标签,将显示一个文本框并隐藏标签

根据你的逻辑,一旦标签被隐藏,我不知道如何显示出来

ASPX

功能启用文本框(标签,文本框){
var lbl=document.getElementById(标签);
var txt=document.getElementById(文本框);
如果(lbl.style.display==“”| | lbl.style.display==“块”){
lbl.style.display=“无”;
txt.style.display=“块”;
}否则{
lbl.style.display=“块”;
txt.style.display=“无”;
}
}
代码隐藏
protected override void OnLoad(事件参数e)
{
如果(!IsPostBack)
{
GridView1.DataSource=新列表
{
新事物{Id=1,Notes=“One”},
新事物{Id=2,Notes=“Two”},
};
GridView1.DataBind();
}
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var notesLabel=e.Row.FindControl(“notesLabel”)作为标签;
var notesTextBox=e.Row.FindControl(“notesTextBox”)作为文本框;
notesLabel.Attributes.Add(“onclick”,
Format(“enableTextBox('{0}','{1}')”,
notesLabel.ClientID,notesTextBox.ClientID));
}
}
公开课
{
公共弦乐