使用javascript为单个asp时代的多个实例启用/禁用表

时间:2009-01-21 16:48:16

标签: asp.net javascript user-controls

我刚刚发布了一个问题,很快得到了答复,谢谢。

我有一个新问题,因为我有一个asp标签,它在实例化期间动态设置文本然后我有一个onmousedown函数绑定它调用一个javascript函数来启用一个位于显示的下方的表区域 - 没有默认值。一切正常,直到我在页面上放置其中两个用户控件。它们都具有正确的特定标签文本,但是javascript enable似乎将第一个usercontrol表的显示样式属性设置为阻止而不是位于所单击标签下方的那个。我确信这是因为脚本在客户端运行(我真的想保留)并且所有用户控件都具有相同的id名称(因为它们都是同一用户控件的instaniations)所以要设置的javascript样式显示属性只获取第一个表。我似乎无法想出一个好的方法来动态命名表格在instationation所以我可以指定这个“uniquie”id名称为javascript或任何其他方式来做这项工作。

用户控件asp代码如下:

function enableDivArea(objName) {
    document.getElementById(objName).style.display = "block";
}
function disableDivArea(objName) {
    document.getElementById(objName).style.display = "none";
    document.forms[0].submit();
}<asp:Label style="cursor:pointer;color:#EA9156;font-family:Arial,Helvetica,sans-serif;font-weight:bold;" ID="m_emailAddressLabel" onmousedown="enableDivArea('EmailFormDivArea');" runat="server" Text="someone@emailaddress.com"></asp:Label>
<table id="EmailFormDivArea" style="display:none; border-style: outset; border-width: thin">
    <tr>
        <td>To: <asp:Label ID="m_sendEmailToLabel" runat="server" Text=""></asp:Label></td>
        <td align="right"><asp:Label onmousedown="disableDivArea('EmailFormDivArea');" id="m_closeLabel" runat="server" Text="close"></asp:Label></td>
    </tr>
    <tr>
        <td><asp:Label ID="m_fromLabel" runat="server" Text="First Name:" Visible="True"></asp:Label></td>
        <td><asp:TextBox ID="m_firstNameBox" runat="server" Visible="True"></asp:TextBox></td>
    </tr>
    <tr>
        <td><asp:Label ID="Label1" runat="server" Text="Last Name:" Visible="True"></asp:Label></td>
        <td><asp:TextBox ID="m_lastNameBox" runat="server" Visible="True"></asp:TextBox></td>
    </tr>
    <tr>
        <td><asp:Label ID="Label2" runat="server" Text="E-mail:" Visible="True"></asp:Label></td>
        <td><asp:TextBox ID="m_emailBox" runat="server" Visible="True"></asp:TextBox></td>
    </tr>
    <tr>
        <td><asp:Label ID="Label3" runat="server" Text="Phone number:" Visible="True"></asp:Label></td>
        <td><asp:TextBox ID="m_phoneNumberBox" runat="server" Visible="True"></asp:TextBox></td>
    </tr>
    <tr>
        <td><asp:Label ID="Label4" runat="server" Text="Message:" Visible="True"></asp:Label></td>
        <td><asp:TextBox ID="m_messageBox" runat="server" Visible="True" Rows="6" TextMode="MultiLine"></asp:TextBox></td>
    </tr>
    <tr>
        <td colspan="2" align="center"><asp:Button ID="m_sendMessageButton" runat="server" Text="Send Message" 
                        onclick="m_sendMessageButton_Click" /></td>
    </tr>
    <tr>
        <td colspan="2" align="center"><asp:Label runat="server" ID="m_statusLabel" Text="" Visible="true"></asp:Label></td>
    </tr>
</table> 

后面的代码如下:

protected void Page_Load(object sender, EventArgs e)
{
    m_emailAddressLabel.Text = this.Parameter;
    m_sendEmailToLabel.Text = this.Parameter;

}

5 个答案:

答案 0 :(得分:1)

拥有表runat服务器,并让用户控件实现INamingContainer。 构造表的ID,并在重写的CreateChildControls中以编程方式(HtmlTable)设置它,例如string.Concat(ID,“table”)。

编辑:你还需要在javascript中动态ID引用。 您可以为此使用一些&lt; asp:PlaceHolder&gt;,然后在CreateChildControls中从代码隐藏中设置它, 但是也许更容易将这些小脚本内联移动,并从代码隐藏中发出脚本, 在asp:Label

上设置客户端属性

答案 1 :(得分:0)

只是一个简单的想法,这些表是否具有相同的ID?所以脚本只选择它找到的第一个?

使ID唯一!

使用asp:table然后

onmousedown="enableDivArea('<%=tableName.ClientID%>');" 

答案 2 :(得分:0)

首先,您可以为样式设置CssClass属性。

隐藏: document.getElementById(theIdOfYourTable).style.display ='none';

启用相同的

答案 3 :(得分:0)

哎呀,对不起,忘了你不能用&lt; %%&gt;在一个服务器标签(并在我脑海中这样做)。对你来说更好的是这样调用脚本:

onmousedown="enableDivArea(this);"

在您的脚本中,您不必再执行document.getElementById,因为您现在已经将该元素作为param。

答案 4 :(得分:0)

我会在Henk的最后一次回应中保持这种想法。

在你的家伙信息的帮助下,我以一种全新的方式“欺骗”了。我动态地向标签添加了一个属性,我希望javascript从Page_Load事件中的onmousedown运行,并将字符串格式化为我想要的。这是一条线,它非常有效(至少我正在寻找)。

m_emailAddressLabel.Attributes.Add("onmousedown", "enableDivArea('" + EmailFormDivArea.ClientID + "');");

感谢您的帮助。