当用户选择行时,可以从RowDataBound上的网格视图获得所需的值
本文关键字:视图 网格 RowDataBound 选择 用户 | 更新日期: 2023-09-27 18:05:20
我想从选定的行中获得DeptID
,我将gridview绑定如下
private void PopulateGridView()
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM tblEmp", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
dynamicGrid.DataSource = ds;
dynamicGrid.DataBind();
}
为选定行应用颜色,如下所示
protected void dynamicGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow))
{
e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + e.Row.ClientID + "')");
// e.Row.Attributes.Add("onclick", "location='Default9.aspx?id=" + e.Row.Cells[0].Text + "'");
//e.Row.Attributes["onClick"] = "location.href='Default8.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}
}
使用的脚本如下
<script type="text/javascript">
//variable that will store the id of the last clicked row
var previousRow;
function ChangeRowColor(row)
{
//If last clicked row and the current clicked row are same
if (previousRow == row)
return;//do nothing
//If there is row clicked earlier
else if (previousRow != null)
document.getElementById(previousRow).style.backgroundColor = "#ffffff";//change the color of the previous row back to white
document.getElementById(row).style.backgroundColor = "#ffffda";//change the color of the current row to light yellow
window.open("Default9.aspx");
//assign the current row id to the previous row id for next row to be clicked
previousRow = row;
}
</script>
现在我想获得特定行的deptid
当用户选择一行时,任何人都可以帮助我,我如何将其传递到我使用的脚本中的以下行
**window.open("Default9.aspx");**
只需更改ChangeColor参数,如下所示:
function ChangeRowColor(row,rowid)
{
...
}
和在服务器端更改onclick javascript:
//Get the value of specified cell
string DeptID = e.Row.Cells[0].Text; //change the index...
e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + e.Row.ClientID + "','"+ DeptID +"')");
在您的RowDataBound
DataRowView drv = e.Row.DataItem as DataRowView;
if ((e.Row.RowType == DataControlRowType.DataRow))
{
int iEmpID = Convert.ToInt32(drv["DeptID"]);
e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + e.Row.ClientID + "','" + iEmpID + "')");
}
和javascript按照Dev
说
好吧,你已经把rowID
传递给你的JavaScript函数,那么为什么不同时传递deptID
呢?你可以在你的ChangeRowColor
函数上设置任意多的参数。
在服务器端,您可以通过以下操作获得deptID
:
int deptID = (int)e.Row["deptID"]; //or maybe use Convert.ToInt32()
欢呼