I'm generating a table of content in an ASP.NET page and I want to add client side OnClick
actions to each cell (the action should be to set the value of a named TextBox control to the text in the clicked cell). If I could directly add stuff to the HTML output, I think I could do this with a single line of JavaScript in each cell.
我正在ASP.NET页面中生成一个内容表,我想向每个单元格添加客户端OnClick操作(操作应该是将命名的TextBox控件的值设置为单击的单元格中的文本)。如果我可以直接在HTML输出中添加内容,我想我可以在每个单元格中使用一行JavaScript来完成此操作。
Given that I'me not now using JQuery (and never have before), what is the simplest solution to adding this (including the time spent learning stuff)?
鉴于我现在没有使用JQuery(以前从未有过),添加此内容的最简单的解决方案是什么(包括学习内容的时间)?
Edit: this seems to work
编辑:这似乎工作
4 个解决方案
#1
If you are creating your table programmatically, using HtmlControls, you can set the onclick client attribute of the HtmlTableCell:
如果使用HtmlControls以编程方式创建表,则可以设置HtmlTableCell的onclick客户端属性:
HtmlTableCell cell = new HtmlTableCell();
cell.Attributes.Add("onclick", "someFunction();");
However, if you're using jQuery you can do something like this:
但是,如果您使用的是jQuery,则可以执行以下操作:
$('td').click(function(){ // Select all the table cells on the document.
$(this).html($('#textboxId').val()); // When clicked, change the innerHTML
}); // with a textbox value.
#2
it depends how you're generating that table of content.
这取决于你如何生成该内容表。
-
If bulding up using a table control, you can add JavaScript to TableCell controls using the Attributes.Add() method.
如果使用表格控件进行扩展,则可以使用Attributes.Add()方法将JavaScript添加到TableCell控件。
-
If building up with the GridView, you can add JavaScript when the RowDataBound event is raised
如果使用GridView构建,则可以在引发RowDataBound事件时添加JavaScript
#3
You should give each cell a css class... class="contentsCell"
你应该给每个单元格一个css类... class =“contentsCell”
You can then add the following script to your head tag (along with a jquery include)
然后,您可以将以下脚本添加到头标记(以及jquery包含)
$(document).ready(function(){
$(".contentsCell").click(function(){
CallYourFunction();
})
})
Where CallYourFunction() is you own function...
CallYourFunction()是你自己的函数......
This will basically attach an
这基本上会附上一个
onclick=" CallYourFunction();"
to each td cell (or any other element) with the class "contentsCell"
使用“contentsCell”类的每个td单元格(或任何其他元素)
Also, if you want to grab the text inside the cell, something like:
此外,如果您想要获取单元格内的文本,例如:
$(document).ready(function(){
$(".contentsCell").click(function(){
var mytext = $(this).text();
CallYourFunction(mytext);
});
});
Check out visualjquery.com for a great reference for jquery
查看visualjquery.com以获取jquery的绝佳参考
#4
Well, your question is kind of difficult to follow, but I'll give you a few pointers.
好吧,你的问题有点难以理解,但我会给你一些指示。
Each control has a ClientID property. This is useful for injecting into your javascript so you know the client side id of the control.
每个控件都有一个ClientID属性。这对于注入javascript非常有用,因此您可以知道控件的客户端ID。
jQuery syntax for putting text into a textbox would be like the following:
用于将文本放入文本框的jQuery语法如下所示:
$("#" + clientId).attr("value", theValue);
and the jQuery syntax for getting text from a cell would be:
用于从单元格获取文本的jQuery语法将是:
$("#" + cellId).html()
#1
If you are creating your table programmatically, using HtmlControls, you can set the onclick client attribute of the HtmlTableCell:
如果使用HtmlControls以编程方式创建表,则可以设置HtmlTableCell的onclick客户端属性:
HtmlTableCell cell = new HtmlTableCell();
cell.Attributes.Add("onclick", "someFunction();");
However, if you're using jQuery you can do something like this:
但是,如果您使用的是jQuery,则可以执行以下操作:
$('td').click(function(){ // Select all the table cells on the document.
$(this).html($('#textboxId').val()); // When clicked, change the innerHTML
}); // with a textbox value.
#2
it depends how you're generating that table of content.
这取决于你如何生成该内容表。
-
If bulding up using a table control, you can add JavaScript to TableCell controls using the Attributes.Add() method.
如果使用表格控件进行扩展,则可以使用Attributes.Add()方法将JavaScript添加到TableCell控件。
-
If building up with the GridView, you can add JavaScript when the RowDataBound event is raised
如果使用GridView构建,则可以在引发RowDataBound事件时添加JavaScript
#3
You should give each cell a css class... class="contentsCell"
你应该给每个单元格一个css类... class =“contentsCell”
You can then add the following script to your head tag (along with a jquery include)
然后,您可以将以下脚本添加到头标记(以及jquery包含)
$(document).ready(function(){
$(".contentsCell").click(function(){
CallYourFunction();
})
})
Where CallYourFunction() is you own function...
CallYourFunction()是你自己的函数......
This will basically attach an
这基本上会附上一个
onclick=" CallYourFunction();"
to each td cell (or any other element) with the class "contentsCell"
使用“contentsCell”类的每个td单元格(或任何其他元素)
Also, if you want to grab the text inside the cell, something like:
此外,如果您想要获取单元格内的文本,例如:
$(document).ready(function(){
$(".contentsCell").click(function(){
var mytext = $(this).text();
CallYourFunction(mytext);
});
});
Check out visualjquery.com for a great reference for jquery
查看visualjquery.com以获取jquery的绝佳参考
#4
Well, your question is kind of difficult to follow, but I'll give you a few pointers.
好吧,你的问题有点难以理解,但我会给你一些指示。
Each control has a ClientID property. This is useful for injecting into your javascript so you know the client side id of the control.
每个控件都有一个ClientID属性。这对于注入javascript非常有用,因此您可以知道控件的客户端ID。
jQuery syntax for putting text into a textbox would be like the following:
用于将文本放入文本框的jQuery语法如下所示:
$("#" + clientId).attr("value", theValue);
and the jQuery syntax for getting text from a cell would be:
用于从单元格获取文本的jQuery语法将是:
$("#" + cellId).html()