这个小小问题,弄了我半天弄不出来,晕了。大家帮帮忙
前台
<asp:DataList ID="DataList1" runat="server" Width="500px"
onitemcommand="DataList1_ItemCommand" >
<ItemTemplate>
<table cellpadding="0" cellspacing="0" class="style2">
<tr>
<td>
<asp:Label ID="lbId" runat="server" Text='<%#Eval("id") %>' Visible="false"></asp:Label>
<%# Eval("title") %>
</td>
</tr>
<tr>
<td>
<%# Eval("time") %>
</td>
</tr>
<tr>
<td>
<%# Eval("text") %></td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">【修改】</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete">【删除】</asp:LinkButton>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table class="style2">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text="标题:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("title") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="内容" ></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Height="150px" TextMode="MultiLine" Text='<%#Eval("text") %>'
Width="100%"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Update">确认修改</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Cancel">取消</asp:LinkButton>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>后台
protected void Page_Load(object sender, EventArgs e)
{
Data_Bind();
}
protected void Data_Bind()
{
string sql = "select * from notice where id=" + Convert.ToInt32(Request.QueryString["id"]);
string sqlstr = ConfigurationManager.ConnectionStrings["webConnectionString1"].ToString();
SqlConnection con = new SqlConnection(sqlstr);
SqlDataAdapter cmd = new SqlDataAdapter(sql, con);
DataSet dt = new DataSet();
cmd.Fill(dt, "notice");
this.DataList1.DataSource = dt.Tables["notice"].DefaultView;
DataList1.DataKeyField = "id";
this.DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
string sqlstr = ConfigurationManager.ConnectionStrings["webConnectionString1"].ToString();
SqlConnection con = new SqlConnection(sqlstr);
if (e.CommandName == "Edit")
{
DataList1.EditItemIndex = e.Item.ItemIndex;
this.Data_Bind();
} if (e.CommandName == "Update")
{
string ID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
//string id = ((Label)e.Item.FindControl("Label1")).Text;
string title = ((TextBox)e.Item.FindControl("TextBox1")).Text;
string text = ((TextBox)e.Item.FindControl("TextBox2")).Text;
// string sql = "update notice set title='" + title + "',text='" + text + "' where id='" + id + "'";
string sql = "update notice set title='" + title + "',text='" + text + "'where id='" + ID + "' ";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
this.Data_Bind();
}
}
}
前台
<asp:DataList ID="DataList1" runat="server" Width="500px"
onitemcommand="DataList1_ItemCommand" >
<ItemTemplate>
<table cellpadding="0" cellspacing="0" class="style2">
<tr>
<td>
<asp:Label ID="lbId" runat="server" Text='<%#Eval("id") %>' Visible="false"></asp:Label>
<%# Eval("title") %>
</td>
</tr>
<tr>
<td>
<%# Eval("time") %>
</td>
</tr>
<tr>
<td>
<%# Eval("text") %></td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">【修改】</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete">【删除】</asp:LinkButton>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table class="style2">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text="标题:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("title") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="内容" ></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Height="150px" TextMode="MultiLine" Text='<%#Eval("text") %>'
Width="100%"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Update">确认修改</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Cancel">取消</asp:LinkButton>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>后台
protected void Page_Load(object sender, EventArgs e)
{
Data_Bind();
}
protected void Data_Bind()
{
string sql = "select * from notice where id=" + Convert.ToInt32(Request.QueryString["id"]);
string sqlstr = ConfigurationManager.ConnectionStrings["webConnectionString1"].ToString();
SqlConnection con = new SqlConnection(sqlstr);
SqlDataAdapter cmd = new SqlDataAdapter(sql, con);
DataSet dt = new DataSet();
cmd.Fill(dt, "notice");
this.DataList1.DataSource = dt.Tables["notice"].DefaultView;
DataList1.DataKeyField = "id";
this.DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
string sqlstr = ConfigurationManager.ConnectionStrings["webConnectionString1"].ToString();
SqlConnection con = new SqlConnection(sqlstr);
if (e.CommandName == "Edit")
{
DataList1.EditItemIndex = e.Item.ItemIndex;
this.Data_Bind();
} if (e.CommandName == "Update")
{
string ID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
//string id = ((Label)e.Item.FindControl("Label1")).Text;
string title = ((TextBox)e.Item.FindControl("TextBox1")).Text;
string text = ((TextBox)e.Item.FindControl("TextBox2")).Text;
// string sql = "update notice set title='" + title + "',text='" + text + "' where id='" + id + "'";
string sql = "update notice set title='" + title + "',text='" + text + "'where id='" + ID + "' ";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
this.Data_Bind();
}
}
}
解决方案 »
- 求助asp.net移动web控件List的分页程序
- 如何判断一字符串中包含日期格式的字符串
- C#能接受事件的返回值吗
- 请问做模板生成静态时如何处理表单问题.
- 如何将后台数据库表某一个字段的值赋给前台js array?
- 散分求一数据绑定的问题
- 吴旗娃的分页控件能够使用ORACLE的存储过程吗?我现在数据库是ORACLE。
- 公开一套完整的c#多文件上传程序源码,请高手纠正里边的个别错误并重新在此版发布,谢谢!
- 一个项目引用另一个项目,除了“引用”中添加还要做别的吗?为什么我的项目不可以调用???
- 在ASP.net中如何得到一个控件的top,left坐标值?
- 在IIS中配置的网站,凡涉及到从SQL Server数据库读取的信息,都不显示。
- 10分钟通过socket获取一次数据,求解决方案???
//改為這個
if (!Page.IsPostBack)
{
Data_Bind();
}