C# 中点击gridview中某字段时在下面显示详细信息的代码?有源代码但是有点小疑问,望帮忙解决一下。。。
aspx代码:<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"BorderColo...
aspx代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderColor="Black"
Font-Size="12px" Width="549px" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField HeaderText="emp_id" DataTextField="emp_id" CommandName="empid" />
</Columns>
</asp:GridView>
GridView2:显示GridView1字段连接
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
GridView1:点击字段
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
string sql = " select top 5 * from emp_info";
DataTable dttable = DB.DatAdapter_DT(sql);
this.GridView1.DataSource = dttable;
this.GridView1.DataKeyNames = new string[] { "emp_id" };
this.GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "empid")
{
int index;
try
{
index = Convert.ToInt32(e.CommandArgument.ToString());
}
catch
{
return;
}
string id = this.GridView1.DataKeys[index]["emp_id"].ToString().Trim();
string sql = " select * from emp_info where emp_id = '" + id + "' ";
DataTable dttable = DB.DatAdapter_DT(sql);
this.GridView2.DataSource = dttable;
this.GridView2.DataBind();
}
}
这是源代码,疑问的是在后台代码里DataTable dttable = DB.DatAdapter_DT(sql);这句中的DB是从哪里来的,在我页面时总是报这个错我也搞不清楚,谁能帮忙解决一下,在这谢谢了。。。 展开
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderColor="Black"
Font-Size="12px" Width="549px" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField HeaderText="emp_id" DataTextField="emp_id" CommandName="empid" />
</Columns>
</asp:GridView>
GridView2:显示GridView1字段连接
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
GridView1:点击字段
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
string sql = " select top 5 * from emp_info";
DataTable dttable = DB.DatAdapter_DT(sql);
this.GridView1.DataSource = dttable;
this.GridView1.DataKeyNames = new string[] { "emp_id" };
this.GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "empid")
{
int index;
try
{
index = Convert.ToInt32(e.CommandArgument.ToString());
}
catch
{
return;
}
string id = this.GridView1.DataKeys[index]["emp_id"].ToString().Trim();
string sql = " select * from emp_info where emp_id = '" + id + "' ";
DataTable dttable = DB.DatAdapter_DT(sql);
this.GridView2.DataSource = dttable;
this.GridView2.DataBind();
}
}
这是源代码,疑问的是在后台代码里DataTable dttable = DB.DatAdapter_DT(sql);这句中的DB是从哪里来的,在我页面时总是报这个错我也搞不清楚,谁能帮忙解决一下,在这谢谢了。。。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询