ASP.NET如何通过容器值作为参数的JavaScript [英] ASP.NET How to pass container value as javascript argument

查看:181
本文介绍了ASP.NET如何通过容器值作为参数的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 oBout Grid控件上的文本框的模板。

我想一个参数传递给一个javascript,网格的当前行的索引,当用户点击它。

但结果

 的onClick ='setGridInEditMode(小于%#Container.RecordIndex%GT;);' />

出来为

 的onClick =setGridInEditMode(放大器; LT;%#Container.RecordIndex%GT;);

有没有一种办法容器值传递给JavaScript?

下面是有问题的标记。

 < CC1:网格ID =_ TrustGrid=服务器
        FolderStyle =样式/ style_7
        AllowAddingRecords =假
        AllowSorting =假
        AllowPageSizeSelection =假
        AllowPaging =假
        AllowMultiRecordEditing =真
        的AutoGenerateColumns =FALSE
        OnUpdatecommand =_ TrustGrid_UpdateCommand
        OnRebind =_ TrustGrid_Rebind>
    <柱体和GT;
        < CC1:列AllowEdit =真AllowDelete =假的HeaderText =编辑WIDTH =130=服务器/>
        &所述; CC1:列数据字段=TrustDocID的HeaderText =TrustDocID宽度=125只读=假AllowDelete =假TemplateId =trustDocIDGridTemplate/>
    < /专栏>
    <模板和GT;
        < CC1:GridTemplate ID =trustDocIDGridTemplate控件ID =TB1=服务器>
            <模板>
                < ASP:文本框ID =trustDocIDTextBox=服务器
                    可见=真
                    文字='<%#Container.Value%GT;'
                    的onClick ='setGridInEditMode(小于%#Container.RecordIndex%GT);' />
            < /模板>
        < / CC1:GridTemplate>
    < /模板>
< / CC1:网格>


解决方案

我第二次达林呼吁使用非侵入式JavaScript。但是,这并不回答您为什么ASP.NET是这样做的问题。

你的原因。

 的onClick =setGridInEditMode(放大器; LT;%#Container.RecordIndex%GT;);

是因为数据绑定到服务器控件的属性要求您没有插入文本的直接绑定到财产的。这意味着,只有属性=<%#......%>中。允许

所以你的情况,你需要说你想在时尚迂回什么(虽然我个人认为这是更清楚一点,更容易维护):

 的onClick ='<%#的String.Format(setGridInEditMode({0});,Container.RecordIndex)%GT;'

(注意你的单引号和双引号虽然!)

该限制适用的只有的服务器控件和它们的属性。它并不适用于服务器控件的嵌套的文字内容(如模板或面板机构),也无法在别处使用纯HTML,这可能是为什么你从​​来没有在这之前注意。

I am using an oBout Grid control with a template on a textbox.

I would like to pass an argument to a javascript, the current row index of a grid when a user clicks on it.

But the result of

    onClick='setGridInEditMode(<%# Container.RecordIndex %>);' />

comes out as

     onClick="setGridInEditMode(&lt;%# Container.RecordIndex %>);"

Is there a way to pass container value to javascript?

Here is the markup in question.

<cc1:Grid ID="_TrustGrid" runat="server"
        FolderStyle="Styles/style_7"
        AllowAddingRecords="False" 
        AllowSorting="false"
        AllowPageSizeSelection="False"
        AllowPaging="False"
        AllowMultiRecordEditing="true"
        AutoGenerateColumns="False" 
        OnUpdatecommand="_TrustGrid_UpdateCommand"
        OnRebind="_TrustGrid_Rebind">
    <Columns>
        <cc1:Column AllowEdit="true" AllowDelete="false" HeaderText="Edit" Width="130" runat="server" />
        <cc1:Column DataField="TrustDocID" HeaderText="TrustDocID" Width="125" ReadOnly="false" AllowDelete="false" TemplateId="trustDocIDGridTemplate" />
    </Columns>
    <Templates>
        <cc1:GridTemplate ID="trustDocIDGridTemplate" ControlID="tb1" runat="server">
            <Template>
                <asp:TextBox ID="trustDocIDTextBox" runat="server" 
                    Visible="true"
                    Text='<%# Container.Value %>'
                    onClick= 'setGridInEditMode(<%# Container.RecordIndex %>);' />
            </Template>
        </cc1:GridTemplate>
    </Templates>
</cc1:Grid>

解决方案

I'd second Darin's call for using unobtrusive JavaScript. However, that doesn't answer your question on why ASP.NET is doing this.

The reason you get

onClick="setGridInEditMode(&lt;%# Container.RecordIndex %>);"

is because databinding to server control properties requires you to bind directly to the property without intervening text. That means, only Property="<%# ... %>" is allowed.

So in your case, you'll need to say what you want in a roundabout fashion (although I personally think this is a little clearer and more maintainable):

onClick='<%# String.Format("setGridInEditMode({0});", Container.RecordIndex) %>'

(Watch your single and double quotes though!)

This limitation applies only to server controls and their properties. It does not apply to a server control's nested literal content (such as bodies of templates or panels) nor to plain HTML used elsewhere, which is probably why you've never noticed this before.

这篇关于ASP.NET如何通过容器值作为参数的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆