我试过了 不行啊 不执行事件 在 <asp:datagrid ... onitemcommand="mygrid_itemcommand"...>代码中这样写的 : sub mygrid_itemcommand(sender As Object, e As DataGridCommandEventArgs) if e.commandname="img1_click" then Dim mygrid3 As DataGrid mygrid3 = CType(e.Item.FindControl("mygrid2"), DataGrid) dim itemcell as tablecell=e.item.cells(2) dim d_id=itemcell.text response.write(d_id) sql="select * from [Office] where D_ID='"& d_id &"'" response.write(sql) myadapter=new sqldataadapter(sql,myconn) myconn.open() myadapter.fill(mydataset,"office") mygrid3.datasource=mydataset.tables("office") mygrid3.databind() myconn.close() end if end sub
用
<asp:image有对应的事件
<asp:ImageButton id="ImageButton1" runat="server" CommandName="image_click"></asp:ImageButton>然后,在datagrid的ItemCommand事件中加入代码(假设key是string)private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if(e.CommandName=="image_click")
{
string currentKey = (string)DataList1.DataKeys[e.Item.ItemIndex]));
}
}
在datagrid的ItemCommand事件绑定的函数里,如:private void MyDataGrid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName == "mycmd")
{
…… '你的过程
}
}
比如:绑定列为第一列:
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write("<script>alert('"+e.Item.Cells[1].Text+"');</script>");
}
sub mygrid_itemcommand(sender As Object, e As DataGridCommandEventArgs)
if e.commandname="img1_click" then
Dim mygrid3 As DataGrid
mygrid3 = CType(e.Item.FindControl("mygrid2"), DataGrid)
dim itemcell as tablecell=e.item.cells(2)
dim d_id=itemcell.text
response.write(d_id)
sql="select * from [Office] where D_ID='"& d_id &"'"
response.write(sql)
myadapter=new sqldataadapter(sql,myconn)
myconn.open()
myadapter.fill(mydataset,"office")
mygrid3.datasource=mydataset.tables("office")
mygrid3.databind()
myconn.close()
end if
end sub
要是没有,则在itemcommand 栏双击鼠标左键。会执行你的事件的。
应该在 "属性"-"事件" 里面的 ItemCommand 事件栏 双击激活itemCommand事件,这样会自动生成该事件的委托代码,然后在生成的函数里面写就可以了