【问题标题】:Finding a div in my SharePoint 2010 Web Part在我的 SharePoint 2010 Web 部件中查找 div
【发布时间】:2011-08-15 21:33:06
【问题描述】:

我有一个使用 C# 动态插入用 VS2010 编写的 div 标签的 Web 部件。我想为这些 div 实现鼠标悬停事件。当 Web 部件部署到 SP2010 上时,当我使用我指定的控件 ID 搜索这些 div 时,我的 JavaScript 无法找到它们。

当我查看页面源时,我发现ct100_m_g_之类的一些标签是在我指定的控件id前面加上前缀的。

我如何猜出这些 id?

【问题讨论】:

  • 这是用于可视 Web 部件还是 Web 控件(无 ascx)?

标签: javascript sharepoint web-parts


【解决方案1】:

ctlxxx 内容由 ASP.NET 自动添加到控件的 ID 之前,以生成客户端 ID。

如果要设置确定性客户端 ID,可以设置 ClientID 属性而不是 ID 属性。见http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

【讨论】:

  • 当您将 Web 部件的多个实例添加到一个页面时,静态 ID 不会导致问题吗?
  • 是的,它肯定会...您可以设置一个确定性(如可预测的)但非静态的 ID,假设页面上可能呈现多个 Web 部件。
  • 谢谢,我必须使用 clientidmode 属性并且它有效。再次感谢
【解决方案2】:

在 webparts 中,您可以使用客户端 ID 在 javascript 中查找您的控件。看看这个例子就明白了:

using System;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace MyDemo.WebParts
{
    public class MyWebPart : WebPart
    {
        private TextBox txtInput;
        private TextBox txtOutput;
        private Button btnDoSomething;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            txtInput = new TextBox();
            Controls.Add(txtInput);

            txtOutput = new TextBox();
            txtOutput.ReadOnly = true;
            Controls.Add(txtOutput);

            btnDoSomething = new Button();
            btnDoSomething.Text = "Do Something";
            btnDoSomething.OnClientClick = string.Format("DoSomething('{0}', '{1}'); return false;", txtInput.ClientID, txtOutput.ClientID);
            Controls.Add(btnDoSomething);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // Check if the script is already registered, this is necessary because the webpart can be
            // added multiple times to the same page
            if (!Page.ClientScript.IsClientScriptBlockRegistered("MyWebPartScript"))
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("function DoSomething(inputControlID, outputControlID){");
                sb.Append("  var inputControl = document.getElementById(inputControlID);");
                sb.Append("  var outputControl = document.getElementById(outputControlID);");
                sb.Append("  outputControl.value = inputControl.value;");
                sb.Append("}");

                Page.ClientScript.RegisterClientScriptBlock(GetType(), "MyWebPartScript", sb.ToString(), true);
            }
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            EnsureChildControls();

            writer.Write("<table>");
            writer.Write("<tr><td>");
            txtInput.RenderControl(writer);
            writer.Write("</td><td>");
            txtOutput.RenderControl(writer);
            writer.Write("</td></tr><tr><td colspan=\"2\">");
            btnDoSomething.RenderControl(writer);
            writer.Write("</td></tr></table>");
        }
    }
}

【讨论】:

  • 谢谢,我不得不将 clientidmode 属性设置为静态,事情开始按预期工作。非常感谢
【解决方案3】:

您可以在生成每个 DIV 时为其指定一个类。然后,只需使用类名来选择它们并添加事件处理程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-19
    • 2011-05-12
    • 2013-05-26
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    相关资源
    最近更新 更多