首页 新闻 会员 周边 捐助

ListView 控件如何增加 编辑,删除,更新按钮

0
悬赏园豆:10 [已解决问题] 解决于 2012-07-18 13:09
我现在写了如下代码,但是没有增加 编辑,删除,更新按钮,都说Listview控件是万能的,那么如何增加这几个按钮呢,非常非常感谢.另外这几个按钮如何添加事件, 要像Gridview一样,点击的时候,可以击发事件,便于写代码,

<asp:ListView ID="ListView1" runat="server">
  <LayoutTemplate>
  <table>
  <thead>
  <tr>
  <th>ID</th>
  <th>业务类型</th>
  <th>订单编号</th>
  <th>加工点</th>
  <th>存货编号</th>
  <th>存货名称</th>
  <th>规格型号</th>
  <th>单位</th>
  <th>工程图版次</th>
  <th>客户型号</th>
  <th>包装描述</th>
  <th>订单日期</th>
  <th>计划到货日期</th>
  <th>计划下达日期</th>
  <th>数量</th>
  </tr>
  </thead>
  <tbody>
  <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
  </tbody>
  </table>
  </LayoutTemplate>
  <ItemTemplate>
  <tr>
  <td><%# Eval("ID") %></td>
  <td><%# Eval("ba01") %></td>
  <td><%# Eval("ba02") %></td>
  <td><%# Eval("ba03") %></td>
  <td><%# Eval("ba04") %></td>
  <td><%# Eval("ba05") %></td>
  <td><%# Eval("ba06") %></td>
  <td><%# Eval("ba07") %></td>
  <td><%# Eval("ba08") %></td>
  <td><%# Eval("ba09") %></td>
  <td><%# Eval("ba10") %></td>
  <td><%# Eval("ba51", "{0:d}") %></td>
  <td><%# Eval("ba52", "{0:d}") %></td>
  <td><%# Eval("ba53", "{0:d}") %></td>
  <td><%# Eval("ba91") %></td>
  </tr>
  </ItemTemplate>
  </asp:ListView>

  

zhengyingcan的主页 zhengyingcan | 初学一级 | 园豆:12
提问于:2012-07-02 16:24
< >
分享
最佳答案
0
View Code
<asp:ListView ID="lvEmployee" runat="server"    
    onitemediting="lvEmployee_ItemEditing"
    onitemupdating="lvEmployee_ItemUpdating"
    onitemcanceling="lvEmployee_ItemCanceling"
    onitemdeleting="lvEmployee_ItemDeleting"
    InsertItemPosition="LastItem"
    oniteminserting="lvEmployee_ItemInserting">
<LayoutTemplate>
<table id="Table1" runat="server">
    <tr id="Tr1" runat="server">
<td id="Td1" runat="server">
    <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">
        </th>
<th id="Th2" runat="server">
     EmpID</th>
<th id="Th3" runat="server">
     EmpName</th>
<th id="Th4" runat="server">
     Department</th>
<th id="Th5" runat="server">
     Age</th>
        <th id="Th6" runat="server">
     Address</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
     </table>
</td>
    </tr>
    <tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
</td>
    </tr>
</table>
</LayoutTemplate>
<ItemTemplate>
        <tr style="">
    <td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
    Text="Delete" />
        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
    </td>
    <td>
<asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' />
            </td>
    <td>
<asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
    </td>
    <td>
<asp:Label ID="DepartmentLabel" runat="server"
    Text='<%# Eval("Department") %>' />
    </td>
    <td>
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
    </td>
    <td>
<asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
    </td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr style="">
    <td>
        <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
    Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
    Text="Cancel" />
    </td>
    <td>
<asp:Label ID="EmpIDLabel1" runat="server" Text='<%# Eval("EmpID") %>' />
    </td>
    <td>
<asp:TextBox ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>' />
    </td>
    <td>
<asp:TextBox ID="DepartmentTextBox" runat="server"
    Text='<%# Bind("Department") %>' />
            </td>
    <td>
<asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' />
    </td>
    <td>
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' />
    </td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr style="">
    <td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
    Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
    Text="Clear" />
    </td>
    <td>
        &nbsp;</td>
    <td>
<asp:TextBox ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>' />
    </td>
    <td>
<asp:TextBox ID="DepartmentTextBox" runat="server"
    Text='<%# Bind("Department") %>' />
    </td>
    <td>
<asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' />
            </td>
    <td>
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' />
    </td>
</tr>
</InsertItemTemplate>
</asp:ListView>
2.      In the codebehind file, assign the DataTable or DataSet to the DataSource Property of the ListView control, like we do for a GridView control.
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
    lvEmployee.DataSource = GetEmployee("Select * from Employee");
    lvEmployee.DataBind();
        }
    }
    public DataTable GetEmployee(string query)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
        SqlDataAdapter ada = new SqlDataAdapter(query, con);
        DataTable dtEmp = new DataTable();
        ada.Fill(dtEmp);
        return dtEmp;
    }
3.      We can write our own code to edit, update, delete and insert in the corresponding events of the ListView control. Refer the below code.
protected void lvEmployee_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvEmployee.EditIndex = e.NewEditIndex;
    lvEmployee.DataSource = GetEmployee("Select * from Employee");
    lvEmployee.DataBind();
    }
  protected void lvEmployee_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
    string empid = "", name = "", dept = "", age = "", address = "";
    Label lbl = (lvEmployee.Items[e.ItemIndex].FindControl("EmpIDLabel1")) as Label;
    if (lbl != null)
empid = lbl.Text;
    TextBox txt = (lvEmployee.Items[e.ItemIndex].FindControl("EmpNameTextBox")) as TextBox;
    if (txt != null)
name = txt.Text;
    txt = (lvEmployee.Items[e.ItemIndex].FindControl("DepartmentTextBox")) as TextBox;
    if (txt != null)
dept = txt.Text;
    txt = (lvEmployee.Items[e.ItemIndex].FindControl("AgeTextBox")) as TextBox;
    if (txt != null)
age = txt.Text;
    txt = (lvEmployee.Items[e.ItemIndex].FindControl("AddressTextBox")) as TextBox;
    if (txt != null)
address = txt.Text;
    string UpdateQuery = "UPDATE [Employee] SET [EmpName] = '" + name + "', [Department] = '" + dept + "', [Age] = '" + age + "', [Address] = '" + address + "' WHERE [EmpID] = '" + empid + "'";
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
    con.Open();
    SqlCommand com = new SqlCommand(UpdateQuery, con);
    com.ExecuteNonQuery();
    con.Close();
    lvEmployee.EditIndex = -1;       
    lvEmployee.DataSource = GetEmployee("Select * from Employee");
    lvEmployee.DataBind();
    }
    protected void lvEmployee_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvEmployee.EditIndex = -1;
    lvEmployee.DataSource = GetEmployee("Select * from Employee");
    lvEmployee.DataBind();
    }
    protected void lvEmployee_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        string empid = "";
        Label lbl = (lvEmployee.Items[e.ItemIndex].FindControl("EmpIDLabel")) as Label;
        if (lbl != null)
    empid = lbl.Text;
        string DeleteQuery = "Delete from Employee WHERE [EmpID] = '" + empid + "'";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand(DeleteQuery, con);
        com.ExecuteNonQuery();
        con.Close();
        lvEmployee.EditIndex = -1;
        lvEmployee.DataSource = GetEmployee("Select * from Employee");
        lvEmployee.DataBind();
    }
protected void lvEmployee_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string name = "", dept = "", age = "", address = "";
        TextBox txt = (e.Item.FindControl("EmpNameTextBox")) as TextBox;
        if (txt != null)
    name = txt.Text;
        txt = (e.Item.FindControl("DepartmentTextBox")) as TextBox;
        if (txt != null)
    dept = txt.Text;
        txt = (e.Item.FindControl("AgeTextBox")) as TextBox;
        if (txt != null)
    age = txt.Text;
        txt = (e.Item.FindControl("AddressTextBox")) as TextBox;
        if (txt != null)
    address = txt.Text;
        string INSERTQuery = "INSERT INTO [Employee] (EmpName,Department,Age,Address) VALUES ('" + name + "','" + dept + "','" + age + "','" + address + "')";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand(INSERTQuery, con);
        com.ExecuteNonQuery();
        con.Close();
        lvEmployee.EditIndex = -1;  
        lvEmployee.DataSource = GetEmployee("Select * from Employee");
        lvEmployee.DataBind();
    }

http://www.codedigest.com/articles/aspnet/105_editupdatedelete_and_insert_in_listview_control.aspx

这个不能再详细了。

收获园豆:10
悟行 | 专家六级 |园豆:12559 | 2012-07-02 16:33

这个是自动生成的,很多时候,没有这么自动化呀,这也不够灵活.有没有手工化一点的.

zhengyingcan | 园豆:12 (初学一级) | 2012-07-02 16:36
悟行 | 园豆:12559 (专家六级) | 2012-07-02 16:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册