where are you handling the event? in ItemCommand or LinkButton's own handler?in ItemCommand handler for the DataGrid, void DataGrid_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
//e.Item.Cells[0].Text, e.Item.Cells[2].Text
}in LinkButton's own handlervoid LinkButton_Click(Object sender, EventArgs e)
{
DataGridItem dgi = (DataGridItem)((LinkButton))sender).Parent.Parent;
//dgi.Cells[0].Text, dgi.Cells[2].Text
}
{
//e.Item.Cells[0].Text, e.Item.Cells[2].Text
}in LinkButton's own handlervoid LinkButton_Click(Object sender, EventArgs e)
{
DataGridItem dgi = (DataGridItem)((LinkButton))sender).Parent.Parent;
//dgi.Cells[0].Text, dgi.Cells[2].Text
}
接下来,编写一个函数,起作用是合并数据:
public string joinString(object obj1,object obj2)
{
return (string)obj1 + (string)obj2;
}
再在HTML代码中想要使用连接的地方写入以下代码:
a.使用DataReader绑定数据:
NavigateURL = <%#joinString(((System.Data.Common.DbDataRecord)Container.DataItem)["字段名1"],((System.Data.Common.DbDataRecord)Container.DataItem)["字段名2"])%>
b.使用DataTable或DataView绑定数据:
NavigateURL = <%#joinString(((System.Data.DataRowView)Container.DataItem)["字段名1"],((System.Data.DataRowView)Container.DataItem)["字段名2"])%>另外可参考:
http://www.tek-tips.com/gfaqs.cfm/pid/855/fid/4372
void DataGrid_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
if(e.Item.Command=="xxx")
{
//e.Item.Cells[0].Text, e.Item.Cells[2].Text 取值
}
}
void DataGrid_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
if(e.Item.Command=="xxx")
{
//string sItem0 = e.Item.Cells[0].Text;
//string sItem2 = e.Item.Cells[2].Text;
}
}
把这段代码放在项目中,编译无法通过,提示System.Web.UI.WebControls.DataGridItem并不包含对"command"的定义!!!什么原因???在线等!!急,解决马上给分!!!!!!!!!!!!!!!!!!!!------------------双击你的Datagrid 让它自动生成事件句柄或从code视图下选DataGrid的ItemCommand事件
不用自己手写
Text="Order Item 10002"
CommandName="abc"
.....
Runat="server"/> if (((LinkButton)e.CommandSource).CommandName == "abc")
{
........
}
你确认生成DataGrid_ItemCommand事件了吗 而且有句柄吗
假设第三列的CommandName是Add
if((((LinkButton)e.CommandSource).CommandName == "Add"))
{
string s = e.Items.Cell[0].Text;
…………
}