2个DataGrid ,DG1绑定表1,点击DG1的一行,根据其 e.Item.ItemIndex 将表2中,id与之相等的数据查询出来,显示在Dg2中,以下是我的部分代码,但是点击之后,没响应,IE左下角有个黄三角。
dg1是绑定在Page_Load里面。
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Style.Add("cursor", "hand");
e.Item.Attributes.Add("onclick","showdg2('"+e.Item.ItemIndex+"');");
}
}
private void showdg2(int index)
{
try
{
string str = "data source = me;uid=sa;pwd=;database=me;";
string strsql = "select * from rocompany1124 where id = '<$index>'";
strsql = strsql.Replace("<$index>",index.ToString());
SqlConnection sqlconn = new SqlConnection(str);
SqlCommand sqlcmd = new SqlCommand(strsql,sqlconn);
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
sqlda.Fill(ds);
DataGrid2.DataSource=ds;
DataGrid2.DataBind();
}
catch(SqlException ex)
{
Response.Write(ex.Message);
}
}
设置断点,在showdg2 根本停不了。谢了
dg1是绑定在Page_Load里面。
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Style.Add("cursor", "hand");
e.Item.Attributes.Add("onclick","showdg2('"+e.Item.ItemIndex+"');");
}
}
private void showdg2(int index)
{
try
{
string str = "data source = me;uid=sa;pwd=;database=me;";
string strsql = "select * from rocompany1124 where id = '<$index>'";
strsql = strsql.Replace("<$index>",index.ToString());
SqlConnection sqlconn = new SqlConnection(str);
SqlCommand sqlcmd = new SqlCommand(strsql,sqlconn);
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
sqlda.Fill(ds);
DataGrid2.DataSource=ds;
DataGrid2.DataBind();
}
catch(SqlException ex)
{
Response.Write(ex.Message);
}
}
设置断点,在showdg2 根本停不了。谢了
onclike是客户端脚本,showdg2是服务器端函数,根本联系不到一起
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
showdg2(Convert.ToInt32(DataGrid1.SelectedItem.Cells[Index].Text));
}
Index:id所在列
给各事例,谢谢
To czsxj(知道一点)
点击 某一行没有反应,另外
e.Item.Attributes.Add("onclick","showdg2('"+e.Item.ItemIndex+"');");
这句要还是不要呢?
这句有没有,好像根本触发不了 SelectIndexChanged 这个事件啊
在e.Item里面建一个隐藏的服务器控件Button,用Style="display:none",不要用Visible。
然后
e.Item.Attributes.Add("onclick", "somebutton.Click();");
当然somebutton要通过FindControl和ClientID获得
这种方法比较简单
要不就自己做个控件实现IPostBackEventHandler
e.Item.Attributes.Add("onclick","showdg2('"+e.Item.ItemIndex+"');");不要了
<HeaderStyle Width="100px"></HeaderStyle> </asp:ButtonColumn>
给这列加上触发事件<asp:datagrid id="MyDataGrid".......
OnEditCommand="SelectThenInput"......>
//分别把datagrid行中第二列第三列的值赋给两个textbox.
public void SelectThenInput(object sender, DataGridCommandEventArgs e)
{
this.Textbox3.Text = e.Item.Cells[1].Text.ToString ();
this.Textbox4.Text = e.Item.Cells[2].Text.ToString();
e.Item.BackColor= Color.Green ;
}
弱弱的问一下,
DataGrid的SelectedIndexChanged怎么才能起作用?