ASP.NET 如何将容器值作为 javascript 参数传递

发布于 2024-08-07 18:57:03 字数 1691 浏览 7 评论 0原文

我正在使用 oBout 网格控件 以及文本框上的模板。

我想将一个参数传递给 JavaScript,即用户单击网格时网格的当前行索引。

但结果

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

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

Is there a way to pass container value to javascript?

这是有问题的标记。

<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 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(<%# 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>

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

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

发布评论

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

评论(2

财迷小姐 2024-08-14 18:57:03

我赞同达林关于使用不引人注目的 JavaScript 的呼吁。但是,这并不能回答您关于 ASP.NET 为何这样做的问题。

您得到的原因

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

是因为到服务器控件属性的数据绑定要求您直接绑定到属性,而无需插入文本。这意味着,仅允许 Property="<%# ... %>"

因此,就您而言,您需要以迂回的方式说出您想要的内容(尽管我个人认为这更清晰且更易于维护):(

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

不过请注意您的单引号和双引号!)

此限制仅适用 服务器控件及其属性。它不适用于服务器控件的嵌套文字内容(例如模板或面板的主体),也不适用于其他地方使用的纯 HTML,这可能就是您以前从未注意到这一点的原因。

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(<%# 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.

じее 2024-08-14 18:57:03

与其使用 javascript 函数污染您的 HTML,不如使用 jQuery 提供一个不显眼的解决方案:

$(function() {
    $('#_TrustGrid input[id*=trustDocIDTextBox]').each(function(index) {
        $(this).click(function() {
            setGridInEditMode(index);
        });
    });
});

如果您更喜欢 ASP.NETish 解决方案,您始终可以这样做:

<asp:TextBox 
    ID="trustDocIDTextBox" 
    runat="server" 
    Visible="true"
    Text='<%# Container.Value %>'
    onclick='<%# "setGridInEditMode(" + Container.RecordIndex + ")" %>' />                

Instead of polluting your HTML with javascript functions how about an unobtrusive solution using jQuery:

$(function() {
    $('#_TrustGrid input[id*=trustDocIDTextBox]').each(function(index) {
        $(this).click(function() {
            setGridInEditMode(index);
        });
    });
});

If you prefer instead the more ASP.NETish solution you could always do this:

<asp:TextBox 
    ID="trustDocIDTextBox" 
    runat="server" 
    Visible="true"
    Text='<%# Container.Value %>'
    onclick='<%# "setGridInEditMode(" + Container.RecordIndex + ")" %>' />                
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文