没报错,但修改不了datelist绑定在TextBox的值啊,大家帮我看看是什么问题
这个小小问题,弄了我半天弄不出来,晕了。大家帮帮忙
前台
<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();
}
}
}
[解决办法]
//改為這個if (!Page.IsPostBack) { Data_Bind(); }