ASP.NET 如何将容器值作为 javascript 参数传递
我正在使用 oBout 网格控件 以及文本框上的模板。
我想将一个参数传递给 JavaScript,即用户单击网格时网格的当前行索引。
但结果
onClick='setGridInEditMode(<%# Container.RecordIndex %>);' />
是
onClick="setGridInEditMode(<%# 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我赞同达林关于使用不引人注目的 JavaScript 的呼吁。但是,这并不能回答您关于 ASP.NET 为何这样做的问题。
您得到的原因
是因为到服务器控件属性的数据绑定要求您直接绑定到属性,而无需插入文本。这意味着,仅允许
Property="<%# ... %>"
。因此,就您而言,您需要以迂回的方式说出您想要的内容(尽管我个人认为这更清晰且更易于维护):(
不过请注意您的单引号和双引号!)
此限制仅适用 服务器控件及其属性。它不适用于服务器控件的嵌套文字内容(例如模板或面板的主体),也不适用于其他地方使用的纯 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
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):
(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.
与其使用 javascript 函数污染您的 HTML,不如使用 jQuery 提供一个不显眼的解决方案:
如果您更喜欢 ASP.NETish 解决方案,您始终可以这样做:
Instead of polluting your HTML with javascript functions how about an unobtrusive solution using jQuery:
If you prefer instead the more ASP.NETish solution you could always do this: