处理多个用户控件
本文关键字:控件 用户 处理 | 更新日期: 2023-09-27 18:06:09
我有一个网页包含UserControl重复2次具有相同的功能。我想在第一个UserControl中禁用文本框,但它在第二个UserControl中被禁用。
如何检查?
<script type="text/javascript">
function confirmCallBackFn(arg)
{
if (arg == true)
{
$find('<%= txtOne.ClientID %>').disable();
}
}
方法1 -如果事件是由父窗体中的控件触发的
在用户控件中,您可以定义一个返回TextBox ID的属性:
public string TextBoxID
{
get { return txtOne.ClientID; }
}
如果您的表单包含两个用户控件的实例,ctrl1
和ctrl2
,您可以像这样针对特定的一个:
document.getElementById('<%= ctrl1.TextBoxID %>').disabled = true;
注意:在用户控件中,确保内部控件不使用ClientIDMode="Static"
。
如果您的用户控件包含以下标记:
<asp:CheckBox ID="chk1" runat="server" />
<asp:TextBox ID="txtOne" runat="server" />
您可以在用户控件的Page_Load
方法中向CheckBox添加Javascript事件处理程序:
protected void Page_Load(object sender, EventArgs e)
{
string js = string.Format("txtBoxID = '{0}';", txtOne.ClientID);
chk1.Attributes.Add("onmousedown", js);
chk1.Attributes.Add("onkeydown", js);
}
这些事件处理程序设置txtBoxID
变量的值,您可以在Javascript块中定义和使用:
<script type="text/javascript">
var txtBoxID;
function confirmCallBackFn(arg) {
if (arg == true) {
document.getElementById(txtBoxID).disabled = true;
}
}
</script>
此方法假设在处理过程中没有回发。如果发生回发,我们可能必须修改该方法,并在代码隐藏的事件处理程序中注册一个脚本。