英文:
When TextBox's are overlapped, the one defined earlier in the ASPX is not clickable
问题
我有许多控件;根据条件,其中一些变为隐藏,其余的则可见。为了简化,我们假设有两个文本框,txtName
和 txtEmail
,具有相同的x/y坐标,例如:
<div style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtEmail" ClientIDMode="Static" Width="100" runat="server" />
</div>
<div style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtName" ClientIDMode="Static" Width="100" runat="server" />
</div>
有时,我想隐藏 txtEmail
。其他时候,我想隐藏 txtName
。例如,在加载时,我像这样隐藏 txtName
:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtName.Visible = false; // txtName.Style.Add("display", "none"); 同样的结果
txtName.Enabled = false;
txtEmail.Visible = true; // txtEmail.Style.Add("display", "block"); 同样的结果
txtEmail.Enabled = true;
}
}
现在的问题是文本框无法点击。在ASPX文件中,txtEmail
首先出现,然后是txtName
。因此,尽管我隐藏了txtName
,但它阻止了我在txtEmail
上单击鼠标。
如何在不更改 asp:TextBox
顺序的情况下纠正这个问题?
英文:
I have many controls; and based on the condition, some of them become hidden and the rest are visible. To simplify, let’s say there are 2 TextBoxes, txtName
and txtEmail
, with the same x/y coordinates, e.g.:
<div style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtEmail" ClientIDMode="Static" Width="100" runat="server" />
</div>
<div style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtName" ClientIDMode="Static" Width="100" runat="server" />
</div>
Sometimes, I want to hide txtEmail
. Othertimes, I want to hide txtName
. For example, on loading time, I hide txtName
like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtName.Visible = false; // txtName.Style.Add("display", "none"); same result
txtName.Enabled = false;
txtEmail.Visible = true; // txtEmail.Style.Add("display", "block"); same result
txtEmail.Enabled = true;
}
}
Now the problem is the textbox is not clickable. In the ASPX file, txtEmail
appears first, and then txtName
. So although I hide txtName
, it blocks my mouse-click on txtEmail
.
How can I correct this without changing the order of asp:TextBox
-es?
答案1
得分: 1
好的,问题在于你隐藏(和显示)了文本框,但是你有两个div仍然可见,而你最终点击的就是它们。
因此,你实际上不需要禁用文本框控件,只需要隐藏(或显示)它们所在的div即可。请注意,虽然设置style="display:none"通常看起来会产生与SomeControl.Visible = false相同的结果?
不,实际上有很大的区别,因为当你在后台代码中使用并将控件设置为visible = false时?
该控件不会被发送,也不会在客户端渲染。这意味着客户端的JavaScript代码无法选择、使用或修改这些控件。
尽管在隐藏控件的样式和使用visible = false之间存在巨大的区别?
在这里的简单方法是隐藏(或显示)你的两个div,然后一切都会正常。既然你使用了"visible",根据上述,我们实际上不需要启用或禁用文本框控件。(它们甚至不会被渲染和发送到客户端浏览器)。
使用.visible与div是一个更好的方法,因为当浏览器标记被渲染时,你实际上不会有两个div重叠在一起(因为如前所述,任何具有visible = false属性的div或控件都不会被发送到客户端浏览器)。
标记:
<div id="mydiv1" runat="server"
style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtEmail" ClientIDMode="Static" Width="100" runat="server" />
</div>
<div id="mydiv2" runat="server" style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtName" ClientIDMode="Static" Width="100" runat="server" />
</div>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
mydiv1.Visible = false;
mydiv2.Visible = true;
}
}
因此,隐藏(或显示)容器。以上的额外好处是你可以在任何一个div中包含多个控件和标记,因此你的服务器端代码可以通过使用该div控件的.visible属性来显示/隐藏那组控件。
因此,通常可以在隐藏或显示单个控件的几条命令的位置上,将它们分组放在一个div中,将runat="server"标记和"id"标记添加到该div中。
现在你可以在后台用一行代码隐藏或显示那组控件。
英文:
Ok, the issue and problem are that you hiding (and showing) the text box, but the 2 div’s you have are still visible, and they are what you wind up clicking on.
Thus, you don't really need to disable the text box controls, but only hide (or show) the div they reside inside of. And note that while setting style="display:none" often looks to produce the same result as SomeControl.Visible = false?
No, there is a VAST difference, since when you use code behind and set a control visible = false?
The control is not sent, and not rendered client side. That means client-side JavaScript code can't select, use, nor modify such controls.
Regardless of the above HUGE difference between using style to hide a control vs. that of using visible = false?
The simple approach here is to hide (or show) your 2 divs, and you be fine. And since you using "visible" then based on above, we don't need to enable, nor disable the text box controls anyway. (They will not even be rendered and sent to the client-side browser).
Using .visible with the div is a better approach, since then when browser markup is rendered, you actually don’t have 2 divs overlapping each other (since as noted, any div or control with visible = false is not sent to the client-side browser).
Markup:
<div id="mydiv1" runat="server"
style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtEmail" ClientIDMode="Static" Width="100" runat="server" />
</div>
<div id="mydiv2" runat="server" style="position: absolute; top: 55px; left: 5px; width: 120px; height: 20px">
<asp:TextBox ID="txtName" ClientIDMode="Static" Width="100" runat="server" />
</div>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
mydiv1.Visible = false;
mydiv2.Visible = true;
}
}
So, hide (or show) the container. The added bonus of above is you can have multiple controls and markup inside of either div, and thus your server-side code can show/hide that group of controls by using the .visible property of that div control.
And thus, often in place of several commands to hide or show individual controls? Group them inside of a div, add the runat="server" tag to that div along with the "id" tag.
You now can hide or show that group of controls with one line of code behind.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论