我做了个就像csdn提问一样的界面,点击问题的title自动跳转到新页面显示问题的详细信息,用id主键控制,但是调试之后还是只显示其实一个id的固定的问题详细内容。我想可能是Session["id"]的问题。求解答,以下是代码
在第一个consult_update页面,<html>里德datalist代码如下
<div class="flexme">
<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" DataKeyField="id" OnItemDataBound="DataList1_ItemDataBound">
<AlternatingItemStyle BackColor="White" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#EFF3FB" />
<itemtemplate>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="left"><a target='_blank' href="xxxx/consult_update1.aspx?id=<%#Eval("id") %>"> <%# DataBinder.Eval(Container.DataItem,"Title") %></a></div></td>
<td><div align="right"><%# DataBinder.Eval(Container.DataItem,"Date") %></div></td>
</tr>
</table>
</itemtemplate>
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</div>
consult_update.cs代码page_load中
string sqlStr = @"server=xxxxxx;database=xxxx;user id=sa;password=xxxxx;";
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();//打开连接
string sSql = "select id,Title,Date from consult where name = '"+ lbl_name.Text +"' and company ='"+lbl_com_cn.Text+"' order by Date desc";
SqlCommand cmd = new SqlCommand(sSql, conn);//定义查询命令
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds,"Title");
DataTable dt = ds.Tables["Title"];
DataList1.DataSource = dt;
DataList1.DataKeyField ="id";
DataList1.DataBind(); conn.Close();
//此处我将int的 id 转换成string格式, 用Session转到下一页面
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int id = (int)DataList1.DataKeys[e.Item.ItemIndex];
string id1= Convert.ToString(id);
string id2 = id.ToString();
Session["id"] = id.ToString();
}
consult_update1.cs代码
string sqlStr = @"server=xxxx;database=xxx;user id=sa;password=xxxx;";
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();//打开连接
string sSql = "select Title,Body from consult where name = '"+ lbl_name.Text +"' and company ='"+lbl_com_cn.Text+"' and id='"+Session["id"].ToString()+"'"; //此处我想让之前页面的Session["id"]来查询数据库里的id.取出id对应的整条数据取Title和body
SqlCommand cmd = new SqlCommand(sSql, conn);//定义查询命令
SqlDataReader sRead = cmd.ExecuteReader();//执行查询
if(sRead.Read())
{
this.lbl_title.Text = sRead[0].ToString();
this.lbl_body.Text = sRead[1].ToString();
}
sRead.Close(); conn.Close();
求大神解答啊。我现在调取的Title和Body 都是id = 5
不知道什么原因,求具体解答,是哪里出错了
在第一个consult_update页面,<html>里德datalist代码如下
<div class="flexme">
<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" DataKeyField="id" OnItemDataBound="DataList1_ItemDataBound">
<AlternatingItemStyle BackColor="White" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#EFF3FB" />
<itemtemplate>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="left"><a target='_blank' href="xxxx/consult_update1.aspx?id=<%#Eval("id") %>"> <%# DataBinder.Eval(Container.DataItem,"Title") %></a></div></td>
<td><div align="right"><%# DataBinder.Eval(Container.DataItem,"Date") %></div></td>
</tr>
</table>
</itemtemplate>
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</div>
consult_update.cs代码page_load中
string sqlStr = @"server=xxxxxx;database=xxxx;user id=sa;password=xxxxx;";
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();//打开连接
string sSql = "select id,Title,Date from consult where name = '"+ lbl_name.Text +"' and company ='"+lbl_com_cn.Text+"' order by Date desc";
SqlCommand cmd = new SqlCommand(sSql, conn);//定义查询命令
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds,"Title");
DataTable dt = ds.Tables["Title"];
DataList1.DataSource = dt;
DataList1.DataKeyField ="id";
DataList1.DataBind(); conn.Close();
//此处我将int的 id 转换成string格式, 用Session转到下一页面
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int id = (int)DataList1.DataKeys[e.Item.ItemIndex];
string id1= Convert.ToString(id);
string id2 = id.ToString();
Session["id"] = id.ToString();
}
consult_update1.cs代码
string sqlStr = @"server=xxxx;database=xxx;user id=sa;password=xxxx;";
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();//打开连接
string sSql = "select Title,Body from consult where name = '"+ lbl_name.Text +"' and company ='"+lbl_com_cn.Text+"' and id='"+Session["id"].ToString()+"'"; //此处我想让之前页面的Session["id"]来查询数据库里的id.取出id对应的整条数据取Title和body
SqlCommand cmd = new SqlCommand(sSql, conn);//定义查询命令
SqlDataReader sRead = cmd.ExecuteReader();//执行查询
if(sRead.Read())
{
this.lbl_title.Text = sRead[0].ToString();
this.lbl_body.Text = sRead[1].ToString();
}
sRead.Close(); conn.Close();
求大神解答啊。我现在调取的Title和Body 都是id = 5
不知道什么原因,求具体解答,是哪里出错了
但是这是在登录界面里,数据库中主键id不是只有此用户,可能id 是 3.10 20.60 等如何调用啊。
session好像传输不变的量。这种变量有什么好方法么?
{
if (!IsPostBack)
{
XXXX
DataList1.DataSource = dt;
DataList1.DataKeyField ="id";
DataList1.DataBind();
第一个页面 我改成这个了 protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind_cus_emp_basic(); string sqlStr = @"server=SVCTAG-4R2JY2X;database=shhr;user id=sa;password=capslock1911;";
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();//打开连接
string sSql = "select id,Title,Date from consult where name = '"+ lbl_name.Text +"' and company ='"+lbl_com_cn.Text+"' order by Date desc";
SqlCommand cmd = new SqlCommand(sSql, conn);//定义查询命令
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds,"Title");
DataTable dt = ds.Tables["Title"];
DataList1.DataSource = dt;
DataList1.DataKeyField ="id";
DataList1.DataBind(); conn.Close(); }
}之前绑的是cus_emp_basic()另一个功能,这样还是不行。这样2个事一起绑的么?