使用客户端 API 进行用户控制

发布于 2024-10-19 13:35:07 字数 489 浏览 6 评论 0原文

也许我选了一个完全不合适/坏的例子。 我拥有的是一个用户控件,其中包含一堆动态创建的 Telerik RadGrid。 我的用户控件被添加到几个 Telerik RadPageView 中,这些视图是与 RadTabStrip 一起使用的 RadMultiPage 的一部分。 我需要做的是在我的用户控件中调用 Javascript 函数,以便在选择其父 RadPageView 时更新其显示。 所以基本上我有以下 Javascript 代码:

function OnClientTabSelected(sender, args)
{
    // Get the MyControl that is on this tab
    var myControl = $find("whatever");

    // Call a method that updates the display
    myControl.doSomething();
}

谢谢,

大卫

Maybe I've picked a totally inappropriate/bad example.
What I have is a user control that contains a bunch of dynamically created Telerik RadGrids.
My user control is added to a couple of Telerik RadPageViews that are part of a RadMultiPage that is used alongside a RadTabStrip.
What I need to do is call a Javascript function in my usercontrol to update it's display whenever it's parent RadPageView is selected.
So basically I have the following Javascript code:

function OnClientTabSelected(sender, args)
{
    // Get the MyControl that is on this tab
    var myControl = $find("whatever");

    // Call a method that updates the display
    myControl.doSomething();
}

Thanks,

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-10-26 13:35:07

您可以在用户控件中添加一个包装 div,然后使用 jQuery 扩展该 div 以添加所需的方法和属性。技巧是设置div的id='<%=this.ID%>' - 这样,div 与用户控件具有相同的 ID(这很好,因为用户控件实际上不渲染任何内容 - 仅渲染其内容)。

然后回到您的包含页面,您可以使用 $get('whatever') 引用您的 UserControl 的 ID - 您实际上将选择您的扩展 div.. 它将包含您的所有方法和属性。

这种方法的好处是,所有方法和属性的作用域都清晰,并且全局名称空间中没有任何内容。

如果您想了解更多信息,我在这里有一个完整的演示解决方案和详细信息:
http://programmerramblings.blogspot.com/2011 /07/clientside-api-for-aspnet-user-controls.html

You can add a wrapper div in your User Control and then extend that div using jQuery to add your desired methods and properties. The trick is to set the div's id='<%=this.ID%>' - that way the div has the same ID as the User Control (which is fine because the User Control doesn't actually render anything - only its contents).

Then back on your containing page, you can just reference your UserControl's ID using $get('whatever') - and you'll actually select your extended div.. which will have all your methods and properties on it.

The nice thing about this approach is that all of your methods and properties and neatly scoped and nothing is in the global namespace.

I have a full demo solution and details here if you want more info:
http://programmerramblings.blogspot.com/2011/07/clientside-api-for-aspnet-user-controls.html

墨离汐 2024-10-26 13:35:07

如果您确定该函数的名称,只需在输入按钮中调用 javascript 方法即可。

<input type="button" value="test" onclick="doSomething()" />

如果您将任何 javascript 代码放置在将被吐到页面上的控件中,并且只要它们的形式相同,就可以调用该代码。

例如,如果您查看该页面的源代码,您的代码将如下所示。

<script type="text/javascript">    
    function doSomething()
    {
      alert(new Date());
    }
</script>

<div>
  <span id="MyControl1_Label1">Dummy label</span>
</div>
<hr />
<input type="button" value="test" onclick="doSomething()" />

编辑:我认为这不是访问这些方法的好方法。当您将一些 javascript 代码放入控件中时,它应该仅在该控件中使用(没有这样的规则,它只是一个设计建议)。如果您尝试从控件外部访问该控件的 javascript 代码,那么您需要重新审视您的设计。

如果您可以向我们提供有关为什么要访问该方法的更多详细信息,也许我们可以建议一些更好的方法来做到这一点。

更新:(正如您修改了问题一样):回答这个问题有点棘手,因为我没有 Rad 控件的实际经验。我想应该有一些功能可以帮助您在不使用 javascript 的情况下更新该页面中的控件,可以查看为 Rad 提供的客户端 API。

了解 RAD 控件的人可能会帮助您。

Just make a call to javascript method in input button if you are sure about the name of that function.

<input type="button" value="test" onclick="doSomething()" />

If you place any javascript code in the control that will be spit on the page and it will be available for calling provided both of them are in the same form.

for example your code will look like this if you look into the source of that page.

<script type="text/javascript">    
    function doSomething()
    {
      alert(new Date());
    }
</script>

<div>
  <span id="MyControl1_Label1">Dummy label</span>
</div>
<hr />
<input type="button" value="test" onclick="doSomething()" />

Edit: This is not a good way to access these methods in my opinion. When you are putting some javascript code inside a control then it should be used in that control only (There is no rule as such, its just a design suggestion). If you are trying to access javascript code of a control from outside that control then you need to revisit your design.

If you can give us more details on why you want to access that method, may be we can suggest some better way to do that.

Update: (As you have modified your question): Bit tricky to answer this as I dont have hands on experience with Rad controls. I guess there should be some feature which will help you to update the controls in that page without using javascript, may be have a look at clientside-API provided for Rad.

May be somebody who knows about RAD controls will help you.

夏夜暖风 2024-10-26 13:35:07

您应该将控制方法公开

public void doSomething

并从页面调用它

myControl1.doSomething();

You should make the control method public

public void doSomething

and call this from the page

myControl1.doSomething();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文