格式如下图:
前台格式设置如下:
<asp:DataList ID="DataList1" runat="server" Style="font-size: smaller" OnItemDataBound="DataList1_ItemDataBound" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand" onupdatecommand="DataList1_UpdateCommand"> <ItemTemplate> <table style="border: solid 1px black" cellpadding="0" cellspacing="0"> <tr> <td rowspan="4" style="border-right: solid 1px black; border-bottom: solid 1px black"> <img src='<%#Eval("EmpImage") %>' height="100px" width="120px" /> </td> </tr> <tr> <td style="border-bottom: solid 1px black"> 姓名:<%#Eval("EmpName") %> </td> </tr> <tr> <td style="border-bottom: solid 1px black"> 性别:<%#Eval("EmpSex") %> </td> </tr> <tr> <td style="border-bottom: solid 1px black"> 库存: <asp:Label ID="Label1" runat="server" Text='<%#Eval("UnitCount") %>'></asp:Label> </td> </tr> <tr> <td colspan="2"> 地址: <%#Eval("EmpAddress") %> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="Button1" CommandName="Edit" runat="server" Text="编辑" /> </td> </tr> </table> </ItemTemplate> <EditItemTemplate> <table style="border: solid 1px black" cellpadding="0" cellspacing="0"> <tr> <td rowspan="4" style="border-right: solid 1px black; border-bottom: solid 1px black"> <img src='<%#Eval("EmpImage") %>' height="100px" width="120px" /> </td> </tr> <tr> <td style="border-bottom: solid 1px black"> 姓名:<asp:TextBox ID="TextBox1" runat="server" Width="60px" Text='<%#Eval("EmpName") %>'></asp:TextBox> </td> </tr> <tr> <td style="border-bottom: solid 1px black"> 性别: <asp:HiddenField ID="HiddenField1" runat="server" Value ='<%#Eval("EmpSex") %>' /> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="请选择" Value="0"></asp:ListItem> <asp:ListItem Text="男" Value="1"></asp:ListItem> <asp:ListItem Text="女" Value="2"></asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td style="border-bottom: solid 1px black"> 库存:<%#Eval("UnitCount") %> </td> </tr> <tr> <td colspan="2"> 地址: <asp:TextBox ID="TextBox4" TextMode="MultiLine" runat="server" Text='<%#Eval("EmpAddress") %>'></asp:TextBox> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="Button2" runat="server" Text="修改" CommandName="Update" /> <asp:Button ID="Button3" runat="server" Text="取消" CommandName="Cancel" /> </td> </tr> </table> </EditItemTemplate> <%-- <AlternatingItemTemplate> <hr /> </AlternatingItemTemplate>--%> </asp:DataList>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { databind(); } } public void databind() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString()); SqlCommand cmd = new SqlCommand("SELECT * FROM T_Users", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); this.DataList1.DataSource = ds.Tables[0]; this.DataList1.DataKeyField = "EmpID"; // this.GridView1.DataKeyNames = new string[] { "CustomerID", "City" }; this.DataList1.DataBind(); } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item|| e.Item.ItemType == ListItemType .AlternatingItem) { Label lb = (Label)e.Item.FindControl("Label1"); if (lb.Text == "1") { lb.Text = "库存不足"; } } else if (e.Item.ItemType == ListItemType.EditItem) { DropDownList dr = (DropDownList)e.Item.FindControl("DropDownList1"); if (dr != null) { string str = ((HiddenField)e.Item.FindControl("HiddenField1")).Value; if (str == "男") { dr.SelectedIndex = 1; } else { dr.SelectedIndex = 2; } } } // e.Item.Attributes.Add("style", "border-color:black"); } protected void DataList1_EditCommand(object source, DataListCommandEventArgs e) { this.DataList1.EditItemIndex = e.Item.ItemIndex; databind(); } protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e) { this.DataList1.EditItemIndex = -1; databind(); } protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString()); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "Update T_Users set EmpName=@EmpName,EmpSex=@EmpSex,EmpAddress=@EmpAddress where EmpID=@EmpID"; SqlParameter[] sp = new SqlParameter[4]; sp[0] = new SqlParameter("@EmpName",SqlDbType.NVarChar,20); sp[1] = new SqlParameter("@EmpSex",SqlDbType.NVarChar,4 ); sp[2] = new SqlParameter("@EmpAddress",SqlDbType.NVarChar,50); sp[3] = new SqlParameter("@EmpID",SqlDbType.Int); sp[0].Value = ((TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox1")).Text; sp[1].Value = ((DropDownList)this.DataList1.Items[e.Item.ItemIndex].FindControl("DropDownList1")).SelectedItem.Text; sp[2].Value = ((TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox3")).Text; sp[3].Value = this.DataList1.DataKeys[e.Item.ItemIndex].ToString(); cmd.Parameters.AddRange(sp); if (con.State == ConnectionState.Closed) { con.Open(); } cmd.ExecuteNonQuery(); this.DataList1.EditItemIndex = -1; databind(); }
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏