Edit(Update,Cancel) ,Delete in DataList webcontrol

1
   we  must set different template of the datalist object  as diffirent appearance, such as itemplate,editplate,headertemplate,footertemplate. in edit template, Update function should have the Update command name as 'Update',and the same to Delete command. At the same time , we 'd better set the datakeyfield of the datalist so as to pick one item's key value.
2
   for different command,writing concerned codes.such as datalist1_editcomamnd,datalist1_updatecommand,datalist1_deletecommand

3 examples:
///this is a datalist object html code for its appearance
  <asp:DataList ID="DataList1"  DataKeyField="UserName" runat="server" Height="129px" Width="775px" OnItemCreated="DataList1_ItemCreated" GridLines="Both" OnDeleteCommand="DataList1_DeleteCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand  OnItemCreated="DataList1_ItemCreated">
       <ItemTemplate>
       <asp:Table  BorderWidth=0px BorderColor=Brown   CellPadding=0 CellSpacing=0 runat=server Height=20 ID="tablefuck">
       <asp:TableRow Width=800px Height=100%>
       <asp:TableCell Width=200px HorizontalAlign=center><%#DataBinder.Eval(Container.DataItem,"UserName")%></asp:TableCell>
        <asp:TableCell Width=300px  HorizontalAlign=center><%#DataBinder.Eval(Container.DataItem,"Site")%></asp:TableCell>
        <asp:TableCell Width=300px  HorizontalAlign=center><%#DataBinder.Eval(Container.DataItem,"Planner")%></asp:TableCell>
       <asp:TableCell Width=300px  HorizontalAlign=center>
    
   <asp:LinkButton Text="Edit" runat=server CommandName="Edit" 
>
   <asp:LinkButton Text="Update" runat=server CommandName="Update" 
>
</asp:LinkButton></asp:TableCell>
       </asp:TableRow>
       </asp:Table>
   
       </ItemTemplate>
            <EditItemTemplate>
            <asp:Table runat=server  ID="table1" Height="36px">
           
            <asp:TableRow Height=100% runat=server>
             <asp:TableCell runat=server Width=200px HorizontalAlign=Center><%# DataBinder.Eval(Container.DataItem,"UserName") %></asp:TableCell>
              <asp:TableCell runat=server Width=300px HorizontalAlign=Center>
             <asp:TextBox runat=server  ID="tb_Site"  Text='<%# DataBinder.Eval(Container.DataItem,"Site") %>'></asp:TextBox>
              </asp:TableCell>
             <asp:TableCell runat=server Width=300px HorizontalAlign=Center>
            <asp:TextBox runat=server ID="tb_Planner"  Text='<%# DataBinder.Eval(Container.DataItem,"Planner") %>' />
             </asp:TableCell>
            <asp:TableCell runat=server Width=300px HorizontalAlign=Center>
            
 <asp:LinkButton Text="Update  " CommandName="Update" runat=server  ID="lb_Update" />
           
<asp:LinkButton Text="  Cancel" CommandName=" Cancel" runat=server /> 
            </asp:TableCell>
            </asp:TableRow>
            </asp:Table>
              
            </EditItemTemplate>
            <HeaderTemplate>
           
              <asp:Table runat=server ID=fuck CellPadding="0" CellSpacing="0" Height="22px" BackColor=black ForeColor=white>
             <asp:TableRow Width=800px BorderWidth=0px runat="server" >
            <asp:TableCell  Width=200px  runat=server  HorizontalAlign=Center >UserName</asp:TableCell>
            <asp:TableCell  Width=300px  runat=server  HorizontalAlign=Center>Site</asp:TableCell>
            <asp:TableCell  Width=300px runat=server  HorizontalAlign=Center>Planner</asp:TableCell>
             <asp:TableCell ID="TableCell1"  Width=300px runat=server  HorizontalAlign=Center>Operation</asp:TableCell>
             </asp:TableRow>
            
              </asp:Table>
            </HeaderTemplate>
          
        </asp:DataList></div>

///corresponding codes for edit(update,cancel),delete

protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
           DataList1.EditItemIndex = e.Item.ItemIndex;
        DataBind();
 RePaintAllControl(e.Item.ItemIndex);
    }

private void RePaintAllControl(int itemIndex)
    {
#if HRG_VERSION
//给contronl增加属性对话框
        ImageButton lb;
        lb = (ImageButton)DataList1.Items[itemIndex].FindControl("lb_Update");
        System.Diagnostics.Debug.Assert(lb != null);
        lb.Attributes.Add("onclick", "return confirm('Are you sure to update?')");
        TextBox ddl;

        ddl = (TextBox)DataList1.Items[itemIndex].FindControl("tb_Planner");
      
  ddl = (DropDownList)DataList1.Items[itemIndex].FindControl("ddl_category");
            ........
            .......
       
#endif
    }

protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
    {
       // Response.Write(e.Item.ItemIndex);
        ImageButton ib;
        ib = (ImageButton)e.Item.FindControl("ib_Delete");
        if (ib != null)
        {
            ib.Attributes.Add("onclick", "return confirm('Are you sure to delete the bug which Id is "+DataList1.DataKeys[e.Item.ItemIndex]+"')");
        }
    }

   protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        Response.Write("<script>alert(\"Delete comamd\")</script>");
    }
   
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        connOpen();
        SqlCommand cmd = new SqlCommand("Pr_UpdateUser", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        Response.Write(((TextBox)e.Item.FindControl("tb_Site")).Text.Trim());


        SqlParameter username = new SqlParameter("@UserName", SqlDbType.VarChar, 20);
        username.Value = DataList1.DataKeys[e.Item.ItemIndex].ToString().Trim();
        cmd.Parameters.Add(username);

        SqlParameter site = new SqlParameter("@Site", SqlDbType.VarChar, 10);
        site.Value = ((TextBox)e.Item.FindControl("tb_Site")).Text.Trim();
        cmd.Parameters.Add(site);

        SqlParameter planner = new SqlParameter("@Planner", SqlDbType.VarChar, 10);
        planner.Value = ((TextBox)e.Item.FindControl("tb_Planner")).Text.Trim();
        cmd.Parameters.Add(planner);

        cmd.ExecuteNonQuery();
        DataList1.EditItemIndex = -1;
        DataBind();
    }


posted on 2008-01-06 13:03  飞天舞者  阅读(525)  评论(0编辑  收藏  举报

导航

For more information about me, feel free email to me winston.he@hotmail.com