到目前为止,我在Gridview上的C#中进行了以下工作,称为GridView1。当我把它放在onSelectedIndexChanged。
时,它可以工作HostTextbox.Text = GridView1.SelectedRow.Cells[0].Text;
但是由于这个回发到服务器我想避免它,因为我将为单元格[0]到单元格[10]。所以,我调查了Javascript。我用Google搜索并找到了各种解决方案,这是我到目前为止“半工作”的解决方案。
我的C#看起来像这样:
int myRowIdx = 0; // class variable
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("ondbclick", "sample('" + myRowIdx.ToString() + "')");
}
myRowIdx++;
}
在我的Javascript中,我插入了警报,告诉我问题在哪里开心。它看起来像这样:
function sample(rowIn) {
alert("A");
var gViewID = '<%= GridView1.ClientID %>';
alert("B");
var gView = getElementById(gViewID);
alert("C");
var gViewRow = gView.rows[rowIn];
alert("D");
var gViewRowColumn = gViewRow.cells[0];
alert("E");
var displayCell = gViewRowColumn.innerText;
alert("F");
alert(displayCell);
}
B是我看到的最后一个警报。我似乎无法弄清楚这一点。我仔细观察它仍然没有成功。请帮忙。
答案 0 :(得分:1)
我不明白你的意思是“我想避免它,因为我将为细胞[0]到细胞[10]”。您可以连续为所选行中的每个单元格执行此操作。所以你只需要一个Postback。
根据您的Javascript问题,您可以简单地将tr(GridViewRow)作为js-variable传递给您的示例函数。因此,您只需将this
作为参数传递:
e.Row.Attributes.Add("ondbclick", "sample(this)");
并在你的js-function中:
function sample(tr) {
var gViewRowColumn = tr.cells[0];
var displayCell = gViewRowColumn.innerText;
alert(displayCell);
}
答案 1 :(得分:0)
使用项目模板面板进行网格视图。 ex.http://msdn.microsoft.com/en-us/library/aa479353.aspx 使用onrowcommand事件获取值。
像.. 事件 { 如果(e.commandname = “编辑”) {
}