div有两个表 [英] div having two table

查看:83
本文介绍了div有两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个div标签,并在其中有两个表.
一个表中有一个按钮,另一表中有第二个按钮.

我想要的是,当我单击按钮1时,只有表1中的控件应该可见,而当我单击按钮2时,仅按钮2中的控件应该可见.
同样在页面加载时,这两个按钮也应该是唯一可见的.

我应该怎么做?请帮忙...:confused:

为此,我将代码编写为

Hi,

I have a div tag and inside it two tables.
There is one button in one table and a second button in another table.

What I want is that when I click on button one only that control which is present in table one should be visible and when I click on button 2 only that control which is present in button 2 should be visible.
Also on pageload is the only time that the two buttons should be visible.

How should I do this? Please help...:confused:

For that I have written code as

    protected void Page_Load(object sender, EventArgs e)
    {
        //HtmlTableCell c =(HtmlTableCell)trid.FindControl("SAN");
        //c.Visible = true;
        HtmlTableRow h = (HtmlTableRow)tab2.FindControl("tr2id");
        HtmlTableCell c1 = (HtmlTableCell)h.FindControl("AN");
        c1.Visible = false;
    }

    protected void butt_Click(object sender, EventArgs e)
    {
        HtmlTableCell c = (HtmlTableCell)trid.FindControl("SAN");
        c.Visible = true;
        HtmlTableRow h = (HtmlTableRow)tab2.FindControl("tr2id");
        HtmlTableCell c1 = (HtmlTableCell)h.FindControl("AN");
        c1.Visible = false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        HtmlTableCell c = (HtmlTableCell)trid.FindControl("SAN");
        c.Visible = false;

        HtmlTableRow h = (HtmlTableRow)tab2.FindControl("tr2id");
        HtmlTableCell c1 = (HtmlTableCell)h.FindControl("AN");
        c1.Visible = true;
    }
}



这是可行的,但需要更好的代码



this is working but a want a better code

推荐答案

为每个表赋予唯一的ID,以便每个按钮都可以使用getElementById找到它并设置样式以使其消失,例如这个:

Give each table an unique id so each button can find it using getElementById and set the style to make it disappear, like this:

document.getElementById(''table_id'').style = "display:none"; 



祝你好运!



Good luck!


E.F.如果您要使用javascript,Nijboer的答案非常好,但是由于您使用的是背后的代码,并且正在那里处理按钮单击事件,因此无论如何您都在回发表单,因此您可以在表中添加和id或您希望影响的单元以及runat ="server"属性一起更改.然后,您可以在按钮单击事件中引用该控件.
E.F. Nijboer''s answer is excellent if you are going to use javascript, but since you are using code behind and you are handling the button click event there, you are posting back the form anyway, so you could just add and id to the tables or cells you wish to effect change on along with the runat="server" attribute. Then you can just reference the control in your button click events.
protected void butt_Click(object sender, EventArgs e)
{
    objectId1.Visible = true;
    objectId2.Visible = false;
}

protected void Button1_Click(object sender, EventArgs e)
{
    objectId1.Visible = false;
    objectId2.Visible = true;
}


这篇关于div有两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆