希望有大神抬我一手。
DATALIST绑定数据
<asp:DataList ID="DataList1" runat="server" CellPadding="10" CellSpacing="20" RepeatDirection="Horizontal" RepeatColumns="5" OnItemDataBound="dtlpic_ItemDataBound" OnItemCommand="dtlpic_ItemCommand">
<ItemTemplate>
<div style="width:114px; height:114px;">
<a href="info.aspx?uid=<%#DataBinder.Eval(Container.DataItem,"frid") %>">
<img style="border:0px" alt="" src="<%#DataBinder.Eval(Container.DataItem,"frtou") %>" width="110"; height="110" />
</a>
</div>
<a href="info.aspx?uid=<%#DataBinder.Eval(Container.DataItem,"myid") %>">
<%#DataBinder.Eval(Container.DataItem,"myid") %></a><br />
<asp:LinkButton ID="lkok" runat="server" CausesValidation="False" CommandName="ok">允许</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Du">拒绝</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
page_load
if (!IsPostBack)
{
uid = Convert.ToInt16(Session["uid"]);
if (uid <= 0)
{
Response.Redirect("/Default.aspx");
}
else
{
string frdsql = "select f.id,f.myid,f.frid,u.username as frname,u.touurl as frtou from friendlist f inner join tb_user u on f.myid=u.id where frid=" + uid + " and states=0";
objb.datalistBind(frdsql, "friendlist", "id", DataList1);
}
}
DATALIST的操作代码
protected void dtlpic_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
LinkButton lbtn = (LinkButton)e.Item.FindControl("lkok");
lbtn.Attributes.Add("onClick", "return confirm('是否确定加为好友!')");
}
}
protected void dtlpic_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "ok")
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();//获取id!!!!就是这里取不到值有点郁闷!!
string sqlsz = "update friendlist set states=1 where id=" + id;
if (sqh.ExecSQL(sqlsz) == false)
{
Response.Write("<script language=javascript>alert('添加失败!!')</script>");
}
else
{
Response.Write("<script language=javascript>alert('添加成功!!')</script>");
}
string frdsql = "select * from friendlist where frid=" + uid + " and state=0";
objb.datalistBind(frdsql, "friendlist", "id", DataList1);
}
if (e.CommandName == "Du")
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();//获取id
string sqlsz = "update friendlist set state=2 where frid=" + id;
if (sqh.ExecSQL(sqlsz) == false)
{
Response.Write("<script language=javascript>alert('添加失败!!')</script>");
}
else
{
Response.Write("<script language=javascript>alert('已拒绝!!')</script>");
}
string frdsql = "select * from friendlist where frid=" + uid + " and state=0 ";
objb.datalistBind(frdsql, "friendlist", "id", DataList1);
}
}
datalist 绑定的类封装函数
public DataSet GetDataSet(string sQueryString, string TableName)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseconnStr"].ConnectionString);
SqlDataAdapter dbAdapter = new SqlDataAdapter(sQueryString, con);
DataSet dataset = new DataSet();
dbAdapter.Fill(dataset, TableName);
return dataset;
}
public void datalistBind(string sql, string tablename, string keyid, DataList dtlbind)
{
dtlbind.DataSource = GetDataSet(sql, tablename);
dtlbind.DataBind();
dtlbind.DataKeyField = keyid;
}
数据库
CREATE TABLE [dbo].[friendlist](
[id] [int] IDENTITY(1,1) NOT NULL,
[myid] [int] NULL,
[frid] [int] NULL,
[frname] [nvarchar](50) NULL,
[frtou] [nvarchar](50) NULL,
[states] [int] NULL
) ON [PRIMARY]
GO
3 个解决方案
#1
#2
哎,睡觉都睡不好,梦里把程序走了一遍发现问题解决了,到现在都没人回有点落寞。
#3
把!ISPOSTBACK去掉就行了
#1
#2
哎,睡觉都睡不好,梦里把程序走了一遍发现问题解决了,到现在都没人回有点落寞。
#3
把!ISPOSTBACK去掉就行了