简体   繁体   English

操作 asp.net 'runat=“server” ' html object 在 ZDE9B9ED78D7E2E1DCEEFFEE78

[英]Manipulate asp.net 'runat=“server” ' html object in javascript

Alright, I know you can get information from an asp.net control with this code:好的,我知道您可以使用以下代码从 asp.net 控件获取信息:

var element = document.getElementById('<%=myControl.ClientID%>'); 

However I am incapable of manipulating said html element after grabbing it in Javascript.但是,我无法在 Javascript 中抓取所述 html 元素后对其进行操作。 What would I need to do to change the properties of something that is set to runat="server" in javascript?我需要做什么来更改 javascript 中设置为 runat="server" 的属性?

Is it only possible through the server-side C#?是否只能通过服务器端C#?

For further clarification I have a div that changes sizes via Javascript and am trying to get the mschart that exists in it's innerhtml to change it's height/width along with it.为了进一步澄清,我有一个通过 Javascript 更改大小的 div,并试图获取它的 innerhtml 中存在的 mschart 以更改它的高度/宽度。 However the fact that it runs at the server causes the problems.然而,它在服务器上运行的事实导致了问题。

<div id="div0" style="background-color:Silver; position: absolute; top: 0px; left: 0px; width: 480px; height: 245px;">
            <asp:Chart ID="chart0" runat="server"  Height="245px" Width="480px" 
                    BackColor="220, 230, 242" BackGradientStyle="None" 
                        BackSecondaryColor="220, 230, 242">
            <BorderSkin PageColor="220, 230, 242" />
            </asp:Chart>
        </div>

Edit: ended up handling chart resizing in a through postback with query string, then grabbing those values in javascript on init and resizing the divs there.编辑:最终使用查询字符串在回发中处理图表大小调整,然后在初始化时获取 javascript 中的这些值并在那里调整 div 的大小。

Because the element has runat=server you would only be able to modify properties in code behind (the server side).因为该元素具有runat=server您只能在代码后面(服务器端)中修改属性。 The actual client side properties such as visibility can be changed via javascript though.不过,可以通过 javascript 更改实际的客户端属性(例如可见性)。

It's a little unclear what you really mean.有点不清楚你的真正意思。 If you mean, you want to manipulate server side properties, or call server side functions, then you can't do that through javascript.如果你的意思是,你想操纵服务器端的属性,或者调用服务器端的函数,那么你不能通过 javascript 来做到这一点。 You would need to use something like ajax or webmethods.您需要使用 ajax 或 webmethods 之类的东西。 If you mean that you want to modify it's client properties, like whether it's visible, or what data it contains then yes you can do that.如果你的意思是你想修改它的客户端属性,比如它是否可见,或者它包含什么数据,那么你可以这样做。 But, you need to specify more information about what exactly you're trying to do.但是,您需要指定有关您正在尝试做什么的更多信息。

EDIT:编辑:

Based on your updated information there's good news and bad news.根据您更新的信息,有好消息也有坏消息。 Yes, you can change the size of the control on the client side, but that will only stretch the image.是的,您可以在客户端更改控件的大小,但这只会拉伸图像。 MSChart generates an image file that is downloaded. MSChart 生成一个下载的图像文件。 Stretching will result in poor quality.拉伸会导致质量差。

You will need to regenerate the chart for your new size if stretching is not an option.如果不能选择拉伸,则需要为新尺寸重新生成图表。 That will require you to use some kind of ajax, or completely refresh the page.这将需要您使用某种 ajax,或完全刷新页面。

You should be able to edit the client-side properties of the "control" if that control translates to a simple HTML element.如果该控件转换为简单的 HTML 元素,您应该能够编辑“控件”的客户端属性。 Sometimes a server-side property doesn't have a client-side analog, however.但是,有时服务器端属性没有客户端模拟。

One common thing to be aware of is if the control is not set to visible, then that means it's not rendered on the page at all;需要注意的一件常见事情是,如果控件未设置为可见,则意味着它根本不会呈现在页面上; if that's the case, then you cannot access it through JavaScript.如果是这种情况,那么您将无法通过 JavaScript 访问它。

if all you are trying to modify is the with and height of the object there is no reason why the following shouldn't work:如果您要修改的只是 object 的高度和高度,那么以下内容没有理由不起作用:

var element = document.getElementById('<%=myControl.ClientID%>'); 
element.style.height = divHeight;

Just look in the generated html that the control with that name is actually a div or an image (or something you can re-size), where you can set width and height on.只需在生成的 html 中查看具有该名称的控件实际上是 div 或图像(或您可以重新调整大小的东西),您可以在其中设置宽度和高度。 I don't know what Chart control renders to.我不知道图表控件呈现给什么。 In any case, use Firebug to inspect the html elements in your page, and see how the changes you do in javascript influence the result.在任何情况下,使用 Firebug 检查您页面中的 html 元素,并查看您在 javascript 中所做的更改如何影响结果。

In any case, keep in mind you can only set client properties in javascript .无论如何,请记住您只能在 javascript 中设置客户端属性 This means you are looking to set this on some div element, rather the the server side control .这意味着您希望在某些 div 元素上设置它,而不是在服务器端控件上。 Knowing that the server side control does render to html, you should be able to achive what you want, one way or another.知道服务器端控件确实呈现给 html,您应该能够以一种或另一种方式实现您想要的。

My suggestion would be to write a script using jQuery.我的建议是使用 jQuery 编写脚本。 The thing it would use are the following in client script code:它将在客户端脚本代码中使用以下内容:

  1. Get the element which height you need.获取所需高度的元素。
  2. Get the element which you want to manipulate.获取要操作的元素。
  3. Change the element height with the value from part 1.使用第 1 部分中的值更改元素高度。

If you think this would be a way to go and your new to jQuery, I can help you with some code for it.如果您认为这是 go 和 jQuery 新手的一种方式,我可以为您提供一些代码。

I attempted to manipulate a control in jquery that contained the runat="server" attribute.我试图操纵 jquery 中包含runat="server"属性的控件。 I was unable to change it.我无法改变它。 As soon as I removed runat="server" , I noticed the jquery changes occur.删除runat="server"后,我注意到发生了 jquery 更改。

You can test it out for yourself:你可以自己测试一下:

<asp:Label ID="myLabel">hello, warlord!</asp:Label>
<asp:Label ID="myLabel2" runat="server">hello, warlord!</asp:Label>
<script type="text/javascript">$("#myLabel").css("border", "3px solid red");</script>
<script type="text/javascript">$("#myLabel2").css("border", "3px solid red");</script>

I had the same issue, this usually happens when your html controls(with runat="server") are inside of a contentPlaceHolder.我遇到了同样的问题,这通常发生在您的 html 控件(带有 runat="server")位于 contentPlaceHolder 内时。 Here's what I did:这是我所做的:

I changed:我变了:

document.getElementById("mypopup").style.visibility = "visible";

to:至:

document.getElementById("ctl00_ContentPlaceHolder1_mypopup").style.visibility = "visible";

When you view the page source code on the browser you'll see the name that your html control really has.当您在浏览器上查看页面源代码时,您会看到 html 控件真正具有的名称。

you can find more information on how to actually use properly the ClientID property here您可以在此处找到有关如何正确使用 ClientID 属性的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM